繁体   English   中英

如何将词典应用于句子列表?

[英]How can I apply a lexicon to a list of sentences?

我有这种形状的词典字典

6   ابن جزمه    1
7   ابو جهل -1
8   اتق الله    -1
9   اتقو الله   1

我想创建一个新列表,其中包含基于词典的每个句子的分数,加上每个单词的分数,如果不存在任何单词,则在实现我的代码时附加零,在添加elif条件后,我会得到len(lex_score) = 3679 len(lex_score) = 95079

len(lex_score)应该等于6064

lex_score = []
def lexic(text):
    for tweet in sentences:
        score = 0
        for word in tweet.split():
            if word in lexicon:
                score = score+lexicon[word]
            elif word not in lexicon:
                score = 0
                lex_score.append(score)

我想在数据框中创建一个新列,其中包含每个句子的分数。 我究竟做错了什么? 有没有更好的方法呢?

IIUC,您可以将每个推文中有效词典条目的分数求和,然后在每次sentences迭代中将该分数附加到lex_score

注意:我假设text == sentences -否则,缺少一行text分解为sentences 无论哪种方式,此基本方法都应仍然有效:

def lexic(text):
    lex_score = []
    for tweet in text: # assuming sentences == text
        score = sum([lexicon[word] for word in tweet.split() if word in lexicon])
        lex_score.append(score)
    return lex_score

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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