【问题标题】:Error while using unnest_tokens() while passing a function to the token将函数传递给令牌时使用 unnest_tokens() 时出错
【发布时间】:2020-03-23 16:56:57
【问题描述】:

unnest_tokens.data.frame(., entity, text, token = tokenize_scispacy_entities, 中的错误: 标记函数的预期输出是长度为 100 的列表

unnest_tokens() 适用于少数观察的样本,但在整个数据集上失败。

https://github.com/dgrtwo/cord19 可重现的例子:

library(dplyr)
library(cord19)
library(tidyverse)
library(tidytext)
library(spacyr)

从这里安装模型 - https://github.com/allenai/scispacy

spacy_initialize("en_core_sci_sm")

tokenize_scispacy_entities <- function(text) {
  spacy_extract_entity(text) %>%
    group_by(doc_id) %>%
    nest() %>%
    pull(data) %>%
    map("text") %>%
    map(str_to_lower)
}

paragraph_entities <- cord19_paragraphs %>% 
  select(paper_id, text) %>%
  sample_n(10) %>% 
  unnest_tokens(entity, text, token = tokenize_scispacy_entities)

【问题讨论】:

  • @phiver - 用一个可重现的例子更新了问题。
  • 看起来像 unnest_tokens 期望返回的样本数。但是查看 spacyr 的文档,它应该反过来工作。首先调用spacy_parse,然后使用tidytext 中的unnest_tokens
  • 我猜,spacy_extract_entity() 在后台做着完全相同的事情——解析然后转换成实体。无论如何 - 我仍然有我之前提到的问题。
  • 如果我使用任何其他功能,它工作得很好。我将更好地查看您的函数以查看它返回的内容。我将 janeaustenr 包作为测试集,因为我没有 cord19。应该对测试没有影响。

标签: r tokenize spacy tidytext


【解决方案1】:

我面临同样的问题。我不知道为什么,在我过滤掉空的抽象和较短的抽象字符串后,一切似乎都很好。

 abstract_entities <- article_data %>%
  filter(nchar(abstract) > 30) %>%
  select(paper_id, title, abstract) %>%
  sample_n(1000) %>%
  unnest_tokens(entity, abstract, token =  tokenize_scispacy_entities)

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多