mardi 19 mars 2019

combine yield with if/else loop in python

I want to join two words separated by an asterisk (*) in a list of French words. After joining these words I want to check if this word exists in a French dictionary. If so, the concatenated word should remain in the list, if not it should be appended to another list. I have used yield (I'm new to this function) in my code but there is something wrong with my nested if/else loop. Can anyone help me to accomplish my goal? My unsuccessful code is below:

words = ['Bien', '*', 'venue', 'pour', 'les','engage', '*', 'ment','trop', 'de', 'YIELD', 'peut','être','contre', '*', 'productif' ]

with open ('Fr-dictionary.txt') as fr:
    dic = word_tokenize(fr.read().lower())

l=[ ]

def join_asterisk(ary):
    i, size = 0, len(ary)
    while i < size-2:
        if ary[i+1] == '*':
            if ary[i] + ary[i+2] in dic:
                yield ary[i] + ary[i+2]
                i+=2
            else: yield ary[i]
            i+=1
            l.append(ary[i] + ary[i+2])
    if i < size:
        yield ary[i]



print(list(join_asterisk(words)))

Aucun commentaire:

Enregistrer un commentaire