match POS tag and sequence of words

后端 未结 5 1745
我寻月下人不归
我寻月下人不归 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:12

    Check StackOverflow Link

    from nltk.tokenize import word_tokenize
    def would_be(tagged):
        return any(['would', 'be', 'JJ'] == [tagged[i][0], tagged[i+1][0], tagged[i+2][1]] for i in xrange(len(tagged) - 2))
    
    S = "more options like the syntax editor would be nice."  
    pos = nltk.pos_tag(word_tokenize(S)) 
    would_be(pos)   
    

    Also check code

    from nltk.tokenize import word_tokenize
    import nltk
    def checkTag(S):
        pos = nltk.pos_tag(word_tokenize(S))
        flag = 0
        for tag in pos:
            if tag[1] == 'JJ':
                flag = 1
        if flag:
            for ind,tag in enumerate(pos):
                if tag[0] == 'would' and pos[ind+1][0] == 'be':
                        return True
            return False
        return False
    
    S = "something like how writer pro or phraseology works would be really cool."
    print checkTag(S)
    

提交回复
热议问题