无论如何,我可以在Spacy Matcher中使用if else语句吗?

时间:2019-12-19 03:21:19

标签: python-3.x spacy matcher

我正在尝试根据工作要求使用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年?

谢谢你!

1 个答案:

答案 0 :(得分:0)

Matcher本身无法做到这一点。您将需要运行匹配器,分析结果以查看匹配的模式,然后在找不到任何内容的情况下输出“ 0 years”(例如)。

此外,请记住Matcher模式是针对单个令牌的,因此{"lower": "fresh grads"}不会因为中间的空格而与任何内容匹配。您需要将其分为两个令牌模式。