[英]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.