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

2024-05-11

我有一个文本文档。我需要找到整个文档中重复单词对的可能数量。例如,我有下面的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. 创建 2 个元素的组合,每个元素使用itertools.combinations (flatMap将每个单词与行中的每个其他单词配对)
  4. 像字数统计一样进行映射和缩减
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 pyspark 计算所有可能的单词对 的相关文章

随机推荐

  • Android 预安装检测

    我的 Android 应用程序将被预安装 我想继续跟踪预安装的应用程序 为此 我需要以某种方式保存密钥或标志 这意味着该应用程序是预安装的 我会将此密钥添加到后端的每个请求中并对其进行分析 我对此有疑问 有一个问题是关于从 Google P
  • Windows 上本机 C++ 应用程序中的自动死代码检测?

    背景 我有一个用原生 C 编写的应用程序 花了几年的时间 大约有 60 KLOC 有很多函数和类已经死了 可能有 10 15 就像下面提出的类似的基于 Unix 的问题 我们最近开始对所有新代码进行单元测试 并尽可能将其应用于修改后的代码
  • 使用 dateTimePicker 在 DataGridView 中编辑日期

    我有一个DateTime我的 WinForms 中的专栏DataGridView 目前只能通过手动输入日期来编辑该字段 例如 2010 09 02 需要什么才能拥有一个DateTimePicker 或同等 用作编辑器 DataGridVie
  • 在Python中通过sys.stdout写入unicode字符串

    暂时假设一个人无法使用print 从而享受自动编码检测的好处 所以这给我们留下了sys stdout 然而 sys stdout太蠢了不做任何合理的编码 http bugs python org issue4947 现在人们阅读 Pytho
  • 如何使用Android Invalidate()

    在我的主要活动中 我定义了两个视图和一个菜单 浏览次数 1 自定义视图游戏 2 按钮btn 菜单 1 打开项目用于打开文件 菜单布局在不同的活动中定义 基本上 当主活动启动时 它会绘制没有任何内容的自定义视图和按钮 然后我使用菜单中的 打开
  • 从 Qthread 更新 Python GUI 元素

    所以我知道有很多关于使用 Qthread 更新 GUI 中的元素的帖子 我尽了最大努力去检查这些 但仍然有一个问题 我正在尝试创建一个 GUI 该 GUI 在单击按钮时运行一个方法 然后该方法启动一个新线程 然后该线程向 GUI 发出信号以
  • 为什么我可以使用 tsql 连接到 Azure MS SQL,但不能使用 pymssql?

    我今天在哪里 TDSVER 7 3 tsql H example database windows net U me D ExampleDB p 1433 P notreallymypassword 这不会 gt gt gt import
  • Hudson 结帐卡在“git fetch”处

    我正在使用 git 版本 1 6 2 2 1669 g7eaf8 在 Hudson 1 314 上使用 Hudson Git 插件 0 7 3 当我触发构建时 Hudson 执行 git fetch 但它永远不会返回 我把一只卡在那里14天
  • 从 C# 调用时无法识别 Powershell 命令

    这是这个的延续Question https stackoverflow com questions 66280000 powershell object returns null 66280138 noredirect 1 comment1
  • Android studio 问题:找不到广告:AdQuality:未指定

    我已经更新了 Android studio 刚刚打开我的项目 我收到以下错误 您能让我知道如何解决这个问题吗 Error A problem occurred configuring project memoryGameCollection
  • 在 python 中使用 re.sub 将字母变成大写?

    在许多编程语言中 以下内容 find foo a z bar并替换为GOO U 1GAR 将导致整个匹配项变为大写 我似乎无法在 python 中找到等效项 它存在吗 您可以将函数传递给re sub http docs python org
  • 如何通过sparkSession向worker提交多个jar?

    我使用的是火花2 2 0 下面是我在 Spark 上使用的 java 代码片段 SparkSession spark SparkSession builder appName MySQL Connection master spark ip
  • 附加之前检查数据库中是否存在 ID

    我通过选择一个带有类别的数组json decode并将它们附加到文章中 public static function setArticleCategory Request request article Article where id r
  • 将许多表转换为 Excel 列

    我创建了用于文章审阅的网络应用程序 我有一个名为 Article 的表 每个表Article有一些ArticleReview Article ArticleId ArticleTitle NumberOfComment NumberOfVi
  • 我应该增强客户端上的 Jquery Mobile 元素还是发送带有 data-enhance="false" 的增强标记?

    我有一个产品搜索 我正在发送回结果 每个结果都包含两个按钮 JQM 控制组 我一次发送 24 条记录 因此需要增强 24 个控制组 如下所示 div class submitButton linkBox div
  • 如何禁用基于 ValidationRule 类的按钮?

    如何禁用基于 ValidationRule 类的 WPF 按钮 下面的代码可以很好地突出显示 TextBox
  • 在 C# .NET 中对非 ASCII 字符进行编码

    我想向我的应用程序发送的电子邮件添加自定义标头 标头名称只能包含 ASCII 字符 但对于值和用户可能会输入 UTF 8 字符 我必须对它们进行 Base64 编码 此外 我还必须将它们解码回 UTF 8 以便在 UI 中向用户显示它们 最
  • 复制电子表格也会复制所有链接的文件

    当我使用库方法时 我希望能够仅复制电子表格及其所有工作表以及所有定义的工作表名称 spreadSheet copy newSSName Or myFile makeCopy newNameOfFile 目前 这些方法复制所有链接的表单和表单
  • 呈现 UIActivityViewController 时发出警告

    当我提出一个UIActivityController使用我得到的下面的代码 它被呈现 但控制台显示 Warning Attempt to present
  • 使用 pyspark 计算所有可能的单词对

    我有一个文本文档 我需要找到整个文档中重复单词对的可能数量 例如 我有下面的word文档 该文档有两行 每行用 分隔 文档 My name is Sam My name is Sam My name is Sam My name is Sa