2017-02-21 29 views
0

Я пытаюсь найти k наиболее распространенных n-граммов из большого корпуса. Я видел множество мест, предлагающих наивный подход - просто просматривая весь корпус и сохраняя словарь количества всех n-граммов. Есть лучший способ сделать это?Есть ли более эффективный способ найти наиболее распространенные n-граммы?

+0

Возможный дубликат http://cs.stackexchange.com/questions/8972/optimal-algorithm-for-finding-all-ngrams-from-a-pre-defined-set-in-a-text – pltrdy

+0

Что вы сравниваете? Насколько велик корпус? Я думаю, вы можете легко подсчитать количество nграмм на C++ довольно быстро для огромного корпуса и даже в Python довольно быстро =) – alvas

+0

Вы имеете в виду персонаж ngrams или word ngrams? – alvas

ответ

0

В Python, используя NLTK:

$ wget http://norvig.com/big.txt 
$ python 
>>> from collections import Counter 
>>> from nltk import ngrams 
>>> bigtxt = open('big.txt').read() 
>>> ngram_counts = Counter(ngrams(bigtxt.split(), 2)) 
>>> ngram_counts.most_common(10) 
[(('of', 'the'), 12422), (('in', 'the'), 5741), (('to', 'the'), 4333), (('and', 'the'), 3065), (('on', 'the'), 2214), (('at', 'the'), 1915), (('by', 'the'), 1863), (('from', 'the'), 1754), (('of', 'a'), 1700), (('with', 'the'), 1656)] 

В Python родной (см Fast/Optimize N-gram implementations in python):

>>> def ngrams(text, n=2): 
...  return zip(*[text[i:] for i in range(n)]) 
>>> ngram_counts = Counter(ngrams(bigtxt.split(), 2)) 
>>> ngram_counts.most_common(10) 
    [(('of', 'the'), 12422), (('in', 'the'), 5741), (('to', 'the'), 4333), (('and', 'the'), 3065), (('on', 'the'), 2214), (('at', 'the'), 1915), (('by', 'the'), 1863), (('from', 'the'), 1754), (('of', 'a'), 1700), (('with', 'the'), 1656)] 

В Julia см Generate ngrams with Julia

import StatsBase: countmap 
import Iterators: partition 
bigtxt = readstring(open("big.txt")) 
ngram_counts = countmap(collect(partition(split(bigtxt), 2, 1))) 

Грубая времени:

$ time python ngram-test.py # With NLTK. 

real 0m3.166s 
user 0m2.274s 
sys 0m0.528s 

$ time python ngram-native-test.py 

real 0m1.521s 
user 0m1.317s 
sys 0m0.145s 

$ time julia ngram-test.jl 

real 0m3.573s 
user 0m3.188s 
sys 0m0.306s