Python 计算非冠词
尝试获取defcountnonarticlewords以计算mobydick文本文件中非文章词的数量。我试图不计算的单词有“a”、“an”和“The”。关于如何修改def countNonParticleWords以输出示例1的任何提示。目前正在输出示例2 示例#1 示例2 到目前为止,我所拥有的:Python 计算非冠词,python,python-3.x,Python,Python 3.x,尝试获取defcountnonarticlewords以计算mobydick文本文件中非文章词的数量。我试图不计算的单词有“a”、“an”和“The”。关于如何修改def countNonParticleWords以输出示例1的任何提示。目前正在输出示例2 示例#1 示例2 到目前为止,我所拥有的: def getTextFile(): filename=input("Enter the test file name: ") textFile=open(filen
def getTextFile():
filename=input("Enter the test file name: ")
textFile=open(filename, 'r')
return filename,textFile
def outputcountresults(filename, linecount, charcount, wordcount, nonarticlewordcount):
print("*****{}*****".format(filename))
print("Total Lines: {}".format(linecount))
print("Total Chars: {}".format(charcount))
print("Total Words: {}".format(wordcount))
print("Total Non-Article Words: {}".format(nonarticlewordcount))
def countcharacters(line):
charcount=0
for c in line:
if not c.isspace():
charcount= charcount +1
return charcount
def countnonarticlewords(line):
nonarticlewords= line.split()
nonarticlewords=0
for nonarticle in line:
#nonarticlewords= line.split()
if not 'a' or 'an' or 'the':
nonarticlewords= nonarticlewords +1
return len(nonarticle)
def countwords(line):
words= line.split()
return len(words)
def countdocstats(docFile):
linecount=0
totalcharacters=0
totalwords=0
totalnonarticlewords=0
for line in docFile:
linecount= linecount + 1
totalwords= totalwords + countwords(line)
totalcharacters=totalcharacters+countcharacters(line)
totalnonarticlewords= totalnonarticlewords + countnonarticlewords(line)
return linecount, totalcharacters, totalwords, totalnonarticlewords
def main():
filename, textFile=getTextFile()
linecount, totalcharacters, totalwords, totalnonarticlewords= countdocstats(textFile)
outputcountresults(filename,linecount,totalcharacters,totalwords,totalnonarticlewords)
main()
不知何故,每次人们都会犯同样的错误。不,说真的,你认为
如果不是'a'或'an'或'the'
怎么办?如果在{'a','an','the'}中没有任何一个条目,你会犯几个错误。对于初学者,您可以执行nonarticlewords=line.split()
,然后紧接着执行nonarticlewords=0
,从而使前一行变得毫无意义。然后直接在第行上迭代,对第行中的非项目使用,,但对第行中的非项目使用进行迭代。split()
。最后,检查:如果不是“a”或“an”或“the”
没有做你认为它正在做的事情
Enter the test file name: mobydick.txt
*****mobydick.txt*****
Total Lines: 15604
Total Chars: 512293
Total Words: 115314
**Total Non-Article Words: 15604**
def getTextFile():
filename=input("Enter the test file name: ")
textFile=open(filename, 'r')
return filename,textFile
def outputcountresults(filename, linecount, charcount, wordcount, nonarticlewordcount):
print("*****{}*****".format(filename))
print("Total Lines: {}".format(linecount))
print("Total Chars: {}".format(charcount))
print("Total Words: {}".format(wordcount))
print("Total Non-Article Words: {}".format(nonarticlewordcount))
def countcharacters(line):
charcount=0
for c in line:
if not c.isspace():
charcount= charcount +1
return charcount
def countnonarticlewords(line):
nonarticlewords= line.split()
nonarticlewords=0
for nonarticle in line:
#nonarticlewords= line.split()
if not 'a' or 'an' or 'the':
nonarticlewords= nonarticlewords +1
return len(nonarticle)
def countwords(line):
words= line.split()
return len(words)
def countdocstats(docFile):
linecount=0
totalcharacters=0
totalwords=0
totalnonarticlewords=0
for line in docFile:
linecount= linecount + 1
totalwords= totalwords + countwords(line)
totalcharacters=totalcharacters+countcharacters(line)
totalnonarticlewords= totalnonarticlewords + countnonarticlewords(line)
return linecount, totalcharacters, totalwords, totalnonarticlewords
def main():
filename, textFile=getTextFile()
linecount, totalcharacters, totalwords, totalnonarticlewords= countdocstats(textFile)
outputcountresults(filename,linecount,totalcharacters,totalwords,totalnonarticlewords)
main()