我正在尝试创建一个程序,它采用给定路径中的所有文本文件并将所有字符串保存在一个列表中:
import os
import collections
vocab = set()
path = 'a\\path\\'
listing = os.listdir(path)
unwanted_chars = ".,-_/()*"
vocab={}
for file in listing:
#print('Current file : ', file)
pos_review = open(path+file, "r", encoding ='utf8')
words = pos_review.read().split()
#print(type(words))
vocab.update(words)
pos_review.close()
print(vocab)
pos_dict = dict.fromkeys(vocab,0)
print(pos_dict)
输入
file1.txt: A quick brown fox.
file2.txt: a quick boy ran.
file3.txt: fox ran away.
输出
A : 2
quick : 2
brown : 1
fox : 2
boy : 1
ran : 2
away : 1
到目前为止,我能够制作这些字符串的字典。但现在不知道如何在所有文本文件中组合键、值对字符串及其频率。
小唯快跑啊
幕布斯7119047
蛊毒传说
相关分类