【问题标题】:Pairs of two consequent words pyspark成对的两个结果词 pyspark
【发布时间】:2018-05-20 02:44:52
【问题描述】:

我正在研究语言模型,想计算两个后续单词的对数。 我在scalaslicing 函数上找到了此类问题的示例。虽然我没能在pyspark中找到类比

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

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

【问题讨论】:

标签: python apache-spark pyspark


【解决方案1】:

也许这会有所帮助。您可以在此处找到其他拆分方法: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), ("我是 working", 1), ('consequent words.I', 1), ('这样的问题', 1), ('of 二', 1)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多