无论如何,我可以在空间匹配器中使用 if else 语句吗?



我正在尝试使用Spacy的Matcher来满足工作要求,以便我能够找到雇主正在寻找的经验年限。

doc = nlp("""
<ul><li>2-3 years working experience ideal but driven Fresh Grads are welcomed!</li><li>Create elegant and compelling designs for all of Dapat products and Social Media channels</li><li>Knowledge in Social Media / Digital Marketing is a PLUS!</li><li>You are a chameleon - Able to adapt to new challenges, working environments etc.</li><li>Ability to work on multiple projects, prioritize as needed and manage time in an efficient manner</li><li>Knowledge in Adobe Creative Suite / Cloud (Photoshop, Illustrator, InDesign)</li><li>Knowledge in UI/UX tools like Sketch, Invision, Balsamiq - is also a PLUS!</li><li>You are an excellent team player and do not shy away from responsibilities and challenges&nbsp;</li><li>Organized, detail oriented and able to work independently on multiple projects</li><li>Proficiency with office productivity tools including email, web browsers, web apps, database search, spreadsheets/word processors (Google Apps)</li><li>Interest in and familiarity with current tech products</li></ul><div><br></div>
""")
# Initialize the Matcher with the shared vocabulary
matcher = Matcher(nlp.vocab)
# Create a pattern matching the tokens
pattern = [{'IS_DIGIT':True}, {"LEMMA": "year", 'POS':'NOUN'}]
pattern1 = [{'IS_DIGIT': True},{'TEXT':'-'},{'IS_DIGIT':True},{"LEMMA": "year", 'POS':'NOUN'}]
pattern2 = [{'IS_DIGIT': True},{'TEXT':'to'},{'IS_DIGIT':True},{"LEMMA": "year", 'POS':'NOUN'}]
pattern3 = [{'IS_DIGIT': True},{'TEXT':'or'},{'IS_DIGIT':True},{"LEMMA": "year", 'POS':'NOUN'}]
pattern4 = [{'IS_DIGIT': True},{'TEXT':'+'},{"LEMMA": "year", 'POS':'NOUN'}]
pattern5 = [{'IS_ALPHA': True},{'TEXT':'('},{'IS_DIGIT': True},{'TEXT':')'},{'TEXT':'to'},{'IS_ALPHA':True},{'TEXT':'('},{'IS_DIGIT': True},{'TEXT':')'},{"LEMMA": "year", 'POS':'NOUN'}]
pattern6 = [{'lower':'fresh grads'}]
# Add the pattern to the matcher
matcher.add("x years", None, pattern)
matcher.add("z - x years", None, pattern1)
matcher.add("z to x years", None, pattern2)
matcher.add("z or y years",None, pattern3)
matcher.add('x+ years', None, pattern4)
matcher.add('alpha(digit) to alpha(digit) years', None, pattern5)

# Use the matcher on the doc
matches = matcher(doc)
matched = [doc[start:end].text for match_id, start, end in matches]
print("Matches:", matched)

对于最后一个模式 pattern6,我正在尝试匹配应届毕业生,所以当我在要求中获得应届毕业生时,它只会打印出 0 年的经验,但如果不是,它只会打印出任何匹配的句子。无论如何,我可以使用 if else 语句,如果触发 pattern6,那么它将打印匹配项:0 年?

提前谢谢你!

Matcher本身没有办法做到这一点。您需要运行匹配器,分析结果以查看哪些模式匹配,如果未找到任何模式,则输出"0 年"(例如(。

另外,请记住,Matcher模式适用于单个令牌,因此由于中间的空间{"lower": "fresh grads"}不会匹配任何内容。您需要将其拆分为两种令牌模式。

最新更新