使用字典计算列表中的单词
Counting words in list using a dictionary
我有一个字典列表,其中包含一个单词和该单词的一些拼写错误。我试图通过一个字符串列表,首先计算单词的出现次数,然后计算每个拼写错误的出现次数。我曾尝试使用 if word in string
但这最终会给我一个错误的计数,因为许多拼写错误中包含实际单词本身。是否可以在这里使用 pythons counter
或者正则表达式更有意义?
例如我有
words = [{'word':'achieve','misspellings': ['acheive','acheiv','achiev']},
{'word':'apparently','misspellings':['apparantly','apparintly']}]
我想查看一个字符串列表,最后得到每个单词及其拼写错误的总数。我遇到像 achiev 这样的拼写错误的问题,当使用 if word in string
时会把计数弄乱,因为 achiev 在 achieve 中,所以计数会被关闭。
正则表达式可能是一个很好的工具 - word boundary anchors 可以帮助您避免单词中的子匹配。
对于每个单词,使用 wordre = re.compile(r"\b" + word + r"\b", re.I|re.U)
构建一个正则表达式,然后计算 re.findall(wordre, string)
的结果。
您应该将拼写错误的单词映射到原始单词:
words = {'acheive':'achieve', 'achiev':'achieve','achieve':'achieve'}
s = "achiev acheive achieve"
from collections import Counter
from string import punctuation
cn = Counter()
for word in s.split():
word = word.strip(punctuation)
if word in words:
wrd = words[word]
cn[wrd] += 1
print(cn)
Counter({'achieve': 3})
您可以将它与正则表达式结合起来查找字符串中的所有单词,而不是按照每个单词进行拆分
回答。
要计算拼写错误和原始拼写,只需检查单词字典查找返回的值是否等于单词,如果是,则更新单词的原始计数,否则更新未命中计数:
words = {'acheive':'achieve', 'achiev':'achieve','achieve':'achieve',
'apparently':'apparently','apparantly':'apparently','apparintly':'apparently'}
s = "achiev acheive achieve! 'apparently' apparintly 'apparantly?""
from collections import defaultdict
from string import punctuation
cn = defaultdict(lambda:{"orig": 0 ,"miss":0})
for word in s.split():
word = word.strip(punctuation)
if word in words:
wrd = words[word]
if wrd == word:
cn[wrd]["orig"] += 1
else:
cn[wrd]["miss"] += 1
print(cn)
defaultdict(<function <lambda> at 0x7f001fb2a8c0>,
{'apparently': {'miss': 2, 'orig': 1}, 'achieve': {'miss': 2, 'orig': 1}})
我有一个字典列表,其中包含一个单词和该单词的一些拼写错误。我试图通过一个字符串列表,首先计算单词的出现次数,然后计算每个拼写错误的出现次数。我曾尝试使用 if word in string
但这最终会给我一个错误的计数,因为许多拼写错误中包含实际单词本身。是否可以在这里使用 pythons counter
或者正则表达式更有意义?
例如我有
words = [{'word':'achieve','misspellings': ['acheive','acheiv','achiev']},
{'word':'apparently','misspellings':['apparantly','apparintly']}]
我想查看一个字符串列表,最后得到每个单词及其拼写错误的总数。我遇到像 achiev 这样的拼写错误的问题,当使用 if word in string
时会把计数弄乱,因为 achiev 在 achieve 中,所以计数会被关闭。
正则表达式可能是一个很好的工具 - word boundary anchors 可以帮助您避免单词中的子匹配。
对于每个单词,使用 wordre = re.compile(r"\b" + word + r"\b", re.I|re.U)
构建一个正则表达式,然后计算 re.findall(wordre, string)
的结果。
您应该将拼写错误的单词映射到原始单词:
words = {'acheive':'achieve', 'achiev':'achieve','achieve':'achieve'}
s = "achiev acheive achieve"
from collections import Counter
from string import punctuation
cn = Counter()
for word in s.split():
word = word.strip(punctuation)
if word in words:
wrd = words[word]
cn[wrd] += 1
print(cn)
Counter({'achieve': 3})
您可以将它与正则表达式结合起来查找字符串中的所有单词,而不是按照每个单词进行拆分
要计算拼写错误和原始拼写,只需检查单词字典查找返回的值是否等于单词,如果是,则更新单词的原始计数,否则更新未命中计数:
words = {'acheive':'achieve', 'achiev':'achieve','achieve':'achieve',
'apparently':'apparently','apparantly':'apparently','apparintly':'apparently'}
s = "achiev acheive achieve! 'apparently' apparintly 'apparantly?""
from collections import defaultdict
from string import punctuation
cn = defaultdict(lambda:{"orig": 0 ,"miss":0})
for word in s.split():
word = word.strip(punctuation)
if word in words:
wrd = words[word]
if wrd == word:
cn[wrd]["orig"] += 1
else:
cn[wrd]["miss"] += 1
print(cn)
defaultdict(<function <lambda> at 0x7f001fb2a8c0>,
{'apparently': {'miss': 2, 'orig': 1}, 'achieve': {'miss': 2, 'orig': 1}})