python - 显示单词不同表示的问题
问题描述
我正在使用 PySpark 进行字数统计,其中我需要处理的任务之一是显示一个单词的所有表示形式(即 apple、Apple、aAPPLe 等)
目前,我的代码只显示单词的小写版本,但它计算所有表示。我知道这是因为我在开始时将文本文件中的所有单词都小写了,但我不确定如何更改它以便我的 wordReps 将显示所有表示。
hussle = sc.textFile("SOME_TEXT_FILE")
def removePunc(x):
bars = x.encode('utf-8')
lowerBars = bars.lower()
cleanBars = lowerBars.translate(None, string.punctuation)
return cleanBars
words = hussle.flatMap(lambda x: removePunc(x).split())
total_words = words.count()
wordCount = words.map(lambda x: (x,1)).reduceByKey(lambda x,y: x + y).map(lambda x:(x[0],x[1],x[1]/float(total_words))).sortByKey(False)
wordReps = words.map(lambda word: (word.lower(), word)).reduceByKey(lambda word1, word2: word1 + " " + word2 if word2 not in word1 else word1)
wordsFinal = wordCount.join(wordReps)
我的预期输出应该是这样的:
'all' (4, 'all All aLL')
而是,我得到:
'all' (4, 'all')
解决方案
推荐阅读
- javascript - preact-cli/babel/typescript - 让 Symbol.iterator 和 [...spread] 正常工作
- flutter - 我真的,甚至不知道如何为这个问题提出问题
- java - PreAuthorize 在 Spring Security 中不起作用
- python - Python 3 从体育网站提取 html 数据
- python - “索引 14 超出轴 0 的范围,大小为 2”
- c# - ASP.NET core OData:将包含字典的类序列化为 JSON,字典变成键值对数组。需要它是一个对象
- c# - 以窗口形式显示来自 CLIPS 的信息
- python - 如何在python中异步运行http请求
- stata - 将变量的值插入矩阵 - Stata
- node.js - Nodejs 在开发过程中崩溃导致连续的“EADDRINUSE:地址已在使用中”。解决方法?