init
This commit is contained in:
90
ch6/analyze.py
Normal file
90
ch6/analyze.py
Normal file
@@ -0,0 +1,90 @@
|
||||
import ch1text
|
||||
|
||||
def count_syllables_in_word(word):
|
||||
count = 0
|
||||
|
||||
endings = '.,;!?:'
|
||||
last_char = word[-1]
|
||||
|
||||
if last_char in endings:
|
||||
processed_word = word[:-1]
|
||||
else:
|
||||
processed_word = word
|
||||
|
||||
if len(processed_word) <= 3:
|
||||
return 1
|
||||
|
||||
if processed_word[-1] in 'eE':
|
||||
processed_word = processed_word[:-1]
|
||||
|
||||
vowels = 'aeiouAEIOU'
|
||||
prev_char_was_vowel = False
|
||||
for char in processed_word:
|
||||
if char in vowels:
|
||||
if not prev_char_was_vowel:
|
||||
count += 1
|
||||
prev_char_was_vowel = True
|
||||
else:
|
||||
prev_char_was_vowel = False
|
||||
|
||||
if processed_word[-1] in 'yY':
|
||||
count += 1
|
||||
|
||||
return count
|
||||
|
||||
def count_syllables(words):
|
||||
count = 0
|
||||
|
||||
for word in words:
|
||||
word_count = count_syllables_in_word(word)
|
||||
count = count + word_count
|
||||
|
||||
return count
|
||||
|
||||
def count_sentences(text):
|
||||
count = 0
|
||||
|
||||
for char in text:
|
||||
if char in ('.', '?', '!'):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def output_results(score):
|
||||
if score >= 90:
|
||||
return 'Уровень 5-го класса'
|
||||
elif score >= 80:
|
||||
return 'Уровень 6-го класса'
|
||||
elif score >= 70:
|
||||
return 'Уровень 7-го класса'
|
||||
elif score >= 60:
|
||||
return 'Уровень 8-9-го класса'
|
||||
elif score >= 50:
|
||||
return 'Уровень 10-11-го класса'
|
||||
elif score >= 30:
|
||||
return 'Уровень студента вуза'
|
||||
else:
|
||||
return 'Уровень выпускнинка вуза'
|
||||
|
||||
def compute_readability(text):
|
||||
total_words = 0
|
||||
total_sentences = 0
|
||||
total_syllables = 0
|
||||
score = 0
|
||||
|
||||
words = text.split()
|
||||
total_words = len(words)
|
||||
total_sentences = count_sentences(text)
|
||||
total_syllables = count_syllables(words)
|
||||
|
||||
score = (206.835 - 1.015 * (total_words / total_sentences) - 84.6 * (total_syllables / total_words))
|
||||
score_result = output_results(score)
|
||||
|
||||
# print(words)
|
||||
print(total_words, 'слов')
|
||||
print(total_sentences, 'предложений')
|
||||
print(total_syllables, 'слогов')
|
||||
print(score, ' - удобочитаемость')
|
||||
print(score_result)
|
||||
# print(text)
|
||||
|
||||
compute_readability(ch1text.text)
|
33
ch6/ch1text.py
Normal file
33
ch6/ch1text.py
Normal file
@@ -0,0 +1,33 @@
|
||||
text = '''The first thing that stands between you and writing your first, real,
|
||||
piece of code, is learning the skill of breaking problems down into
|
||||
acheivable little actions that a computer can do for you. Of course,
|
||||
you and the computer will also need to be speaking a common language,
|
||||
but we'll get to that topic in just a bit.
|
||||
|
||||
Now breaking problems down into a number of steps may sound a new
|
||||
skill, but its actually something you do every day. Let’s look at an
|
||||
example, a simple one: say you wanted to break the activity of fishing
|
||||
down into a simple set of instructions that you could hand to a robot,
|
||||
who would do your fishing for you. Here’s our first attempt to do that,
|
||||
check it out:
|
||||
|
||||
You can think of these statements as a nice recipe for fishing. Like any
|
||||
recipe, this one provides a set of steps, that when followed in order,
|
||||
will produce some result or outcome in our case, hopefully, catching
|
||||
some fish.
|
||||
|
||||
Notice that most steps consists of simple instruction, like "cast line
|
||||
into pond", or "pull in the fish." But also notice that other
|
||||
instructions are a bit different because they depend on a condition,
|
||||
like “is the bobber above or below water?". Instructions might also
|
||||
direct the flow of the recipe, like "if you haven’t finished fishing,
|
||||
then cycle back to the beginning and put another worm on the hook."
|
||||
Or, how about a condition for stopping, as in “if you’re done” then go
|
||||
home.
|
||||
|
||||
You’re going to find these simple statements or instructions are the
|
||||
first key to coding, in fact every App or software program you’ve ever
|
||||
used has been nothing more than a (sometimes large) set of simple
|
||||
instructions to the computer that tell it what to do.'''
|
||||
|
||||
# print(text)
|
31
ch6/text.txt
Normal file
31
ch6/text.txt
Normal file
@@ -0,0 +1,31 @@
|
||||
The first thing that stands between you and writing your first, real,
|
||||
piece of code, is learning the skill of breaking problems down into
|
||||
acheivable little actions that a computer can do for you. Of course,
|
||||
you and the computer will also need to be speaking a common language,
|
||||
but we'll get to that topic in just a bit.
|
||||
|
||||
Now breaking problems down into a number of steps may sound a new
|
||||
skill, but its actually something you do every day. Let’s look at an
|
||||
example, a simple one: say you wanted to break the activity of fishing
|
||||
down into a simple set of instructions that you could hand to a robot,
|
||||
who would do your fishing for you. Here’s our first attempt to do that,
|
||||
check it out:
|
||||
|
||||
You can think of these statements as a nice recipe for fishing. Like any
|
||||
recipe, this one provides a set of steps, that when followed in order,
|
||||
will produce some result or outcome in our case, hopefully, catching
|
||||
some fish.
|
||||
|
||||
Notice that most steps consists of simple instruction, like "cast line
|
||||
into pond", or "pull in the fish." But also notice that other
|
||||
instructions are a bit different because they depend on a condition,
|
||||
like “is the bobber above or below water?". Instructions might also
|
||||
direct the flow of the recipe, like "if you haven’t finished fishing,
|
||||
then cycle back to the beginning and put another worm on the hook."
|
||||
Or, how about a condition for stopping, as in “if you’re done” then go
|
||||
home.
|
||||
|
||||
You’re going to find these simple statements or instructions are the
|
||||
first key to coding, in fact every App or software program you’ve ever
|
||||
used has been nothing more than a (sometimes large) set of simple
|
||||
instructions to the computer that tell it what to do.
|
Reference in New Issue
Block a user