Python:循环后存储在变量中的每个单词的分数

Python: Giving Score for Each Word stored in a Variable after Looping

我有一个案例需要解决,但我已经将近一个星期没有解决方案了。 是这样的。我有三个变量:

candidates = ["you", "the", "best", "love", "fun", "feeling", "emotionally"]
seeds = ["happy", "love", "enjoy", "fun", "grace", "sad", "guilty"]
tweets = ["you look so happy", "I am in love with you", "hey you do the best at having fun okay", "i am emotionally sad right now", "feeling guilty"]

我想做的是将变量 "candidates" 中的词与 "seeds" 中的词配对,在推文中将它们循环在一起,并在循环后给变量中的每个词打分"candidates".

例如: 对于我配对的第一个循环:

you + happy
you + love
you + enjoy
you + fun
you + grace
you + sad
you + guilty

并将它们循环遍历变量 "tweets" 中的字符串,并根据这些对在变量推文的句子中出现的次数给出分数。所以在这种情况下,单词 "you" 与变量 "seeds" 中的所有单词配对得到的分数是 3。

并继续第二个循环,其中对是:

the + happy
the + love
the + fun
the + enjoy
the + grace
the + sad
the + guilty

并将它们再次循环到变量 "tweets" 中的字符串,并根据这些对在变量推文的句子中出现的次数给出分数。所以在这种情况下,单词 "you" 与变量 "seeds" 中的所有单词配对得到的分数是 1。

我希望我的程序能够自动 return 对变量 "candidates" 中的每个单词与变量 "seeds" 中的单词配对进行评分,并将它们一起循环到变量 "tweets".

#looping all pair candidates and seed to the tweets
for tweet in tweets:
    for candidate in candidates:
        for seed in seeds:
            if candidate in tweet and seed in tweet:
                if "happy" in tweet or "love" in tweet or "fun" in tweet:
                    print(candidate, seed, tweet)
                    count_happy += 1
                elif "sad" in tweet or "guilty" in tweet:
                    print(candidate, seed, tweet)
                    count_sad += 1
        count_a += 1

以上是我为执行我想执行的操作而创建的脚本,但它无法按我希望的方式运行。所以请有人知道如何解决我的这个问题吗?已经一个星期了,我还没有找到解决方案。

这是您要执行的操作的示例脚本: 所做的是将每个候选元素添加到字典中,键为候选名称+一个字符串 all seeds 。当循环遍历种子时,我只是将此键的值附加 +=1

import codecs
import itertools
import threading
import csv


candidates = []
seeds = []
tweets = []
global lock

with codecs.open("d:\untitled\candidates.csv", encoding='utf8')  as candFile:
    readCSV = csv.reader(candFile, delimiter=',')
    for lines in candFile:
        if lines.rstrip() != "" :
            candidates.append(lines.rstrip())

with codecs.open("d:\untitled\seeds.csv",encoding='utf8')  as candFile:
    readCSV = csv.reader(candFile, delimiter=',')
    for lines in candFile:
        if lines.rstrip() != "" :
            seeds.append(lines.rstrip())

with codecs.open("d:\untitled\tweets.csv", encoding='utf8')  as candFile:
    readCSV = csv.reader(candFile, delimiter=',')
    for lines in candFile:
        if lines.rstrip() != "" :
            tweets.append(lines.rstrip())


counts = {}


def findMatch(listToWorkWith,tweet):
    for CS in listToWorkWith:
        if (CS[0] in tweet) and (CS[1] in tweet):
            try:
                counts[CS[0] + "+ All Seeds"] += 1
            except:
                counts[CS[0] + "+ All Seeds"] = 1


listOFAugrs = list(itertools.product(candidates, seeds))
lock = threading.Lock()
threads = [threading.Thread(target=findMatch, args=(listOFAugrs,x)) for x in tweets[0:10]]
for t in threads:
    t.start()
for t in threads:
    t.join()

print(counts)

这是我们在这里所能做的最好的,但对于每条推文来说,数量仍然很大,我们要循环的组合数量呈指数级增长。所以这需要一些时间。

10 条推文和所有组合的输出,即推文[0:10]

