match POS tag and sequence of words

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

    from itertools import tee,izip,dropwhile
    import nltk
    def check_sentence(S):
        def pairwise(iterable):
            "s -> (s0,s1), (s1,s2), (s2, s3), ..."
            a, b = tee(iterable)
            next(b, None)
            return izip(a, b)
    
    
        def consecutive_would_be(word_group):
            first, second = word_group
            (would_word, _) = first
            (be_word, _) = second
            return would_word.lower() != "would" && be_word.lower() != "be"
    
    
        for word_groups in dropwhile(consecutive_would_be, pairwise(nltk.pos_tag(nltk.word_tokenize(S))):
            first, second = word_groups
            (_, pos1) = first
            (_, pos2) = second
            if pos1 == "JJ" || pos2 == "JJ":
                return True
        return False
    

    and then you can use the function like so:

    S = "more options like the syntax editor would be nice."  
    check_sentence(S)
    

提交回复
热议问题