我一直在寻找一种方法来做到这一点,我只找到了用字符串做的方法我想知道它是否可以用列表来完成。
#----------------------------
#flesch reading score program
#----------------------------
#----getting file to read----
myfile = open('project2test.txt')
txt = myfile.read()
words = txt.split()
txt = list(txt)
txt.append(' ')
#----global variables----
numWords, numberOfWords, numSentences, numSyllables = 0,0,0,0
punctuation = '.?;:!'
extendedPunctuation = '.?;:!,/<>]}\|)(*&^%$#@'
abc = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
syllables = 'aeiouAEIOU'
numbers = '1234567890'
#----counting functions----
def sentenceEnd(i,numSentences):
if txt[i] in punctuation:
numSentences += 1
return numSentences
def wordEnd(i,numWords):
if txt[i] in abc:
if txt[i+1] == ' ' or txt[i+1] == '\n':
numWords += 1
if txt[i+1] in extendedPunctuation:
numWords += 1
if txt[i+1] in numbers:
numWords += 1
return numWords
def syllableEnd(i,numSyllables):
if txt[i] in syllables:
if txt[i+1] not in syllables:
numSyllables += 1
return numSyllables
#----main loop----
for i in range(len(txt)):
numWords = wordEnd(i, numWords)
numSentences = sentenceEnd(i,numSentences)
numSyllables = syllableEnd(i,numSyllables)
for word in words:
numberOfWords += 1
flesh = 206.835 - 1.015 * (numberOfWords/numSentences) - 84.6 * ((numSyllables)/numberOfWords)
print("The flesch redability of your document is: ", flesh)
print(numSyllables, ' ', numSentences, ' ' , numWords, ' ', numberOfWords)
现在我必须定义一切。 我低声想知道如果不是坐着puntuation =,abs = ... list [i] .ascii == true
答案 0 :(得分:1)
punctuations = [' ',',']
original_string = "Hello,World! How Clean is Python!"
words = []
words.append(original_string)
for x in punctuations:
temp = []
for word in words:
temp += word.split(x)
words = temp
这应该有用。