본문 바로가기
파이썬

파이썬 Finding Proper Nouns using NLTK WordNet

by º기록 2021. 1. 12.
반응형

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

 

 

반응형

댓글