2012-10-26 2 views
3

빠른 질문은 혼란 스럽습니다. NLTK가 설치되어 있고 정상적으로 작동합니다. 그러나 나는 코퍼스의 bigrams를 얻으려고 노력하고 기본적으로 bigrams (corpus)를 사용하고 싶습니다.하지만 "bigrams를 nltk에서 가져올 때"bigrams가 정의되지 않았다고 말합니다.NLTK 라이브러리에서 Bigram을 가져올 수 없습니다.

트라이 그램과 동일합니다. 내가 놓친 게 있니? 또한, 어떻게하면 수동으로 코퍼스에서 바이 그램을 얻을 수 있습니까?

나는 bigrams trigrams와 quads의 빈도를 계산하기를 원하지만 정확하게 이것에 대해 어떻게해야하는지 확실히 모릅니다.

처음에는 "<s>""</s>"으로 토큰 화 된 토큰이 있고 적절하게 끝납니다. 지금까지 여기에 프로그램 :

#!/usr/bin/env python 
import re 
import nltk 
import nltk.corpus as corpus 
import tokenize 
from nltk.corpus import brown 

def alter_list(row): 
    if row[-1] == '.': 
     row[-1] = '</s>' 
    else: 
     row.append('</s>') 
    return ['<s>'] + row 

news = corpus.brown.sents(categories = 'editorial') 
print len(news),'\n' 

x = len(news) 
for row in news[:x]: 
    print(alter_list(row)) 

답변

6

나는 VIRTUALENV이 테스트 및 작동 :

In [20]: from nltk import bigrams 

In [21]: bigrams('This is a test') 
Out[21]: 
[('T', 'h'), 
('h', 'i'), 
('i', 's'), 
('s', ' '), 
(' ', 'i'), 
('i', 's'), 
('s', ' '), 
(' ', 'a'), 
('a', ' '), 
(' ', 't'), 
('t', 'e'), 
('e', 's'), 
('s', 't')] 

유일한 오류 당신이 얻고인가요? 두 번째 질문에 대한 같은 방식으로

:

from collections import Counter 
In [44]: b = bigrams('This is a test') 

In [45]: Counter(b) 
Out[45]: Counter({('i', 's'): 2, ('s', ' '): 2, ('a', ' '): 1, (' ', 't'): 1, ('e', 's'): 1, ('h', 'i'): 1, ('t', 'e'): 1, ('T', 'h'): 1, (' ', 'i'): 1, (' ', 'a'): 1, ('s', 't'): 1}) 

단어 :

In [49]: b = bigrams("This is a test".split(' ')) 

In [50]: b 
Out[50]: [('This', 'is'), ('is', 'a'), ('a', 'test')] 

In [51]: Counter(b) 
Out[51]: Counter({('is', 'a'): 1, ('a', 'test'): 1, ('This', 'is'): 1}) 

단어로이 분할은 분명히 매우 피상적이지만 충분하다 응용 프로그램에 따라입니다. 분명히 훨씬 더 정교한 nltk의 토큰 화를 사용할 수 있습니다. 불필요하게 큰 이었기 때문에

In [56]: d = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." 

In [56]: from nltk import trigrams 
In [57]: tri = trigrams(d.split(' ')) 

In [60]: counter = Counter(tri) 

In [61]: import random 

In [62]: random.sample(counter, 5) 
Out[62]: 
[('Ipsum', 'has', 'been'), 
('industry.', 'Lorem', 'Ipsum'), 
('Ipsum', 'passages,', 'and'), 
('was', 'popularised', 'in'), 
('galley', 'of', 'type')] 

내가 출력을 손질,하지만 당신은 아이디어를 얻을 : 최종 목표를 달성하기 위해

, 당신은 그런 식으로 뭔가를 할 수 있습니다.

+0

답장을 보내 주셔서 감사합니다. 나는 무엇을했는지는 모르지만 지금은 수입 중입니다. 음 ... 문제는 이제 각 단어를 기준으로 계산할 수 있도록 글자 단위가 아닌 단어 당 bigram이 필요하다는 것입니다. 그럴 수 있니? 또한 필자가 필요로하는 바이그램 (및 트라이와 쿼드)을 기반으로 한 ngram의 계산을하기 위해서도 원래의 코퍼스와 유사한 ngram을 기반으로 무작위 텍스트를 생성해야합니다. – user1378618

+0

업데이트 됨 ... 내 대답보기. –

+0

지금 봅니다. Ok 불행히도 위에서 "뉴스"는 프로그램에서 사용되는 .split과 함께 사용할 수있는 유형이 아닙니다. 오류가 발생했습니다 : AttributeError : 'ConcatenatedCorpusView'객체에 'split'속성이 없습니다. 어떻게 주석을 사용하여 변경된 버전의 뉴스를 사용할 수 있습니까? 또한 bigrams, tri 등으로 분리하는 데 사용할 수 있습니까? 편집 : 좋아, 여기에 1 초 보자 – user1378618

관련 문제