【问题标题】:Suggest code improvements: Count instances of words while ignoring punctuation and common words建议代码改进:在忽略标点符号和常用词的情况下计算单词实例
【发布时间】:2017-09-13 15:36:12
【问题描述】:

该程序的这一点是计算单词的出现次数,同时忽略标点符号、冠词和连词。所需的输出是使用的前 15 个单词和使用的后 15 个单词的列表,但未显示它们的出现。我是初学者,任何帮助将不胜感激。谢谢!

# This program reads a text file, performs a content analysis
# and prints both a top 15 and a bottom 15 report

name = input('Enter name of file: ')

 # Clean Function
def clean(s):
    punctuations = ["!","@","#","$"]
    art_con = ['the','a','an','some','and','but','or','nor','for']
    for each in punctuations:
        s = s.replace(each,"")
    words = s.split()
    resultwords = [word for word in words if word.lower() not in art_con]
    result= ''.join(resultwords)
    return result

# Analyze Function
def analyze(name):
    print('Reading',name,'for analysis...')
    print('===========================')
    print('Creating content analysis dictionary...')
    r = open(name, 'r')
    s = r.read()
    result = clean(s)
    count = dict((x,result.count(x)) for x in set(result))
    print('Analysis complete!')
    print('===================')
    return count

count = analyze(name)

# turn dictionary into a list of tuples to sort
def function(count):
    list1 = []
    for key in count:
        t = (count[key],key)
        list1.append((t))
    list1.sort()
    result = [list1[i] for i in range(len(list1))]
    t15 = result[0:15]
    b15 = result[-15:0]
    print("The top 15 words are ",t15)
    print("The bottom 15 words are ",b15)

#Main Function
def main():
    count = analyze(name)
    function(count)
main() 

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。
  • 好的……你的问题是什么?

标签: python list file dictionary tuples


【解决方案1】:

我是一个初学者,任何帮助将不胜感激。

总的来说,代码看起来不错。 clean() 函数可以通过以下方式更快更简洁:1)在开始时将输入字符串小写,2)使用正则表达式提取单词而忽略标点符号,以及 3)消除常用词使用集差运算。

下面是一个粗略的介绍:

words = re.findall(r"[a-z\'\-]+", s.lower())
return set(words) - {'the','a','an','some','and','but','or','nor','for'}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多