반응형
NLTK WordNet을 사용하여 고유 명사를 찾을 수있는 방법이 있습니까? 즉, nltk Wordnet을 사용하여 소유 명사에 태그를 지정할 수 있습니까?
해결 방법
고유 명사를 찾기 위해 WordNet이 필요하다고 생각하지 않습니다. Part-Of-Speech tagger pos_tag
를 사용하는 것이 좋습니다.
고유 명사를 찾으려면 NNP
태그를 찾으세요.
from nltk.tag import pos_tag
sentence = "Michael Jackson likes to eat at McDonalds"
tagged_sent = pos_tag(sentence.split())
# [('Michael', 'NNP'), ('Jackson', 'NNP'), ('likes', 'VBZ'), ('to', 'TO'), ('eat', 'VB'), ('at', 'IN'), ('McDonalds', 'NNP')]
propernouns = [word for word,pos in tagged_sent if pos == 'NNP']
# ['Michael','Jackson', 'McDonalds']
Michael
및 Jackson
이 2 개의 토큰으로 분할되어 있기 때문에 만족스럽지 않을 수 있으며, Name Entity tagger와 같은 더 복잡한 것이 필요할 수 있습니다.
소유 명사를 찾으려면 str.endswith ( " 's") 또는 str.endswith ( "s'")를 찾으세요.
from nltk.tag import pos_tag
sentence = "Michael Jackson took Daniel Jackson's hamburger and Agnes' fries"
tagged_sent = pos_tag(sentence.split())
# [('Michael', 'NNP'), ('Jackson', 'NNP'), ('took', 'VBD'), ('Daniel', 'NNP'), ("Jackson's", 'NNP'), ('hamburger', 'NN'), ('and', 'CC'), ("Agnes'", 'NNP'), ('fries', 'NNS')]
possessives = [word for word in sentence if word.endswith("'s") or word.endswith("s'")]
# ["Jackson's", "Agnes'"]
또는 NLTK ne_chunk
를 사용할 수 있지만 문장에서 어떤 종류의 고유 명사를 얻었는지 염려하지 않는 한 다른 작업을 많이 수행하지 않는 것 같습니다.
>>> from nltk.tree import Tree; from nltk.chunk import ne_chunk
>>> [chunk for chunk in ne_chunk(tagged_sent) if isinstance(chunk, Tree)]
[Tree('PERSON', [('Michael', 'NNP')]), Tree('PERSON', [('Jackson', 'NNP')]), Tree('PERSON', [('Daniel', 'NNP')])]
>>> [i[0] for i in list(chain(*[chunk.leaves() for chunk in ne_chunk(tagged_sent) if isinstance(chunk, Tree)]))]
['Michael', 'Jackson', 'Daniel']
ne_chunk
를 사용하는 것은 약간 장황하고 소유격을 얻지 못합니다.
참조 페이지 https://stackoverflow.com/questions/17669952
반응형
'파이썬' 카테고리의 다른 글
파이썬에서 wav 파일을 재생하는 방법? (0) | 2021.01.12 |
---|---|
파이썬 Python Socket Receive Large Amount of Data (0) | 2021.01.12 |
파이썬 Numpy Vector (N,1) dimension -> (N,) dimension conversion (0) | 2021.01.12 |
파이썬에서 if / elif 문에 대한 대안은 무엇입니까? (0) | 2021.01.12 |
파이썬 Python에서 한 디렉터리 위로 이동 (0) | 2021.01.12 |
댓글