首页 > 解决方案 > 成对的两个结果词 pyspark

问题描述

我正在研究语言模型,并想计算两个后续单词的数量对。我在scalawhithslicing函数上找到了一个这样的问题的例子。虽然我没有设法找到类比pyspark

data.splicing(2).map(lambda (x,y): ((x,y),1).redcueByKey(lambda x,y: x+y)

我想应该是这样的。解决方案可能是一个创建函数,可以在数组中找到下一个单词,但我想应该有一个内置解决方案。

标签: pythonapache-sparkpyspark

解决方案


也许这会有所帮助。您可以在此处找到其他拆分方法: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)]


推荐阅读