java - 每行的词袋

标签 java csv hadoop

我目前有一个巨大的 csv 文件。其中包含 reddit 帖子标题。我想为每个帖子创建一个特征向量。

假设帖子图 block 是“to be or not to be”,它属于“some_category”。 csv 文件采用以下格式。

"some_category1", "some title1"

"some_category2", "some title2"

"some_category1", "some title3"

我想创建一个特征向量,如下所示。

"some_category" : to(2) be(2) or(1) not(1).

我需要在 hadoop 上完成这一切。我卡在了第一步,如何将每一行转换为特征向量(我觉得它类似于字数统计,但我如何将它应用于每一行)。

我对这个问题的最初想法是每一行的关键(即每个帖子的标题和类别)是帖子的类别,值是标题的特征向量(即标题的字数)。

感谢任何有关如何解决此问题的帮助。

最佳答案

回答你的第一部分: Reading a csv linewise in Hadoop 已在这篇文章中得到解答:StackOverflow:how-to-read-first-line-in-hadoop-hdfs-file-efficiently-using-java . 只需将最后一行更改为:

final Scanner sc = new Scanner(input);
while (sc.hastNextLine()) {
    //doStuff with sc.nextLine()!
}

要创建特征向量,我会使用您提到的计数策略:

/**
* We will use Java8-Style to do that easily
* 0) Split each line by space separated (split("\\s")
* 1) Create a stream: Arrays.stream(Array)
* 2) Collect the input (.collect) and group it by every identical word (Function.identity) to the corresponding amount (Collectors.counting)
*
* @param title the right hand side after the comma
* @return a map mapping each word to its count
**/
private Map<String, Long> createFeatureVectorForTitle(String title) {
    return Arrays.stream(title.split("\\s").collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}

您将每个类别键控到创建的特征向量的想法听起来很合理。虽然我不太熟悉 Hadoop,但也许有人可以指出更好的解决方案。

关于java - 每行的词袋,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35426004/

相关文章:

caching - 如何从 hadoop 缓存图像以及如何隐藏 url 中给定的端口号

c# - 在 linux/osx 机器上使用 c# 进行 hadoop 流式传输

java - Apache 通用 CSVParser/CSVRecord 为空字段返回 null

selenium - 使用robotframework-selenium时如何测试空白文本字段?

Python:获取给定文件夹中特定文件类型的名称

csv - 如何不显示/导出 CSV 文件导出的第一行-csv、Azure、powershell、

hadoop - 我可以通过Hive客户端从Hive表中读取数据。但我看不懂talend之类的工具

java - 如何在 JFrame 中动态添加 JPanel?

java - 对于两个整数用户输入,如何检测第一个输入是否传递了字符串?

java - 我应该使用什么直接 FK 字段或实体对象?