{'jypetwic+ All Seeds': 2, 'twiceth+ All Seeds': 2, '5th+ All Seeds': 2, 'mini+ All Seeds': 2, 'albumltwhat+ All Seeds': 2, 'lovegtreleas+ All Seeds': 2, 'onlinemelon+ All Seeds': 2, 'twice+ All Seeds': 2, '트와이스+ All Seeds': 2, 'whatislov+ All Seeds': 2, 'u+ All Seeds': 16, 'voidtopaz+ All Seeds': 4, 'fandom+ All Seeds': 4, 'lolchoni+ All Seeds': 4, 'm+ All Seeds': 26, 'sinhuigang+ All Seeds': 2, 'rocket+ All Seeds': 2, 'confid+ All Seeds': 2, 'gayfal+ All Seeds': 2, 'distinguish+ All Seeds': 2, 'gaymi+ All Seeds': 2, 'gay+ All Seeds': 2, 'and+ All Seeds': 11, 're+ All Seeds': 7, 'morwennajh+ All Seeds': 3, 'imagin+ All Seeds': 3, 'painthi+ All Seeds': 3, 'darl+ All Seeds': 3, 'boy+ All Seeds': 6, 'inbut+ All Seeds': 3, 'need+ All Seeds': 3, 'chanc+ All Seeds': 3, 'liveco+ All Seeds': 3, 'singet+ All Seeds': 3, 'get+ All Seeds': 7, 'de+ All Seeds': 9, 'ed+ All Seeds': 3, 'akoposimarcelo+ All Seeds': 1, 'dontdont+ All Seeds': 1, 'deservem+ All Seeds': 1, 'im+ All Seeds': 7, 'live+ All Seeds': 3, 'ke+ All Seeds': 7, 'but+ All Seeds': 3, 'ur+ All Seeds': 10, 'htapmenami+ All Seeds': 5, 'alway+ All Seeds': 5, 'saturday+ All Seeds': 4, 'invit+ All Seeds': 4, 'friend+ All Seeds': 4, 'teach+ All Seeds': 4, 'magicyou+ All Seeds': 4, 'llanowar+ All Seeds': 4, 'elv+ All Seeds': 4, 'promo+ All Seeds': 4, 'll+ All Seeds': 4, 'or+ All Seeds': 5, 'y+ All Seeds': 21, 'p+ All Seeds': 24, 'imag+ All Seeds': 3, 'badrepseokjin+ All Seeds': 2, 'loop+ All Seeds': 2, 'jin+ All Seeds': 2, 'say+ All Seeds': 2, 'korean+ All Seeds': 2, 'english+ All Seeds': 2, 'vid+ All Seeds': 2, 'cr+ All Seeds': 2, 'eundaromi+ All Seeds': 2, 'r+ All Seeds': 26, 'abl+ All Seeds': 5, 'harder+ All Seeds': 5, 'end+ All Seeds': 4, 'sat+ All Seeds': 4, 'h+ All Seeds': 22, 'id+ All Seeds': 8, 'ab+ All Seeds': 5, 'yo+ All Seeds': 7, 'dr+ All Seeds': 2, 'gu+ All Seeds': 5, 'oc+ All Seeds': 2, 'con+ All Seeds': 5, 'f+ All Seeds': 13, 'v+ All Seeds': 25, 'ad+ All Seeds': 3, 'sh+ All Seeds': 4, 'ou+ All Seeds': 7, 'ive+ All Seeds': 3, 'don+ All Seeds': 1, 'n+ All Seeds': 27, 'rock+ All Seeds': 2, 'bu+ All Seeds': 5, 'neadawn808+ All Seeds': 1, 'soo+ All Seeds': 1, 'paint+ All Seeds': 3, 'sin+ All Seeds': 5, 'releas+ All Seeds': 2, 'twic+ All Seeds': 2, 'deserv+ All Seeds': 1, 'sa+ All Seeds': 6, 'l+ All Seeds': 27, 'way+ All Seeds': 5, 'album+ All Seeds': 2, 'melon+ All Seeds': 2, 'k+ All Seeds': 13, 'j+ All Seeds': 11, 've+ All Seeds': 21, 'gucciboytaeba+ All Seeds': 3, 'pass+ All Seeds': 3, 'second+ All Seeds': 3, 'bc+ All Seeds': 3, 'your+ All Seeds': 3, 'th+ All Seeds': 5, 'd+ All Seeds': 25, 'ig+ All Seeds': 2, 'life+ All Seeds': 3, 'go+ All Seeds': 3, 'stress+ All Seeds': 3, 'time+ All Seeds': 3, 'rn+ All Seeds': 3, 'nb+ All Seeds': 3, 'pa+ All Seeds': 10, 'eu+ All Seeds': 2, 'jh+ All Seeds': 3, 'vi+ All Seeds': 6, 'en+ All Seeds': 14, 'ok+ All Seeds': 7, 'b+ All Seeds': 15, 'c+ All Seeds': 21, 'magic+ All Seeds': 4, 'dese+ All Seeds': 1, 'hu+ All Seeds': 2, 'g+ All Seeds': 16, 'op+ All Seeds': 7, 'w+ All Seeds': 15, 'co+ All Seeds': 12, 'ar+ All Seeds': 15, 'fr+ All Seeds': 4, 'ma+ All Seeds': 8, 'e+ All Seeds': 27, 'o+ All Seeds': 27, 'fa+ All Seeds': 6, 'int+ All Seeds': 3, 'ea+ All Seeds': 9, 'bo+ All Seeds': 6, 'ang+ All Seeds': 2, 'di+ All Seeds': 2, 'bro+ All Seeds': 5, 'kor+ All Seeds': 2, 'la+ All Seeds': 4, 'bum+ All Seeds': 2, 'ti+ All Seeds': 7, 'lov+ All Seeds': 18, 'se+ All Seeds': 6, 'dis+ All Seeds': 2, 'ng+ All Seeds': 7, 'lo+ All Seeds': 18, 'und+ All Seeds': 2, 'aint+ All Seeds': 3, 'kore+ All Seeds': 2, 'rom+ All Seeds': 6, 'li+ All Seeds': 10, 'aro+ All Seeds': 2, 'korea+ All Seeds': 2, 'el+ All Seeds': 7, 'ko+ All Seeds': 3, 'al+ All Seeds': 13, 'pro+ All Seeds': 4, 'na+ All Seeds': 8, 'un+ All Seeds': 2, 'lan+ All Seeds': 4, '8+ All Seeds': 1, 'rea+ All Seeds': 2, 'serv+ All Seeds': 1, 'mor+ All Seeds': 3, 'ain+ All Seeds': 3, 'prom+ All Seeds': 4, 'men+ All Seeds': 5, 'tu+ All Seeds': 4, 'ov+ All Seeds': 18, 'mo+ All Seeds': 7, 'pm+ All Seeds': 5, 'mag+ All Seeds': 7, 'sing+ All Seeds': 3, 'gang+ All Seeds': 2, 'ht+ All Seeds': 5, 'ch+ All Seeds': 13, 'te+ All Seeds': 4, 'ting+ All Seeds': 2, 'jyp+ All Seeds': 2, 'thi+ All Seeds': 3, 'ep+ All Seeds': 2, 'anc+ All Seeds': 3, 'st+ All Seeds': 5, 'rd+ All Seeds': 12, 'wha+ All Seeds': 2, 'han+ All Seeds': 3, 'br+ All Seeds': 5, 'wa+ All Seeds': 9, 'in+ All Seeds': 13, 'seokjin+ All Seeds': 2, 'gi+ All Seeds': 7, 'fi+ All Seeds': 2, 'ay+ All Seeds': 13, 'mi+ All Seeds': 11, 'tin+ All Seeds': 2, 'mu+ All Seeds': 3, 'da+ All Seeds': 10, 'cha+ All Seeds': 5, 'ri+ All Seeds': 4, 'oy+ All Seeds': 10, 'mar+ All Seeds': 1, 'nda+ All Seeds': 2, 'ngl+ All Seeds': 2, 'rep+ All Seeds': 2, 'ho+ All Seeds': 4, 'ima+ All Seeds': 4, 'seo+ All Seeds': 2, 'ne+ All Seeds': 6, 'res+ All Seeds': 3, 'lea+ All Seeds': 2, 'turd+ All Seeds': 4, 'et+ All Seeds': 11, 'fe+ All Seeds': 3, 'tim+ All Seeds': 3, '5+ All Seeds': 2, 'fri+ All Seeds': 4, 'line+ All Seeds': 2, 'gt+ All Seeds': 2, 'ro+ All Seeds': 13, 'liv+ All Seeds': 3, 'si+ All Seeds': 6, 'ba+ All Seeds': 5, 'wh+ All Seeds': 2, 'jype+ All Seeds': 2, 'dom+ All Seeds': 4, 'chao+ All Seeds': 2, 'nd+ All Seeds': 13, 'ji+ All Seeds': 2, 'whati+ All Seeds': 2, 'ac+ All Seeds': 4, 'eth+ All Seeds': 2, 'aj+ All Seeds': 3, 'deser+ All Seeds': 1, 'ai+ All Seeds': 3, 'wi+ All Seeds': 2, 'em+ All Seeds': 3, 'cc+ All Seeds': 3, 'ag+ All Seeds': 7, 'what+ All Seeds': 2, 'om+ All Seeds': 10, 'onlin+ All Seeds': 2, 'eco+ All Seeds': 6, 'ove+ All Seeds': 18, 'lon+ All Seeds': 2, 'ot+ All Seeds': 2, 'ol+ All Seeds': 8, 'le+ All Seeds': 2, 'az+ All Seeds': 4, 'pet+ All Seeds': 2, 'ie+ All Seeds': 4, 'ae+ All Seeds': 3, 'dre+ All Seeds': 2, 'chan+ All Seeds': 3, 'po+ All Seeds': 1, 'min+ All Seeds': 2, 'gin+ All Seeds': 3, 'eng+ All Seeds': 2, 'ine+ All Seeds': 2, 'mur+ All Seeds': 3, 'ak+ All Seeds': 1, 'aw+ All Seeds': 1, 'tap+ All Seeds': 5, 'ci+ All Seeds': 3, 'nee+ All Seeds': 3, 'onl+ All Seeds': 2, 'lt+ All Seeds': 2, 'ce+ All Seeds': 3, 'um+ All Seeds': 2, 'ako+ All Seeds': 1, 'ap+ All Seeds': 5, '0+ All Seeds': 1, 'arc+ All Seeds': 1, 'tr+ All Seeds': 5, 'bl+ All Seeds': 5, 'har+ All Seeds': 5, 'rel+ All Seeds': 2, 'ice+ All Seeds': 2, 'tae+ All Seeds': 3, 'gucci+ All Seeds': 3, 'ni+ All Seeds': 6, 'ta+ All Seeds': 8, 'twi+ All Seeds': 2, 'fu+ All Seeds': 3, 'pe+ All Seeds': 2, 'hati+ All Seeds': 2, 'tw+ All Seeds': 2, 'pos+ All Seeds': 1, 'ami+ All Seeds': 5, 'vo+ All Seeds': 4, 'lif+ All Seeds': 3, 'sim+ All Seeds': 1, 'cond+ All Seeds': 3, 'yt+ All Seeds': 3, 'void+ All Seeds': 4, 'relea+ All Seeds': 2, 'whatislo+ All Seeds': 2, 'dawn+ All Seeds': 1, 'sec+ All Seeds': 3, 'ml+ All Seeds': 2, 'eas+ All Seeds': 2, '80+ All Seeds': 1, 'nam+ All Seeds': 5, 'econ+ All Seeds': 3, 'ada+ All Seeds': 1, 'ont+ All Seeds': 1, 'posi+ All Seeds': 1, 'ib+ All Seeds': 3, 'lb+ All Seeds': 2, 'seco+ All Seeds': 3, 'veg+ All Seeds': 2, 'isl+ All Seeds': 2, 'hat+ All Seeds': 2}

