文本挖掘 - 将文本拆分为单个观察

时间:2018-01-25 02:54:49

标签: r text-mining data-cleaning

我有一个唯一ID的数据集和每个ID的句子。我想用语言分解句子并删除停用词来清理数据以便进一步分析。

Example of dataset: 
ID  Sentence
1  The quick brown fox 
2  Feel free to be

Breaking up sentence: 
ID  Word 
1  The 
1  quick 
1  brown 
1  fox 
2  Feel 
2  free 
2  to 
2  be 

Removing the stopwords: 
ID  Word
1  quick 
1  brown 
1  fox 
2  Feel 
2  free

我已经拥有数据框中的ID和句子。什么是合适的功能来分解文本,包括删除每个单词后的标点符号,然后删除带有停用词的行。

2 个答案:

答案 0 :(得分:2)

使用tidytext包,您可以执行以下操作。该软件包有停用词。您需要调用数据。然后,您将unnest_tokens()应用于文本列。您需要指定两个名称。一个用于目标列,另一个用于输出中的新列。一旦你分开了句子,你就可以对数据进行子集化。我在filter()包中使用了dplyr

library(dplyr)
library(tidytext)

foo <- data.frame(ID = c(1, 2),
                  Sentence = c("The quick brown fox", "Feel free to be"),
                  stringsAsFactors = FALSE)

data(stop_words)

unnest_tokens(foo, input = Sentence, output = word) %>%
filter(!word %in% stop_words$word)

  ID  word
1  1 quick
2  1 brown
3  1   fox
4  2  feel
5  2  free

答案 1 :(得分:0)

  A=read.table(text="ID  Sentence
    1  'The quick brown fox' 
    2  'Feel free to be'",h=T,stringsAsFactors=F)


(dat=rev(stack(setNames(strsplit(A$Sentence," "),1:2))))

  ind values
1   1    The
2   1  quick
3   1  brown
4   1    fox
5   2   Feel
6   2   free
7   2     to
8   2     be


dat[-grep("The|to|be",dat$values),]
  ind values
2   1  quick
3   1  brown
4   1    fox
5   2   Feel
6   2   free

或者你可以这样做:

 dat[!dat$values%in%stop_words$word,TRUE),]
相关问题