改进for循环尝试比较两个dict列表

2024-09-29 23:25:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我会尽可能地让自己明白:我有5万条tweet我想做文本挖掘,我想改进我的代码。数据如下(sample_data)。你知道吗

我对我清理和标记的单词(即twToken键的值)进行柠檬化很感兴趣

sample_data = [{'twAuthor': 'Jean Lassalle',
                'twMedium': 'iPhone',
                'nFav': None,
                'nRT': '33',
                'isRT': True,
                'twText': ' RT @ColPeguyVauvil : @jeanlassalle "allez aux bouts de vos rêves" ',
                'twParty': 'Résistons!',
                'cleanText': ' rt colpeguyvauvil jeanlassalle allez aux bouts de vos rêves ',
                'twToken': ['colpeguyvauvil', 'jeanlassalle', 'allez', 'bouts', 'rêves']},
               {'twAuthor': 'Jean-Luc Mélenchon',
                'twMedium': 'Twitter Web Client',
                'nFav': '806',
                'nRT': '375',
                'isRT': False,
                'twText': ' (2/2) Ils préfèrent créer une nouvelle majorité cohérente plutôt que les alliances à géométrie variable opportunistes de leur direction. ',
                'twParty': 'La France Insoumise',
                'cleanText': ' 2 2 ils préfèrent créer une nouvelle majorité cohérente plutôt que les alliances à géométrie variable opportunistes de leur direction ',
                'twToken': ['2', '2', 'préfèrent', 'créer', 'nouvelle', 'majorité', 'cohérente', 'plutôt', 'alliances', 'géométrie', 'variable', 'opportunistes', 'direction']},
               {'twAuthor': 'Nathalie Arthaud',
                'twMedium': 'Android',
                'nFav': '37',
                'nRT': '24',
                'isRT': False,
                'twText': ' #10mai Commemoration fin de l esclavage. Reste à supprimer l esclavage salarial defendu par #Macron et Hollande ',
                'twParty': 'Lutte Ouvrière',
                'cleanText': ' 10mai commemoration fin de l esclavage reste à supprimer l esclavage salarial defendu par macron et hollande ',
                'twToken': ['10mai', 'commemoration', 'fin', 'esclavage', 'reste', 'supprimer', 'esclavage', 'salarial', 'defendu', 'macron', 'hollande']
               }]

然而,在Python中没有可靠的法国lemmatizer。所以我使用了一些资源,有我自己的法语单词lemmatizer字典。字典看起来是这样的:

sample_lemmas = [{"ortho":"rêves","lemme":"rêve","cgram":"NOM"},
                 {"ortho":"opportunistes","lemme":"opportuniste","cgram":"ADJ"},
                 {"ortho":"préfèrent","lemme":"préférer","cgram":"VER"},
                 {"ortho":"nouvelle","lemme":"nouveau","cgram":"ADJ"},
                 {"ortho":"allez","lemme":"aller","cgram":"VER"},
                 {"ortho":"défendu","lemme":"défendre","cgram":"VER"}]

因此ortho是一个词的书面形式(如加工的),lemme是这个词的柠檬化形式(如加工的),cgram是一个词的语法范畴(如动词的VER)。你知道吗

所以我想做的是为每个tweet创建一个twLemmas键,它是从twToken列表派生的引理列表。因此,我循环遍历sample_data中的每个tweet,然后循环遍历twToken中的每个标记,查看标记是否存在于我的引理字典sample_lemmas中,如果存在,我从sample_lemmas字典中检索引理,并将其添加到每个twLemmas键中的列表中。如果没有,我就把这个词添加到列表中。你知道吗

我的代码如下:

list_of_ortho = []                      #List of words used to compare if a token doesn't exist in my lemmas dictionary
for wordDict in sample_lemmas:          #This loop feeds this list with each word
    list_of_ortho.append(wordDict["ortho"])

for elemList in sample_data:            #Here I iterate over each tweet in my data
    list_of_lemmas = []                 #This is the temporary list which will be the value to each twLemmas key
    for token in elemList["twToken"]:   #Here, I iterate over each token/word of a tweet
        for wordDict in sample_lemmas:
            if token == wordDict["ortho"]:
                list_of_lemmas.append(wordDict["lemme"])
        if token not in list_of_ortho:  #And this is to add a word to my list if it doesn't exist in my lemmas dictionary
            list_of_lemmas.append(token)
    elemList["lemmas"] = list_of_lemmas

sample_data

循环工作正常,但需要大约4小时才能完成。现在我知道我既不是程序员也不是Python专家,而且我知道无论发生什么事情都需要时间来完成。然而,这就是为什么我想问你是否有人对如何改进我的代码有更好的想法?你知道吗

如果有人能花时间理解我的代码并帮助我,谢谢你。我希望我说的够清楚(对不起,英语不是我的第一语言)。你知道吗


Tags: ofsampleintokendatadeprlist

热门问题