【问题标题】:Python word counter sensitive to if word is surrounded by quotation marks?Python单词计数器是否对单词是否用引号引起来敏感?
【发布时间】:2019-03-31 16:40:04
【问题描述】:

我的 Python 程序有问题。我正在尝试做一个单词计数器,来自Exercism 的练习。

现在,我的程序必须通过 13 个测试,所有这些测试都是包含空格、字符、数字等的不同字符串。 我曾经有一个问题,因为我会用空格替换所有非字母和非数字。这给"don't" 之类的词带来了问题,因为它会将其分成两个字符串,dont。为了解决这个问题,我添加了一个if 语句,不包括单个' 标记被替换,这很有效。

但是,我必须测试的字符串之一是"Joe can't tell between 'large' and large."。问题是由于我排除了' 市场,这里large'large' 被认为是两个不同的东西,它们也是同一个词。如何告诉我的程序“擦除”一个单词环绕的引号?

这是我的代码,我添加了两个场景,一个是上面的字符串,另一个是另一个字符串,只有一个 ' 标记,你不应该删除:

def word_count(phrase):
    count = {}
    for c in phrase:
        if not c.isalpha() and not c.isdigit() and c != "'":
            phrase = phrase.replace(c, " ")
    for word in phrase.lower().split():
        if word not in count:
            count[word] = 1
        else:
            count[word] += 1
    return count

print(word_count("Joe can't tell between 'large' and large."))
print(word_count("Don't delete that single quote!"))

感谢您的帮助。

【问题讨论】:

标签: python string python-3.x counter


【解决方案1】:

模块string 包含一些不错的文本常量- 对您来说很重要的是punctuation。模块collections holds Counter - 一个专门用于计算事物的字典类:

from collections import Counter 
from string import punctuation

# lookup in set is fastest 
ps = set(string.punctuation)  # "!#$%&'()*+,-./:;<=>?@[\]^_`{|}~

def cleanSplitString(s):
    """cleans all punctualtion from the string s and returns split words."""
    return ''.join([m for m in s if m not in ps]).lower().split()

def word_count(sentence):
    return dict(Counter(cleanSplitString(sentence))) # return a "normal" dict

print(word_count("Joe can't tell between 'large' and large.")) 
print(word_count("Don't delete that single quote!"))

输出:

{'joe': 1, 'cant': 1, 'tell': 1, 'between': 1, 'large': 2, 'and': 1}
{'dont': 1, 'delete': 1, 'that': 1, 'single': 1, 'quote': 1}

如果要将标点符号保留在单词中,请使用:

def cleanSplitString_2(s):
    """Cleans all punctuations from start and end of words, keeps them if inside."""
    return [w.strip(punctuation) for w in s.lower().split()] 

输出:

{'joe': 1, "can't": 1, 'tell': 1, 'between': 1, 'large': 2, 'and': 1}
{"don't": 1, 'delete': 1, 'that': 1, 'single': 1, 'quote': 1} 

Readup on strip()

【讨论】:

    【解决方案2】:

    使用.strip() 删除列表中的第一个和最后一个字符 - https://python-reference.readthedocs.io/en/latest/docs/str/strip.html

    def word_count(phrase):
        count = {}
        for c in phrase:
            if not c.isalpha() and not c.isdigit() and c != "'":
                phrase = phrase.replace(c, " ")
        print(phrase)
        for word in phrase.lower().split():
            word = word.strip("\'")
            if word not in count:
                count[word] = 1
            else:
                count[word] += 1
        return count
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 2017-09-19
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多