Text files in the following format
Copy codeThe Code is as follows:
/"/Please /! /"/"/Please /! /"/Two/swordsmen/respective/reverse//,/right hand/Grip/hilt /,
/Left hand/Attach/right hand/,/bow /. /Two/person/body/not yet/station/straight /,
/Suddenly/room/white flashing/animation/,/followed by/else/1/sound /,
/Pair of swords/intersection/,/Two/person/each/step back /.
/Side/audience/people/both/Yes/"/audience/"/A/call /. /Tsing Yi/swordsman/connection/split/3/sword/
Perform Word Frequency Statistics in this section. The result is in the form of word-number. For example, please 2 and put the result in a txt file.
In this case, words or words are used as the dictionary key to cyclically judge whether the key exists and no new one is added. If yes, add 1 to the value corresponding to the key.
Copy codeThe Code is as follows:
# Coding: UTF-8
Word_lst = []
Word_dict = {}
With open ("Chinese .txt", "r") as f1, open ("Word Count .txt", 'w') as f2:
For line in f1:
Word_lst.append (line. split ('/'))
For item in word_lst:
For item2 in item:
If item2.strip () not in ",!. "":
If item2 not in word_dict:
Word_dict [item2] = 1
Else:
Word_dict [item2] + = 1
For key in word_dict:
Print key, word_dict [key]
F2.write (key + ''+ str (word_dict [key])