所以我希望程序与单词“ptu”匹配,这很简单,但如果senetence包含“ptu switch”,我希望它不匹配。我如何实现这一点?
import spacy
nlp = spacy.load("en_core_web_sm")
matcher = spacy.matcher.Matcher(nlp.vocab)
# Define the pattern to match "PTU switch"
pattern1 = [{'LOWER': 'ptu'}, {'LOWER': 'switch', 'IS_STOP': True}]
# Add the pattern to the matcher
matcher.add("PTU_SWITCH", [pattern1])
# Process the text and check for matches
doc = nlp("ptu switch was replaced")
matches = matcher(doc)
# Print the result
if matches:
    for match_id, start, end in matches:
        string_id = nlp.vocab.strings[match_id]  # Get string representation
        span = doc[start:end]
        print(str(span))
            
else:
    print("PTU not found.")
我尝试过使用这个,但如果有开关,它仍然可以与PTU匹配。我不想写if语句来寻址,因为我不想硬编码它,而是纯粹根据我可以寻址的模式。