python中每个单词出现的次数是一组字符串?

2024-10-01 19:18:52 发布

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

我在文本中搜索了以下3个句子,并使用sentence.append()将它们附加到列表sentence

例如

sentence[0]=" hello my name is John"
sentence[1]="good morning I am John"
sentence[2]= "hello I am Smith"

我想根据每个单词在三个句子中出现的次数为每个句子和每个单词分配一个分数。在

例如:

^{pr2}$

所以我用它来计算句子中每个单词的出现率(分数),我的问题是如何用它来计算句子的分数?

dict = {}
for sentenceC in sentence:
    for word in re.split('\s', sentenceC): # split with whitespace
        try:
            dict[word] += 1
        except KeyError:
            dict[word] = 1
print (dict)

Tags: in文本helloforamjohn单词分数
2条回答

你可以这样得到你的分数:

import re

sentence = list()

sentence.append(" hello my name is John")
sentence.append("good morning I am John")
sentence.append("hello I am Smith")

value = dict()
for sentenceC in sentence:
    for word in sentenceC.strip().split(" "): # split with whitespace
        try:
            value[word.lower()] += 1
        except KeyError:
            value[word.lower()] = 1
print (value)

score = dict()
number = 1
for sentenceC in sentence:
    for word in sentenceC.strip().split(" "): # split with whitespace
        try:
            score[number] += value[word.lower()]
        except KeyError:
            score[number] = value[word.lower()]
    number += 1

print score

#output: {1: 7, 2: 8, 3: 7}

将问题分解为子任务

def getWordScores(sentences):
   scores = {}
   for sentence in sentences:
      for word in sentence.strip().split():
          word = word.lower()
          scores[word] = scores.get(word,0) + 1
   return scores

def getSentenceScore(sentence, word_scores):
   return sum(word_scores.get(w.lower(), 0) for w in sentence.strip().split())

然后组合任务以获得解决方案

^{pr2}$

相关问题 更多 >

    热门问题