/ / Wie benutze ich pos_tag in NLTK? - python, nlp, nltk, pos-tagging

Wie benutze ich pos_tag in NLTK? - python, nlp, nltk, pos-tagging

Also habe ich versucht, eine Reihe von Wörtern in einer Liste zu markieren (POS-Markierung, um genau zu sein):

pos = [nltk.pos_tag(i,tagset="universal") for i in lw]

woher lw ist eine Liste von Wörtern (es ist wirklich lang oder ich hätte es gepostet, aber es ist wie [["hello"],["world"]] (aka eine Liste von Listen, die jeweils ein Wort enthalten)

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:Usersmy systemAppDataLocalProgramsPythonPython35libsite-packagesnltktag__init__.py", line 134, in pos_tag
return _pos_tag(tokens, tagset, tagger)
File "C:Usersmy systemAppDataLocalProgramsPythonPython35libsite-packagesnltktag__init__.py", line 102, in _pos_tag
tagged_tokens = tagger.tag(tokens)
File "C:Usersmy systemAppDataLocalProgramsPythonPython35libsite-packagesnltktagperceptron.py", line 152, in tag
context = self.START + [self.normalize(w) for w in tokens] + self.END
File "C:Usersmy systemAppDataLocalProgramsPythonPython35libsite-packagesnltktagperceptron.py", line 152, in <listcomp>
context = self.START + [self.normalize(w) for w in tokens] + self.END
File "C:Usersmy systemAppDataLocalProgramsPythonPython35libsite-packagesnltktagperceptron.py", line 240, in normalize
elif word[0].isdigit():
IndexError: string index out of range

Kann mir jemand sagen, warum und wie ich diesen Fehler bekomme und wie ich ihn beheben kann? Danke vielmals.

Antworten:

1 für die Antwort № 1

Verwenden Sie zunächst menschenlesbare Variablennamen, es hilft =)

Nächster, pos_tag Eingabe ist eine Liste von Zeichenfolgen. So ist es

>>> 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")]]

Wenn Sie die Eingabe als unformatierte Zeichenfolgen haben, können Sie auch verwenden word_tokenize Vor 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")]]

Und Sie haben die Sätze in einem Absatz, die Sie verwenden können sent_tokenize den Satz aufteilen.

>>> 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"), (".", ".")]]

Siehe auch: Wie kann ich das POS-Tagging mit dem NLTK-POS-Tagger in Python durchführen?