나는 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')]
내가 출력을 손질,하지만 당신은 아이디어를 얻을 : 최종 목표를 달성하기 위해
, 당신은 그런 식으로 뭔가를 할 수 있습니다.
답장을 보내 주셔서 감사합니다. 나는 무엇을했는지는 모르지만 지금은 수입 중입니다. 음 ... 문제는 이제 각 단어를 기준으로 계산할 수 있도록 글자 단위가 아닌 단어 당 bigram이 필요하다는 것입니다. 그럴 수 있니? 또한 필자가 필요로하는 바이그램 (및 트라이와 쿼드)을 기반으로 한 ngram의 계산을하기 위해서도 원래의 코퍼스와 유사한 ngram을 기반으로 무작위 텍스트를 생성해야합니다. – user1378618
업데이트 됨 ... 내 대답보기. –
지금 봅니다. Ok 불행히도 위에서 "뉴스"는 프로그램에서 사용되는 .split과 함께 사용할 수있는 유형이 아닙니다. 오류가 발생했습니다 : AttributeError : 'ConcatenatedCorpusView'객체에 'split'속성이 없습니다. 어떻게 주석을 사용하여 변경된 버전의 뉴스를 사용할 수 있습니까? 또한 bigrams, tri 등으로 분리하는 데 사용할 수 있습니까? 편집 : 좋아, 여기에 1 초 보자 – user1378618