python - 使用 pyspark 计算所有可能的单词对

标签 python apache-spark pyspark rdd

我有一个文本文档。我需要找到整个文档中重复单词对的可能数量。例如,我有下面的word文档。该文档有两行,每行用“;”分隔。 文档:

My name is Sam My name is Sam My name is Sam;
My name is Sam;

我正在研究配对词计数。预期结果是:

[(('my', 'my'), 3), (('name', 'is'), 7), (('is', 'name'), 3), (('sam', 'sam'), 3), (('my', 'name'), 7), (('name', 'sam'), 7), (('is', 'my'), 3), (('sam', 'is'), 3), (('my', 'sam'), 7), (('name', 'name'), 3), (('is', 'is'), 3), (('sam', 'my'), 3), (('my', 'is'), 7), (('name', 'my'), 3), (('is', 'sam'), 7), (('sam', 'name'), 3)]

如果我使用:

wordPairCount = rddData.map(lambda line: line.split()).flatMap(lambda x: [((x[i], x[i + 1]), 1) for i in range(0, len(x) - 1)]).reduceByKey(lambda a,b:a + b)

我得到连续单词的配对词及其重复出现的次数。

如何将行中的每个单词与其他单词配对,然后在所有行中搜索同一对?

有人可以看一下吗?谢谢

最佳答案

您的输入字符串:

# spark is SparkSession object
s1 = 'The Adventure of the Blue Carbuncle The Adventure of the Blue Carbuncle The Adventure of the Blue Carbuncle; The Adventure of the Blue Carbuncle;'

# Split the string on ; and I parallelize it to make an rdd
rddData = spark.sparkContext.parallelize(rdd_Data.split(";"))

rddData.collect()
# ['The Adventure of the Blue Carbuncle The Adventure of the Blue Carbuncle The Adventure of the Blue Carbuncle', ' The Adventure of the Blue Carbuncle', '']

import itertools

final = (
    rddData.filter(lambda x: x != "")
        .map(lambda x: x.split(" "))
        .flatMap(lambda x: itertools.combinations(x, 2))
        .filter(lambda x: x[0] != "")
        .map(lambda x: (x, 1))
        .reduceByKey(lambda x, y: x + y).collect()
)
# [(('The', 'of'), 7), (('The', 'Blue'), 7), (('The', 'Carbuncle'), 7), (('Adventure', 'the'), 7), (('Adventure', 'Adventure'), 3), (('of', 'The'), 3), (('the', 'Adventure'), 3), (('the', 'the'), 3), (('Blue', 'The'), 3), (('Carbuncle', 'The'), 3), (('Adventure', 'The'), 3), (('of', 'the'), 7), (('of', 'Adventure'), 3), (('the', 'The'), 3), (('Blue', 'Adventure'), 3), (('Blue', 'the'), 3), (('Carbuncle', 'Adventure'), 3), (('Carbuncle', 'the'), 3), (('The', 'The'), 3), (('of', 'Blue'), 7), (('of', 'Carbuncle'), 7), (('of', 'of'), 3), (('Blue', 'Carbuncle'), 7), (('Blue', 'of'), 3), (('Blue', 'Blue'), 3), (('Carbuncle', 'of'), 3), (('Carbuncle', 'Blue'), 3), (('Carbuncle', 'Carbuncle'), 3), (('The', 'Adventure'), 7), (('The', 'the'), 7), (('Adventure', 'of'), 7), (('Adventure', 'Blue'), 7), (('Adventure', 'Carbuncle'), 7), (('the', 'Blue'), 7), (('the', 'Carbuncle'), 7), (('the', 'of'), 3)]
  1. 删除第一个拆分中的所有空格
  2. 按空格分割 x,它是一个空格分隔的字符串
  3. 使用 itertools.combinations 创建 2 个元素的组合(flatMap 将每个单词与行中的每个其他单词配对)
  4. 像字数统计一样进行映射和缩减

关于python - 使用 pyspark 计算所有可能的单词对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58465089/

相关文章:

python - 序列化程序不从查询集中写入单个对象

mysql - 在 Apache Spark 2.0.0 中,是否可以从外部数据库获取查询(而不是获取整个表)?

hadoop - 比较 2 个配置单元表以查找没有任何唯一列/时间戳的更新/插入/删除记录并将其附加到 Hadoop 中的基表

python - PySpark:迭代 PairRDD 中的值

apache-spark - 将UDF应用于spark 2.0中的SparseVector列

python - 如何向 pandas.DataFrame 列(列表)添加新元素?

python - 当它是一个单独的词时替换一个子串

python - 如何使用 Scrapy 抓取 JSON 数据

apache-spark - 如何使用外部数据库(postgresql)作为流式查询的输入?

r - 在 sparklyr 中创建虚拟变量?