-1

我想编写一个 Python 脚本来搜索所有 Excel 行并返回前 10 个最常见的句子。我已经为 txt 文件编写了 ngrams 的基础知识。

该文件包含 csv 文本,其中 dj 最好 4 次,gd 最好 3 次。

import nltk
import pandas as pd

file = open('dj.txt', encoding="utf8")
text= file.read()
length = [3]
ngrams_count = {}
for n in length:
    ngrams = tuple(nltk.ngrams(text.split(' '), n=n))
    ngrams_count.update({' '.join(i) : ngrams.count(i) for i in ngrams})
ngrams_count
df = pd.DataFrame(list(zip(ngrams_count, ngrams_count.values())), 
                  columns=['Ngramm', 'Count']).sort_values(['Count'], 
                                                           ascending=False)
df

输出 -

   Ngramm  Count
1                      is best,dj is      4
3                      is cool,gd is      2
21                     is best,gd is      2
25                best,dj is Best,dj      1
19                    not cool,dj is      1
20                cool,dj is best,gd      1
22                best,gd is cool,dj      1
23                     is cool,dj is      1
24                cool,dj is best,dj      1
0                      dj is best,dj      1
18                    is not cool,dj      1
27                Best,dj is best,dj      1
28                best,dj is best,dj      1
29                best,dj is best,gd      1
30                best,gd is cool,gd      1
31                cool,gd is COOL,gd      1
32                     is COOL,gd is      1
26                     is Best,dj is      1
17                    good,dj is not      1
16                    not good,dj is      1
15                    is not good,dj      1
14                  better,dj is not      1
13                   is better,dj is      1
12         good,sandeep is better,dj      1
11                is good,sandeep is      1
10    excellent,prem is good,sandeep      1
9               is excellent,prem is      1
8   superb,sandeep is excellent,prem      1
7               is superb,sandeep is      1
6        best,prem is superb,sandeep      1
5                    is best,prem is      1
4               cool,gd is best,prem      1
2                 best,dj is cool,gd      1
33                   COOL,gd is cool      1

所以首先,它显示 2 for gd 很酷,我不知道为什么?...然后我想对这个输出进行排序,以便它显示类似这样的内容

Ngramm  Count
dj is cool   4
gd is cool   3
....and so on....

然后我希望对excel文件逐行执行此操作。

我真的很陌生,有人能指出我正确的方向吗?

4

2 回答 2

0

如您所见,text.split(' ')不会在标点符号拆分,例如逗号。
对这个特定数据的快速而肮脏的修复(其中唯一出现的标点符号似乎是逗号,并且它们都没有以空格结尾)可能是写入。

text.replace(',',' ').split(' ')
>>> "a b,c".split(' ')
['a', 'b,c']                                 # <--- 2 elements
>>> "a b,c".replace(',',' ').split(' ')
['a', 'b', 'c']                              # <--- 3 elements

从长远来看,您可能想了解正则表达式,这可能是一个痛苦的经历,但对于这种情况,这很容易:

>>> import re
>>> re.split("[ ,]+","a b,c")
['a', 'b', 'c']
于 2018-11-19T13:25:13.723 回答
0

由于这是一个 csv 文件,请帮自己一个忙,先解析 csv!然后获取内容并以您想要的任何方式处理它们。但是你的数据似乎每个单元格包含一个“句子”,所以如果我们的目标是找到最常见的句子,你为什么要在这个任务中使用标记化和 ngram?

import csv
from collections import Counter
with open('dj.txt', encoding="utf8") as handle:
    sentcounts = Counter(cell for row in csv.reader(handle) for cell in row)

print("Frequency  Sentence")
for sent, freq in sentcounts.most_common(5):
    print("%9d"%freq, sent)

如果您确实想要标记,您可以split()在这个简单的情况下使用,但对于更真实的文本使用nltk.word_tokenize(),它知道所有关于标点符号。

于 2018-11-19T20:32:51.383 回答