SpaCy 매처에서 부정을 이해하는 데 문제가 있습니다. 나는이 code를 시도 :
import spacy
from spacy.matcher import Matcher
import json
nlp= spacy.load('en_core_web_sm')
#from spacy.tokenizer import Tokenizer
matcher= Matcher(nlp.vocab)
Sentence= "The cat is black"
negative_sentence= "The cat is not black"
test_pattern= '''
[
[
{
"TEXT": "cat"
},
{
"LEMMA": "be"
},
{
"LOWER": "not",
"OP": "!"
},
{
"LOWER": "black"
}
]
]
'''
db= json.loads(test_pattern)
matcher.add("TEST_PATTERNS", db)
'''*********************Validate matcher on positive sentence******************'''
doc= nlp(Sentence, matcher)
matches= matcher(doc)
if matches != []:
print('Positive sentence identified')
else:
print('Nothing found for positive sentence')
'''*********************Validate matcher on negative sentence******************'''
doc= nlp(negative_sentence, matcher)
matches= matcher(doc)
if matches != []:
print('Negative sentence identified')
else:
print('Nothing found for negative sentence')
결과는 다음과 같습니다.
- 긍정적인 문장을 찾을 수 없음
- 부정문에 대한 검색 결과가 없습니다.
"고양이는 검다"라는 문장이 일치할 것으로 예상합니다. 게다가 교체할 때 ! 다른 기호("*", "?" 또는 "+")를 사용하면 예상대로 작동합니다.
import spacy
from spacy.matcher import Matcher
import json
nlp= spacy.load('en_core_web_sm')
#from spacy.tokenizer import Tokenizer
matcher= Matcher(nlp.vocab)
Sentence= "The cat is black"
negative_sentence= "The cat is not black"
test_pattern= '''
[
[
{
"TEXT": "cat"
},
{
"LEMMA": "be"
},
{
"LOWER": "not",
"OP": "?"
},
{
"LOWER": "black"
}
]
]
'''
db= json.loads(test_pattern)
matcher.add("TEST_PATTERNS", db)
'''*********************Validate matcher on positive sentence******************'''
doc= nlp(Sentence, matcher)
matches= matcher(doc)
if matches != []:
print('Positive sentence identified')
else:
print('Nothing found for positive sentence')
'''*********************Validate matcher on negative sentence******************'''
doc= nlp(negative_sentence, matcher)
matches= matcher(doc)
if matches != []:
print('Negative sentence identified')
else:
print('Nothing found for negative sentence')
결과:
- 긍정 문장 식별
- 부정 문장 식별
부정을 어떻게 사용하고 "고양이는 검은색이 아니다"가 아닌 "고양이는 검은색이다"만 식별할 수 있습니까?
"OP"의 like to가 된 이유는 "is"와 "black" 사이에 다른 단어가 있을 수 있기 때문입니다(예: "cat is not kind and black"가 아니라 "cat is kind and black"). ).
SpaCy 매처로 부정을 이해하는 데 도움을 주시면 감사하겠습니다.
- 답변 # 1
일치 패턴의 각 사전은 기본적으로 토큰에 해당합니다. 와 더불어
!
연산자는 여전히 부정적인 의미에서 하나의 토큰에 해당합니다. 와 더불어*
연산자는 0개 이상의 토큰에 해당하며,+
하나 이상의 토큰입니다.원래 패턴을 보면 다음과 같은 토큰이 있습니다.
"고양이는 검다"라는 문장이 주어지면 일치 프로세스는 다음과 같이 작동합니다.
패턴을 디버깅할 때 위와 같이 단계별로 실행하는 것이 도움이 됩니다.
다른 작전을 위해...
*
그리고?
"not"이 0번과 일치하기 때문에 작동합니다. 나는 기대하지 않을 것이다+
긍정적 인 경우에 작동합니다.부정적인 것과 일치하는 것을 피하려는 방법은 다소 까다롭습니다. 부정을 무시하고 모든 문장을 먼저 관련 단어와 일치시킨 다음 종속성 구문 분석을 사용하여 부정이 있는지 그런 다음확인하는 것이 좋습니다.