使用 AlgorithmStar and Spark 实现 词频统计

使用 AlgorithmStar and Spark 实现 词频统计

数据分析案例

(AS 算法之星 机器学习库)(Spark 分布式计算 框架)实现 词频 统计 数据分析 的 案例

目录

在这里插入图片描述

AlgorithmStar 实现词频统计

AlgorithmStar 是相较于Spark来说,使用起来比较简单的框架,API非常简洁,推荐使用Java语言调用,下面开始进行一个实现。

代码

在下面的代码中,每个步骤都进行了注释,同时在结尾处演示了两种打印方法。

package com.zhao;

import zhao.algorithmMagic.algorithm.featureExtraction.WordFrequency;
import zhao.algorithmMagic.core.AlgorithmStar;
import zhao.algorithmMagic.operands.matrix.ColumnIntegerMatrix;

/**
 * @author 赵凌宇
 */
public class MAIN {
    public static void main(String[] args) {
        // 准备一个句子
        final String data = "The next step is to demonstrate some operations related to word frequency statistics. You will learn about the relevant operations of word frequency statistics in different frameworks";
        // 获取到算法之星门户类 这里的泛型中 第二个参数代表的就是计算组件的计算结果类型
        // 词频向量计算之后返回的是个 ColumnIntegerMatrix 矩阵
        // 所以 第二个 泛型就要设置为 ColumnIntegerMatrix
        final AlgorithmStar<Object, ColumnIntegerMatrix> instance = AlgorithmStar.getInstance();
        // 开始转换成为词频向量
        final ColumnIntegerMatrix wordCount = instance.extract(WordFrequency.getInstance("wordCount"), data);
        // 查看词频向量 这个操作会使用默认的方式打印出矩阵格式
        System.out.println(wordCount);
        // 获取到其中的值 词频向量在这里计算完毕之后是个带列与行名的 ColumnIntegerMatrix,不过其中只有一行数值
        // 列名代表的就是被统计单词
        // 行名代表的就是被统计的句子
        // 在这里我们需要先获取到列名
        final String[] rowFieldNames = wordCount.getRowFieldNames();
        for (int i = 0; i < wordCount.getRowCount(); i++) {
            // 然后才可以在循环中 使用索引 定位到当前这列的第一个元素(这个就是被统计好的数值)与列名(当前统计的数量对应的单词)关联起来
            System.out.println("出现次数:" + wordCount.get(i, 0) + "\t被统计单词:" + rowFieldNames[i]);
        }
    }
}

代码计算结果

在计算结果中 我们使用两种方式打印出了词频结果,可以看到这里的结果还是蛮清晰的。

------------IntegerMatrixStart-----------
The next step is to demonstrate some operations related to word frequency statistics. You will learn about the relevant operations of word frequency statistics in different frameworks	rowColName
[1]	next
[1]	some
[1]	will
[1]	learn
[1]	in
[1]	frameworks
[1]	about
[1]	is
[2]	frequency
[1]	The
[1]	the
[1]	relevant
[2]	operations
[1]	related
[1]	of
[1]	step
[2]	to
[1]	demonstrate
[1]	different
[2]	word
[1]	You
[2]	statistics
------------IntegerMatrixEnd------------

出现次数:1	被统计单词:next
出现次数:1	被统计单词:some
出现次数:1	被统计单词:will
出现次数:1	被统计单词:learn
出现次数:1	被统计单词:in
出现次数:1	被统计单词:frameworks
出现次数:1	被统计单词:about
出现次数:1	被统计单词:is
出现次数:2	被统计单词:frequency
出现次数:1	被统计单词:The
出现次数:1	被统计单词:the
出现次数:1	被统计单词:relevant
出现次数:2	被统计单词:operations
出现次数:1	被统计单词:related
出现次数:1	被统计单词:of
出现次数:1	被统计单词:step
出现次数:2	被统计单词:to
出现次数:1	被统计单词:demonstrate
出现次数:1	被统计单词:different
出现次数:2	被统计单词:word
出现次数:1	被统计单词:You
出现次数:2	被统计单词:statistics

进程已结束,退出代码0

Spark 实现词频统计

代码

package run

import org.apache.spark.{SparkConf, SparkContext}