toheedNiaz 已经回答了你,这就是为什么我不会重做他所做的。但是,如果你还想知道一对出现的次数,你也可以这样做

candidates = ["you", "the", "best", "love", "fun", "feeling", "emotionally"]
seeds = ["happy", "love", "enjoy", "fun", "grace", "sad", "guilty"]
tweets = ["you look so happy", "I am in love with you", "hey you do the best at having fun okay", "i am emotionally sad right now", "feeling guilty"]

    dic = {}
    for candidate in candidates:
        for seed in seeds:
            dic[(candidate,seed)] = 0

    for tweet in tweets:
        for elem in dic:
            if elem[0] in tweet and elem[1] in tweet:
                dic[elem] += 1

    print dic

输出:

('the', 'fun'): 1, ('emotionally', 'fun'): 0, ('the', 'grace'): 0, ('love', 'fun'): 0, ('feeling', 'happy'): 0, ('best', 'fun'): 1, ('best', 'sad'): 0, ('you', 'enjoy'): 0, ('love', 'sad'): 0, ('fun', 'love'): 0, ('the', 'happy'): 0, ('best', 'happy'): 0, ('you', 'love'): 1, ('emotionally', 'sad'): 1, ('feeling', 'sad'): 0, ('love', 'happy'): 0, ('fun', 'enjoy'): 0, ('feeling', 'enjoy'): 0, ('the', 'guilty'): 0}

现在应用治疗更容易了。

编辑:

不是超级优化,但应该会更好,分类:

candidates = ["you", "the", "best", "love", "fun", "feeling", "emotionally"]
seeds = ["happy", "love", "enjoy", "fun", "grace", "sad", "guilty"]
tweets = ["you look so happy", "I am in love with you", "hey you do the best at having fun okay",
          "i am emotionally sad right now", "feeling guilty"]
dico = {}
for tweet in tweets:
    seeds_list = []
    candidates_list = []
    for word in tweet.split(' '):
        if word in seeds:
            seeds_list.append(word)
        if word in candidates:
            candidates_list.append(word)

    for seed in seeds_list:
        if seed not in dico:
            dico[seed] = len(candidates_list)
        else:
            dico[seed] += len(candidates_list)

输出:

{'sad': 1, 'happy': 1, 'fun': 4, 'guilty': 1, 'love': 2}