两个后接单词对pysp

2024-06-26 11:00:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究语言模型,想计算两个结果词的对数。 我在scala函数中发现了这样一个问题的例子。虽然我没能在pyspark中找到这个类比

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

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


Tags: 方法lambda函数模型语言mapdata对数
1条回答
网友
1楼 · 发布于 2024-06-26 11:00:04

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

[('found an', 1), ('count the', 2), ('want to', 2), ('examples of', 1), ('model and', 2), ('on language', 2), ('number pairs', 2), ("I'm working", 1), ('consequent words.I', 1), ('such problem', 1), ('of two', 1)]

相关问题 更多 >