Python词云词频统计函数未达预期要求,求定位遗漏问题
词云生成代码问题诊断与修复
原代码核心问题
- 标点处理逻辑完全错误:
split()拆分得到的单词很多会携带附着的标点(比如example,、hello!),原代码直接判断整个单词是否在标点列表中无法过滤这类附着标点,且对列表类型的words调用字符串replace()方法会直接触发语法报错。同时未清理标点就判断isalpha(),会把携带标点的有效单词直接过滤掉。 - 词频统计逻辑错误:首次出现的单词被赋值为0,仅第二次及之后出现才累加计数,所有单词的统计频率都会比实际值少1,首次出现的单词频率直接为0不会被词云渲染。
- 函数返回值不符合要求:需求明确要求
calculate_frequencies返回词频字典,原代码最终返回的是词云图片数组,不符合后续模块调用要求。
修正后的完整代码
import wordcloud def calculate_frequencies(file_contents): # 标点符号与停用词列表 punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~''' uninteresting_words = ["the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just", "in", "for", "so" ,"on", "says", "not", "into", "because", "could", "out", "up", "back", "about"] frequencies = {} words = file_contents.split() for item in words: # 统一转小写 item = item.lower() # 移除单词中所有标点符号 cleaned_item = ''.join([char for char in item if char not in punctuations]) # 过滤非全字母单词、停用词 if cleaned_item.isalpha() and cleaned_item not in uninteresting_words: # 累加词频 frequencies[cleaned_item] = frequencies.get(cleaned_item, 0) + 1 # 若需要直接生成词云可保留以下代码,否则直接返回frequencies即可符合输出字典的要求 # cloud = wordcloud.WordCloud() # cloud.generate_from_frequencies(frequencies) # return cloud.to_array() # 符合需求的返回值:词频字典 return frequencies
内容的提问来源于stack exchange,提问作者madmulchr
相关产品推荐
相关产品推荐