object MAIN3 {

  def main(args: Array[String]): Unit = {
    // 准备一个句子
    val data = "The next step is to demonstrate some operations related to word frequency statistics. You will learn about the relevant operations of word frequency statistics in different frameworks"
    // 构造 Spark 门户类
    val sparkContext = new SparkContext(
      new SparkConf()
        // 在这里我们指定 Spark 计算使用的是本地计算
        .setMaster("local[*]")
        // 在这里我们指定 Spark 计算任务的名字
        .setAppName("wordCount")
    )
    // 将 句子 加载为 RDD
    val value = sparkContext.makeRDD(
      // 在这里我们指定按照 逗号拆分
      data.split(" ")
    )
    // 将拆分之后的单词开始进行计算
    val tempRes = value.mapPartitions(iter => {
      // iter 是当前分区中的单词集合
      for (word <- iter) yield {
        // 在这里我们将 iter 中的每个单词 word 依次迭代
        // 并在这里打标签 为每个单词出现的次数标记为 1
        (word, 1)
      }
    }
    ).groupByKey()
    // 到这里,我们已经实现了最基本的单词分组,每个单词都是 key 每个 key 中都对应了一个 与 key 单词数量相同的容器 容器中的元素都是1
    // 例如句子中有两个 operations 则 key 为 operations 对应的 容器中有两个元素为 1
    // 例如句子中有一个 is 则 key 为 is 对应的 容器中有一个元素为 1
    // 所以我们在这里直接将每个容器的尺寸做为 每个 key 出现的次数
    tempRes.map(wc => wc._1 -> wc._2.size).foreach(
      // 然后在这里进行打印
      wc => println("出现次数:" + wc._2 + "\t被统计单词:" + wc._1)
    )
  }
}

代码计算结果

出现次数:1	被统计单词:related
出现次数:1	被统计单词:statistics.
出现次数:2	被统计单词:operations
出现次数:1	被统计单词:relevant
出现次数:1	被统计单词:demonstrate
出现次数:1	被统计单词:next
出现次数:1	被统计单词:The
出现次数:1	被统计单词:learn
出现次数:1	被统计单词:in
出现次数:2	被统计单词:word
出现次数:1	被统计单词:the
出现次数:1	被统计单词:is
出现次数:1	被统计单词:different
出现次数:1	被统计单词:You
出现次数:1	被统计单词:some
出现次数:1	被统计单词:step
出现次数:1	被统计单词:of
出现次数:2	被统计单词:frequency
出现次数:2	被统计单词:to
出现次数:1	被统计单词:about
出现次数:1	被统计单词:frameworks
出现次数:1	被统计单词:will
出现次数:1	被统计单词:statistics

操作记录
作者:root
操作时间:2024-01-03 12:16:38 星期三
事件描述备注:保存/发布


参考文章:http://www.lingyuzhao.top/?/linkController=/articleController&link=-22966947

相关推荐

  1. Python词频统计

    2024-01-04 13:44:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-04 13:44:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-04 13:44:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-04 13:44:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-04 13:44:01       20 阅读

热门阅读

  1. 向爬虫而生---Redis 拓宽篇1 < pipeline传输效率>

    2024-01-04 13:44:01       40 阅读
  2. 《微信小程序开发从入门到实战》学习七十一

    2024-01-04 13:44:01       42 阅读
  3. 【已解决】k210模型烧录——kfpkg 打包

    2024-01-04 13:44:01       41 阅读
  4. C++静态函数指针的问题

    2024-01-04 13:44:01       39 阅读
  5. git版本管理

    2024-01-04 13:44:01       44 阅读
  6. 三种 SqlSession

    2024-01-04 13:44:01       42 阅读
  7. Python 遍历某文件夹下所有文件夹或文件

    2024-01-04 13:44:01       45 阅读
  8. python学习之一百实例收集

    2024-01-04 13:44:01       46 阅读
  9. 智能合约开发(1)

    2024-01-04 13:44:01       33 阅读
  10. 【数据结构】——期末复习题题库(4)

    2024-01-04 13:44:01       35 阅读
  11. git学习

    git学习

    2024-01-04 13:44:01      36 阅读