如何将句子分成单词

时间:2017-12-18 22:36:31

标签: r words sentence

在r中,我目前正在使用会话数据集。目前的数据如下:

Mike, "Hello how are you"
Sally, "Good you"

我计划最终创建这个数据的文字云,并且需要它看起来像这样:

Mike, Hello
Mike, how
Mike, are
Mike, you
Sally, good
Sally, you

2 个答案:

答案 0 :(得分:2)

使用reshape2::melt可能是这样的吗?

# Sample data
df <- read.csv(text =
    'Mike, "Hello how are you"
    Sally, "Good you"', header = F)

# Split on words
lst <- strsplit(trimws(as.character(df[, 2])), "\\s");
names(lst) <- trimws(df[, 1]);

# Reshape into long dataframe 
library(reshape2);
df.long <- (melt(lst))[2:1];
#     L1 value
#1  Mike Hello
#2  Mike   how
#3  Mike   are
#4  Mike   you
#5 Sally  Good
#6 Sally   you

说明:在空格trimws的第二列中拆分尾随/前导空格(\\s)条目并存储在list中。从第一列中获取list条目名称,然后使用data.frame重塑为长reshape2::melt

我把它变成逗号分隔的data.frame直到你......

答案 1 :(得分:0)

使用标记器,例如通过tidytext::unnest_tokens

library(tidyverse)
library(tidytext)

dialogue <- read_csv(
    'Mike, "Hello how are you"
     Sally, "Good you"', 
    col_names = c('speaker', 'sentence')
)

dialogue %>% unnest_tokens(word, sentence)
#> # A tibble: 6 x 2
#>   speaker  word
#>     <chr> <chr>
#> 1    Mike hello
#> 2    Mike   how
#> 3    Mike   are
#> 4    Mike   you
#> 5   Sally  good
#> 6   Sally   you