2017-11-27 9 views
0

그래서 나는 (POS는 정확하게는 태그)과 같이 목록에서 단어의 무리에 태그를 시도하고 있었다 그것은 [['hello'],['world']] (각 목록은 하나 개의 단어를 포함하는 목록 일명 목록)처럼하지만 난 시도하고 그것을 실행할 때 내가 얻을 :NLTK에서 pos_tag를 사용하는 방법은 무엇입니까? <code>lw</code> 단어의 목록 (정말 길다 또는 내가 그것을 게시하지만 것입니다</p> <pre><code>pos = [nltk.pos_tag(i,tagset='universal') for i in lw] </code></pre> <p>:

Traceback (most recent call last): 
    File "<pyshell#183>", line 1, in <module> 
    pos = [nltk.pos_tag(i,tagset='universal') for i in lw] 
    File "<pyshell#183>", line 1, in <listcomp> 
    pos = [nltk.pos_tag(i,tagset='universal') for i in lw] 
    File "C:\Users\my system\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tag\__init__.py", line 134, in pos_tag 
    return _pos_tag(tokens, tagset, tagger) 
    File "C:\Users\my system\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tag\__init__.py", line 102, in _pos_tag 
    tagged_tokens = tagger.tag(tokens) 
    File "C:\Users\my system\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tag\perceptron.py", line 152, in tag 
    context = self.START + [self.normalize(w) for w in tokens] + self.END 
    File "C:\Users\my system\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tag\perceptron.py", line 152, in <listcomp> 
    context = self.START + [self.normalize(w) for w in tokens] + self.END 
    File "C:\Users\my system\AppData\Local\Programs\Python\Python35\lib\site-packages\nltk\tag\perceptron.py", line 240, in normalize 
    elif word[0].isdigit(): 
IndexError: string index out of range 

누군가가 말해 줄 수 왜 내가이 오류가 어떻게 어떻게 그것을 해결하기 위해? 많은 감사합니다.

답변

0

먼저, 사람이 읽을 수있는 변수 이름을 사용하십시오.) =

다음은 pos_tag 입력은 문자열 목록입니다. 당신이 단락의 문장이

>>> from nltk import pos_tag, word_tokenize 
>>> a_sentence = 'hello world' 
>>> word_tokenize(a_sentence) 
['hello', 'world'] 
>>> pos_tag(word_tokenize(a_sentence)) 
[('hello', 'NN'), ('world', 'NN')] 

>>> two_sentences = ['hello world', 'good morning'] 
>>> [word_tokenize(sent) for sent in two_sentences] 
[['hello', 'world'], ['good', 'morning']] 
>>> [pos_tag(word_tokenize(sent)) for sent in two_sentences] 
[[('hello', 'NN'), ('world', 'NN')], [('good', 'JJ'), ('morning', 'NN')]] 

그리고, 당신은을 분할 sent_tokenize를 사용할 수 있습니다 그래서 당신은 원시 문자열로 입력이있는 경우 word_tokenizepos_tag하기 전에, 당신이 사용할 수있는, 또한

>>> from nltk import pos_tag 
>>> sentences = [ ['hello', 'world'], ['good', 'morning'] ] 
>>> [pos_tag(sent) for sent in sentences] 
[[('hello', 'NN'), ('world', 'NN')], [('good', 'JJ'), ('morning', 'NN')]] 

있어 문장. 답에 대한 How to do POS tagging using the NLTK POS tagger in Python?

+0

감사합니다, 그리고 여기 단지 문제는 이런 일이되었다 _why_ 나는 또한 궁금 점이다, 작동 :

>>> from nltk import sent_tokenize, word_tokenize, pos_tag >>> text = "Hello world. Good morning." >>> sent_tokenize(text) ['Hello world.', 'Good morning.'] >>> [word_tokenize(sent) for sent in sent_tokenize(text)] [['Hello', 'world', '.'], ['Good', 'morning', '.']] >>> [pos_tag(word_tokenize(sent)) for sent in sent_tokenize(text)] [[('Hello', 'NNP'), ('world', 'NN'), ('.', '.')], [('Good', 'JJ'), ('morning', 'NN'), ('.', '.')]] 

도 참조하십시오. 그럼에도 불구하고 귀하의 답변을 주셔서 감사합니다. – EighteenthVariable