简体   繁体   中英

lists of tuples by a tuple element?

I have a problem on matching the part of speech pos pattern. we had a rules of preposition phrase pattern such as NN + IN + NN, VBG + IN + NN or ADJ + IN + NN.

The idea is extract the pattern from any given sentence and do matching with the define rules above, if matched then return True.

example extracted from sentence: sent_pos = [('increasing', 'VBG'), ('of', 'IN'), ('mutation', 'NN')] match with either rules1 = [('', 'VBG'), ('', 'IN'), ('', 'NN')] or rule2 = [('', 'NN'), ('', 'IN'), ('', 'NN')] or [('', 'ADJ'), ('', 'IN'), ('', 'NN')]

result return True.

It is possible in python code?

Thanks appreciated for your reply.

I'm not really sure if this is what you are looking for because I don't know if the order of the patterns is always the same in rule1, rule2 and rule3, but try this:

def function():

   sent_pos = [('increasing', 'VBG'), ('of', 'IN'), ('mutation', 'NN')]
   rule1 = [('', 'VBG'), ('', 'IN'), ('', 'NN')]
   rule2 = [('', 'NN'), ('', 'IN'), ('', 'NN')]
   rule3 = [('', 'ADJ'), ('', 'IN'), ('', 'NN')]

   rules = [rule1, rule2, rule3]

   for element in rules:
        counter = 0
        for i in range(len(sent_pos)):
            if sent_pos[i][1] == element[i][1]:
                counter += 1
        if counter == len(sent_pos):
            return True
   return False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM