如果我有以下示例:
library(text2vec)
library(magrittr)
reviews <- movie_review[1:10,]
vocabInsomnia <- reviews$review %>% itoken(tolower, word_tokenizer, n_chunks = 10) %>%
create_vocabulary %>%
prune_vocabulary(term_count_min = 10, doc_proportion_max = 0.5) %>%
vocab_vectorizer %>%
create_dtm(<output_from_itoken>,<output_from_vocab_vectorizer>)
您可以看到,在最后一個鏈序列中,我想使用前面兩個步驟的輸出作為函式的引數create_dtm。我只知道如何在輸出之前直接輸入鏈的輸出,而不是序列中第一個鏈vocab_vectorizer的函式的輸出。itokenmagrittr 允許這樣做嗎?
uj5u.com熱心網友回復:
我們可以使用創建一個臨時物件 pipeR
library(text2vec)
library(pipeR)
library(magrittr)
reviews$review %>%
itoken(tolower, word_tokenizer, n_chunks = 10) %>>%
(~ tmp) %>%
create_vocabulary %>%
prune_vocabulary(term_count_min = 10, doc_proportion_max = 0.5) %>%
vocab_vectorizer %>%
create_dtm(tmp, .)
-輸出
10 x 6 sparse Matrix of class "dgCMatrix"
an so by are he br
1 2 4 . 2 9 8
2 . 1 1 . . .
3 1 . 6 7 . 2
4 4 1 3 2 . 4
5 2 . 1 1 . .
6 . . . . . .
7 1 3 . . . .
8 . 1 . . 2 .
9 . . . . 1 4
10 . . . . . 2
uj5u.com熱心網友回復:
我不知道是否有更清潔或更有效的方法來做到這一點,但我通常在這種情況下做的是將管道嵌套在最高級別,我需要從中提取輸入并在輸出中使用管道.繼續鏈。
library(text2vec)
library(magrittr)
reviews <- movie_review[1:10,]
vocabInsomnia <- reviews$review %>%
itoken(tolower, word_tokenizer, n_chunks = 10) %>%
create_dtm(., create_vocabulary(.) %>%
prune_vocabulary(term_count_min = 10, doc_proportion_max = 0.5) %>%
vocab_vectorizer())
vocabInsomnia
#> 10 x 6 sparse Matrix of class "dgCMatrix"
#> an so by are he br
#> 1 2 4 . 2 9 8
#> 2 . 1 1 . . .
#> 3 1 . 6 7 . 2
#> 4 4 1 3 2 . 4
#> 5 2 . 1 1 . .
#> 6 . . . . . .
#> 7 1 3 . . . .
#> 8 . 1 . . 2 .
#> 9 . . . . 1 4
#> 10 . . . . . 2
由reprex 包(v2.0.1)于 2022-01-18 創建
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/414682.html
標籤:
