[英]Find the most common sentences/phrases among millions of documents using Python
[英]Finding common phrases using python
我正在尝试获取一个CSV文件,并使用Python 2.7查找常用短语和计数。 目前,我只能得到单个单词及其数量,但是我需要常用短语。
到目前为止,这是我的代码:
import csv
from sys import argv
from collections import defaultdict
from collections import Counter
script, filename = argv
data = defaultdict(list)
with open (filename, 'rb') as f:
reader = csv.reader(f)
text_file = open("output.txt", "w")
next(reader, None)
for row in reader:
data[row[2]].append(row[3])
text_file.write("%r" % data)
text_file.close()
print(data)
c = Counter(defaultdict)
print c.most_common(10)
首先,考虑使用自然语言标记器的短语。 即使是最简单的语言,在句子的定义上也有很多微妙之处,例如,尝试用正则表达式解析短语可能会使您发疯。
从那里开始,使用您的方法来计算“短语”(而不是单词)的频率,就像您已经在做的那样,考虑到“常用短语”意味着出现多次。 如果这不是“常用短语”的意思,那么您应该在问题中进一步澄清。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.