def occurencecount():
# Ask the user to input a word
word = raw_input("Enter a word : ")
# Create a list of file which we will be looking into for matches
fileList = ['Text1.txt', 'Text2.txt', 'Text3.txt', 'Text4.txt']
# Open the files one by one, read them and find the occurance count inside each file
for filename in fileList:
# Openthe file
fp_text = codecs.open(filename, 'r', 'utf-8')
# Read all the words inside the file
words_text = word_tokenize(fp_text.read())
# Find the number of occurances of each word using built in method from NLTK
fd_text = FreqDist(words_text)
# Print out the number of occurances for that specific word
print("Number of occurences in " + filename + " : " + str(fd_text[word]))
评论列表
文章目录