【发布时间】:2018-12-16 21:57:53
【问题描述】:
应用phrasemachine() 后,我有一个包含短语的嵌套列表。现在我想创建一个文档特征矩阵,其中第一列中的文档(用户)和所有特征作为剩余列,每个用户在单元格中的使用频率。
library(rJava)
library(phrasemachine)
library(quanteda)
#creating dummy data
id <- c(1:2)
text <- c("Election day is coming up and I am super excited. Election day. Wooho. I voted President Obama.", "School is boring. Partying is cool. Happy Birthday to me. When is Election Day?")
test <- data.frame(id, text)
test$text <- as.character(test$text)
corpus_test <- corpus(test[["text"]], docnames = test[["id"]])
tokens_test <- tokens(corpus_test)
phrases_test <- phrasemachine(tokens_test, minimum_ngram_length = 2, maximum_ngram_length = 3, return_phrase_vectors = TRUE, return_tag_sequences = TRUE)
phrases_test
# > phrases_test
# [[1]]
# [[1]]$phrases
# [1] "Election_day" "Election_day" "President_Obama"
#
# [[1]]$tags
# [1] "NN" "NN" "NN"
#
#
# [[2]]
# [[2]]$phrases
# [1] "Happy_Birthday" "Election_Day"
#
# [[2]]$tags
# [1] "AN" "NN"
这是我正在寻找的输出(文档特征矩阵):
# user Election_day President_Obama Happy_Birthday
# 1 2 1 0
# 2 1 0 1
我尝试使用lapply,但由于每个用户的短语的维度不同,这不起作用。
这是我尝试过的:
library(plyr)
phrases_user <- laply(phrases_test, function(x) laply(x, identity)) #Error: Results must have the same dimensions.
library(dplyr)
phrases_user <- lapply(phrases_test, `[`, "phrases")
在找出每个 ID 提取短语的问题后,我想我必须执行以下操作:
corpus_test_2 <- corpus(phrases_user[["phrases"]], docnames = phrases_user[["id"]])
dfm_test <- dfm(corpus_test_2)
有人可以帮忙吗? :)
【问题讨论】:
标签: r dplyr nlp plyr nested-lists