【问题标题】:counting words in "lines" tokens计算“行”标记中的单词
【发布时间】:2017-05-08 20:14:25
【问题描述】:

我是 R 的新手,所以这个问题似乎很明显。但是,我没有管理,也没有找到解决方案

当标记是行(实际上是评论)时,如何计算标记中的单词数? 因此,有一个数据集,其中评论(reviewText)与产品 ID(asin)相连

amazonr_tidy_sent = amazonr_tidy_sent%>%unnest_tokens(word, reviewText, token = "lines") amazonr_tidy_sent = amazonr_tidy_sent %>% anti_join(stop_words)%>%ungroup()

我尝试了以下方式

wordcounts <- amazonr_tidy_sent %>% group_by(word, asin)%>% summarize(word = n())

但这不合适。我认为,没有办法计算,因为作为标记的行不能“分离”

非常感谢

【问题讨论】:

  • amazonr_tidy_sent 长什么样子?
  • 两列:“asin”(例如,B000M341QE、B000J3OTO6 等)和“word”。 “word”列是使用unnest_tokens 标记为行的评论
  • 你能发帖dput(head(amazonr_tidy_sent, 10))吗?
  • structure(list(asin = "0764005693", reviewText = "the famous author peter neal is on a publicity tour through italy when a mysterious killer begins staging murders after those found in his most recent novel neal must partner with the police and follow a trail ... &lt;truncated&gt; "reviewText"), row.names = 1L, class = "data.frame")
  • 长度(strsplit(x, sep = " "))?也许使用 group_by 行文本。

标签: r tidyr tidytext


【解决方案1】:

如果适合您的分析,您可以多次使用unnest_tokens()

首先,您可以使用unnest_tokens() 来获取您想要的行。请注意,我正在添加一列来跟踪每行的 id;您可以随意调用它,但重要的是要有一个列来记录您所在的行。

library(tidytext)
library(dplyr)
library(janeaustenr)


d <- data_frame(txt = prideprejudice)

d_lines <- d %>%
    unnest_tokens(line, txt, token = "lines") %>%
    mutate(id = row_number())

d_lines

#> # A tibble: 10,721 × 2
#>                                                                        line
#>                                                                       <chr>
#>  1                                                      pride and prejudice
#>  2                                                           by jane austen
#>  3                                                                chapter 1
#>  4  it is a truth universally acknowledged, that a single man in possession
#>  5                            of a good fortune, must be in want of a wife.
#>  6   however little known the feelings or views of such a man may be on his
#>  7 first entering a neighbourhood, this truth is so well fixed in the minds
#>  8 of the surrounding families, that he is considered the rightful property
#>  9                                 of some one or other of their daughters.
#> 10 "my dear mr. bennet," said his lady to him one day, "have you heard that
#> # ... with 10,711 more rows, and 1 more variables: id <int>

现在您可以使用unnest_tokens()再次,但这次使用words,这样您将获得每个单词的一行。请注意,您仍然知道每个单词来自哪一行。

d_words <- d_lines %>%
    unnest_tokens(word, line, token = "words")

d_words
#> # A tibble: 122,204 × 2
#>       id      word
#>    <int>     <chr>
#>  1     1     pride
#>  2     1       and
#>  3     1 prejudice
#>  4     2        by
#>  5     2      jane
#>  6     2    austen
#>  7     3   chapter
#>  8     3         1
#>  9     4        it
#> 10     4        is
#> # ... with 122,194 more rows

现在你可以做任何你想做的计数,例如,也许你想知道每行有多少单词?

d_words %>%
    count(id)

#> # A tibble: 10,715 × 2
#>       id     n
#>    <int> <int>
#>  1     1     3
#>  2     2     3
#>  3     3     2
#>  4     4    12
#>  5     5    11
#>  6     6    15
#>  7     7    13
#>  8     8    11
#>  9     9     8
#> 10    10    15
#> # ... with 10,705 more rows

【讨论】:

    【解决方案2】:

    通过使用str_split 分割每一行,我们可以计算每行的字数。

    一些示例数据(包含换行符和停用词):

    library(dplyr)
    library(tidytext)
    d = data_frame(reviewText = c('1 2 3 4 5 able', '1 2\n3 4 5\n6\n7\n8\n9 10 above', '1!2', '1',
                              '!', '', '\n', '1', 'able able', 'above above', 'able', 'above'),
               asin = rep(letters, each = 2, length.out = length(reviewText)))
    

    统计字数:

    by_line %>%
        group_by(asin) %>%
        summarize(word = sum(sapply(strsplit(word, '\\s'), length)))
    
       asin  word
      <chr> <int>
    1     a    17
    2     b     2
    3     c     1
    4     d     1
    5     e     4
    

    注意:在您的原始代码中,大多数停用词不会被删除,因为您按行拆分数据。只有恰好是单个停用词的行才会被删除。

    要从字数中排除停用词,请使用:

    by_line %>%
        group_by(asin) %>%
        summarize(word = word %>% strsplit('\\s') %>%
                      lapply(setdiff, y = stop_words$word) %>% sapply(length) %>% sum)
    
       asin  word
      <chr> <int>
    1     a    15
    2     b     2
    3     c     1
    4     d     1
    5     e     0
    6     f     0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2012-08-09
      • 1970-01-01
      相关资源
      最近更新 更多