match POS tag and sequence of words

后端 未结 5 1734
我寻月下人不归
我寻月下人不归 2020-12-22 06:30

I have the following two strings with their POS tags:

Sent1: \"something like how writer pro or phraseology works would be really cool.\"<

5条回答
  •  离开以前
    2020-12-22 07:25

    it seem you would just search consecutive tags for "would" followed by "be" and then for the first instance of tag "JJ". Something like this:

    import nltk
    
    def has_would_be_adj(S):
        # make pos tags
        pos = nltk.pos_tag(S.split())
        # Search consecutive tags for "would", "be"
        j = None  # index of found "would"
        for i, (x, y) in enumerate(zip(pos[:-1], pos[1:])):
            if x[0] == "would" and y[0] == "be":
                j = i
                break
        if j is None or len(pos) < j + 2:
            return False
        a = None  # index of found adjective
        for i, (word, tag) in enumerate(pos[j + 2:]):
            if tag == "JJ":
                a = i+j+2 #
                break
        if a is None:
            return False
        print("Found adjective {} at {}", pos[a], a)
        return True
    
    S = "something like how writer pro or phraseology works would be really cool."
    print(has_would_be_adj(S))
    

    I'm sure this could be written compacter and cleaner but it does what it says on the box :)

提交回复
热议问题