r - 从R中的字符串创建词袋
问题描述
我找到了许多词袋的实现,但仍然找不到简单的长字符串的简单实现。我的结果是这样的:
word1: 56
word2: 31
word:X 7
我的库有问题,qdap
因为 in 在我的 R 上不起作用...
解决方案
由于大小写和标点符号,使用类似的东西strsplit
可能不会完全符合您的要求。包tokenizers
是tidytext
.
library(tokenizers)
text <- "this is some random TEXT is string 45 things and numbers and text!"
table(tokenize_words(text))
45 and is numbers random some string text things this
1 2 2 1 1 1 1 2 1 1
如果您只是按空格分割,请注意差异。
table(strsplit(text, " "))
45 and is numbers random some string TEXT text! things this
1 2 2 1 1 1 1 1 1 1 1
如果你走这条路,你可能只想完全跳到tidytext
.
library(dplyr)
library(tidytext)
library(tibble)
df <- tibble(string = text)
df %>%
unnest_tokens(word, string) %>%
count(word)
# A tibble: 10 x 2
word n
<chr> <int>
1 45 1
2 and 2
3 is 2
4 numbers 1
5 random 1
6 some 1
7 string 1
8 text 2
9 things 1
10 this 1
推荐阅读
- python-3.x - 如何将数据标题和数据本身拆分到各自的列?
- sql-server - SSIS 包中使用的 send_DbMail 中的希伯来语字符集问题
- ios - 粘贴长文本时UITextView动态高度滚动不起作用
- java - jaas.conf 中未提取 KafkaClient 部分
- javascript - Potree Viewer 中具有正交模式的 EDL 着色器
- c# - 注册表使用 C# 以编程方式获取目录所有者
- angular - Angular4 +表单控件的无值访问器
- python - 内存错误是系统错误的直接原因
- python - 为什么从我的 csv 文件创建的 Numpy 数组中充满了 NaN 值?
- sql - 使用数据库中的数据禁用按钮