시퀀스를 기반으로 한 문장에서 찾고 싶은 단어 목록이 두 개 있습니다. "정규식"사용이 가능한지 확인하고 싶은지 아니면 if 조건으로 문장을 확인해야하나요?
n_ali = set(['ali','aliasghar'])
n_leyla = set(['leyla','lili',leila])
positive_adj = set(['good','nice','handsome'])
negative_adj = set(['bad','hate','lousy'])
Sentence = "aliasghar is nice man. ali is handsome man of my life. lili has so many bad attitude who is next to my friend. "
다음과 같은 패턴을 찾고 싶습니다.
VS2015에서 파이썬 3.5를 사용하고 있으며 NLTK에서 처음입니다. 한 단어를 확인하기 위해 "정규식"을 만드는 방법을 알고 있지만 유사한 이름 목록에 가장 적합한 방법이 무엇인지 잘 모르겠습니다. 친절하게 저를 도와주고이 접근 방식을 구현하는 가장 좋은 방법을 제안하십시오.
불용어 제거를 고려해야합니다.
import nltk
from nltk.corpus import stopwords
>>> words = [word for word in nltk.word_tokenize(sentence) if word not in stopwords.words('english')]
>>> words
['aliasghar', 'nice', 'man', '.', 'ali', 'handsome', 'man', 'life', '.', 'lili', 'many', 'bad', 'attitude', 'next', 'friend', '.']
좋아, 이제 원하는 데이터를 얻었습니다 (대부분). 간단한 루핑을 사용하여 결과를 for ali
및 leila
개별적 으로 쌍으로 저장해 봅시다 .
>>> ali_adj = []
>>> leila_adj = []
>>> for i, word in enumerate(words[:-1]):
... if word in n_ali and (words[i+1] in positive_adj.union(negative_adj)):
... ali_adj.append((word, words[i+1]))
... if word in n_leyla and (words[i+1] in positive_adj.union(negative_adj)):
... leila_adj.append((word, words[i+1]))
...
>>>
>>> ali_adj
[('aliasghar', 'nice'), ('ali', 'handsome')]
>>> leila_adj
[]
leila
"many"는 불용어가 아니기 때문에 설명 할 형용사를 찾을 수 없습니다 . 이 유형의 문장을 수동으로 정리해야 할 수도 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다