使用 tidytext 以 txt 格式实现标记化
achieve tokenize in a txt format with tidytext
我正在尝试处理 tidytext,使用一个名为:texto_revision
的 .txt 文件,其结构如下:
# A tibble: 254 x 230
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 la expro~ de la tier~ ocur~ con frec~ dura~ el proc~ rapi~ de la urba~ en
2 como las difer~ en el moti~ del cons~ cons~ en esta~ unid~ y china afec~ la
3 las desig~ etnic~ en los patr~ de cons~ (pre~ de vest~ joye~ auto~ han sido obje~
4 este artic~ exami~ el impa~ de vari~ dife~ indi~ en la prop~ de los cons~ a
5 este artic~ inves~ la infl~ de los regi~ poli~ sobre la impo~
# ...
尝试使用unnest_tokens
格式时,代码如下:
library(tidytext)
texto_revision %>%
unnest_tokens(word, text)
我收到以下错误:
Error:
Error in check_input(x) :
Input must be a character vector of any length or a list of character
vectors, each of which has a length of 1.
为了尝试更正错误并继续进行标记化,我尝试使用以下代码将文本转换为数据框:
text_df <- as.data.frame(texto_revision)
但我仍然收到以下错误
Error in check_input(x) :
Input must be a character vector of any length or a list of character
vectors, each of which has a length of 1.
请注意 unnest_tokens 的语法是 "unnest_tokens([new column name],[reference column]." 您的 tibble/data 框架中似乎没有 "text" 列。下面是一个玩具示例来说明:
State <- as.character(c("SC is in the South","NC is in the south",
"NY is in the north"))
DF <- data.frame(State, stringsAsFactors = FALSE)
> DF
State
1 SC is in the South
2 NC is in the south
.....
DF %>% unnest_tokens(word,State)
word
1 sc
1.1 is
1.2 in
1.3 the
....
看起来你的文本已经被标记化了,所以你只需要融化数据框就可以得到你想要的数据结构.例如,
library(tidyverse)
texto_revision %>%
gather(document, word)
请参阅 docs tidyr::gather()
。
我正在尝试处理 tidytext,使用一个名为:texto_revision
的 .txt 文件,其结构如下:
# A tibble: 254 x 230
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 la expro~ de la tier~ ocur~ con frec~ dura~ el proc~ rapi~ de la urba~ en
2 como las difer~ en el moti~ del cons~ cons~ en esta~ unid~ y china afec~ la
3 las desig~ etnic~ en los patr~ de cons~ (pre~ de vest~ joye~ auto~ han sido obje~
4 este artic~ exami~ el impa~ de vari~ dife~ indi~ en la prop~ de los cons~ a
5 este artic~ inves~ la infl~ de los regi~ poli~ sobre la impo~
# ...
尝试使用unnest_tokens
格式时,代码如下:
library(tidytext)
texto_revision %>%
unnest_tokens(word, text)
我收到以下错误:
Error: Error in check_input(x) : Input must be a character vector of any length or a list of character vectors, each of which has a length of 1.
为了尝试更正错误并继续进行标记化,我尝试使用以下代码将文本转换为数据框:
text_df <- as.data.frame(texto_revision)
但我仍然收到以下错误
Error in check_input(x) : Input must be a character vector of any length or a list of character vectors, each of which has a length of 1.
请注意 unnest_tokens 的语法是 "unnest_tokens([new column name],[reference column]." 您的 tibble/data 框架中似乎没有 "text" 列。下面是一个玩具示例来说明:
State <- as.character(c("SC is in the South","NC is in the south",
"NY is in the north"))
DF <- data.frame(State, stringsAsFactors = FALSE)
> DF
State
1 SC is in the South
2 NC is in the south
.....
DF %>% unnest_tokens(word,State)
word
1 sc
1.1 is
1.2 in
1.3 the
....
看起来你的文本已经被标记化了,所以你只需要融化数据框就可以得到你想要的数据结构.例如,
library(tidyverse)
texto_revision %>%
gather(document, word)
请参阅 docs tidyr::gather()
。