在我的程序中,我找到n个克,然后打印出一个数据集中有多少个。
https://en.wikipedia.org/wiki/N-gram
对于那些不知道n-gram是什么的人。
这是我的代码:
from collections import defaultdict
import sys
from string import punctuation
def tokenize(text, ngrams=1):
tokens = text.split()
return [tuple(tokens[i:i+ngrams]) for i in range(len(tokens)-ngrams+1)]
line = ""
for i in sys.stdin:
stripped = i.strip(punctuation)
line += stripped.lower()
for n in range(1, 10):
a = tokenize(line, n)
d = defaultdict(int)
for i in a:
d[i] += 1
result = max(d.items(), key = lambda x: x[1])
if(result[1] >= 3):
s = ' '.join(result[0])
print('{:<6} {:<0} {:<0} {:<10}'.format(str(result[1]), str(n) + "-grams ", "|", s))
下面是我的程序使用数据集输出的示例:
10 1-grams | and
3 2-grams | balloonman whistles
3 3-grams | balloonman whistles far
3 4-grams | balloonman whistles far and
3 5-grams | balloonman whistles far and wee
下面是我应该得到的(忽略格式差异):
10 1-grams | and
3 2-grams | balloonman whistles
3 2-grams | whistles far
3 2-grams | far and
3 2-grams | and wee
3 3-grams | balloonman whistles far
3 3-grams | whistles far and
3 3-grams | far and wee
3 4-grams | balloonman whistles far and
3 4-grams | whistles far and wee
3 5-grams | balloonman whistles far and wee
问题似乎是,当我在默认dict中找到最大值时,我只得到其中的一个,例如3个3克,但我想得到所有3个3克。有什么想法吗?提前谢谢你