python - 成对的两个结果词 pyspark
问题描述
我正在研究语言模型,并想计算两个后续单词的数量对。我在scala
whithslicing
函数上找到了一个这样的问题的例子。虽然我没有设法找到类比pyspark
data.splicing(2).map(lambda (x,y): ((x,y),1).redcueByKey(lambda x,y: x+y)
我想应该是这样的。解决方案可能是一个创建函数,可以在数组中找到下一个单词,但我想应该有一个内置解决方案。
解决方案
也许这会有所帮助。您可以在此处找到其他拆分方法:Is there a way to split a string by every nth separator in Python?
from itertools import izip
text = "I'm working on language model and want to count the number pairs of two consequent words.\
I found an examples of such problem on language model and want to count the number pairs"
i = iter(text.split())
rdd = sc.parallelize([" ".join(x) for x in izip(i,i)])
print rdd.map(lambda x: (x, 1)).reduceByKey(lambda x, y: x + y).collect()
[('找到一个', 1), ('计数', 2), ('想', 2), ('例子', 1), ('模型和', 2), ('论语', 2), ('数字对', 2), ("我在工作", 1), ('结果词.I', 1), ('这样的问题', 1), ('of two' , 1)]
推荐阅读
- android - 如何在android xml字符串资源中获取标签
- android - 在视图初始化时触发自身的所有 UI 组件(视图)中添加附加功能
- docker - 使用 docker engine api 加载 docker 镜像
- javascript - 使用没有包管理器的 Polymer Web 组件
- python - 如何用另一个数据框的一行的下一个元素填充数据框?
- php - 付款前如何在结帐页面获取订单ID
- javascript - 使用 $(document).ready() 和 $.getScript() 在页面加载完成后加载外部脚本
- php - 翻译器未加载正确的语言环境
- java - 为什么 FutureTask 内部不再使用 AQS?
- postgresql - 如何修复 SQL 错误 [42804]:错误:WHERE 的参数必须是布尔类型,而不是类型字符变化