문맥에 질문을 넣으려면 몇 가지 (신경) 언어 모델을 테스트하고 비교하고 싶습니다. 데이터 준비보다는 모델에 집중하기 위해 nltk에서 Brown corpus를 사용하고 nltk와 함께 제공된 Ngrams 모델을 기준으로 선택했습니다 (다른 LM과 비교하기 위해).Ngram 모델과 NLTK에서의 괴로움
그래서 내 첫 번째 질문은 의심스러운 nltk의 Ngram 모델의 동작에 관한 것입니다. 코드는 내가 여기를 붙여 오히려 짧기 때문에 : N- 그램 모델링은 매우 4.6의 혼란으로
... build
... train
len(corpus) = 1161192, len(vocabulary) = 13817, len(train) = 1103132, len(test) = 58060
perplexity(test) = 4.60298447026
보인다 :
import nltk
print "... build"
brown = nltk.corpus.brown
corpus = [word.lower() for word in brown.words()]
# Train on 95% f the corpus and test on the rest
spl = 95*len(corpus)/100
train = corpus[:spl]
test = corpus[spl:]
# Remove rare words from the corpus
fdist = nltk.FreqDist(w for w in train)
vocabulary = set(map(lambda x: x[0], filter(lambda x: x[1] >= 5, fdist.iteritems())))
train = map(lambda x: x if x in vocabulary else "*unknown*", train)
test = map(lambda x: x if x in vocabulary else "*unknown*", test)
print "... train"
from nltk.model import NgramModel
from nltk.probability import LidstoneProbDist
estimator = lambda fdist, bins: LidstoneProbDist(fdist, 0.2)
lm = NgramModel(5, train, estimator=estimator)
print "len(corpus) = %s, len(vocabulary) = %s, len(train) = %s, len(test) = %s" % (len(corpus), len(vocabulary), len(train), len(test))
print "perplexity(test) =", lm.perplexity(test)
내가 매우 의심스러운 발견하면 나는 다음과 같은 결과를 얻을 수 있다는 점이다가 그 코퍼스 잘했다. 제 해석이 맞다면 모델은 대략 5 번의 시도로 정확한 단어를 추측 할 수 있어야합니다 (13817 가지 가능성이 있지만 ...). 이 혼란의 가치에 대한 경험을 공유 할 수 있다면 (나는 정말로 그것을 믿지 않는다)? net에서 nltk의 ngram 모델에 대한 불만을 찾지 못했습니다 (하지만 잘못했을 수도 있습니다). Ngram 모델과 컴퓨팅 난독증에 대한 NLTK의 훌륭한 대안을 알고 계십니까?
감사합니다.
NLTK에서 ngram의 구현이 잘못되었다고 생각됩니다. SRILM (http://www.speech.sri.com/projects/srilm/)은 ~ 150의 혼란을줍니다 (훨씬 더 믿을만한). 그래도 NLTK의 인기를 얻은 나는 아무도이 사실을 처음부터 경험하지 못했다는 사실에 놀랐습니다. – zermelozf