【问题标题】:R unnest with Sentence start and end positions带有句子开始和结束位置的R unnest
【发布时间】:2018-02-23 15:29:02
【问题描述】:

R 新手。 我正在使用tidytext::unnest_tokens 将长文本分解为以下使用的单个句子

tidy_drugs <- drugstext.raw %>% unnest_tokens(sentence, Section, token="sentences")

所以我得到了一个 data.frame,其中所有句子都转换为行。

我想获取从长文本中未嵌套的每个句子的开始和结束位置。

这是一个长文本文件的示例。它来自药品标签。

<< *6.1 Clinical Trial Experience
  Because clinical trials are conducted under widely varying conditions, adverse reaction rates observed in clinical trials of a drug cannot be directly compared to rates in the clinical trials of another drug and may not reflect the rates observed in practice.
 The data below reflect exposure to ARDECRETRIS as monotherapy in 327 patients with classical Hodgkin lymphoma (HL) and systemic anaplastic large cell lymphoma (sALCL), including 160 patients in two uncontrolled single-arm trials (Studies 1 and 2) and 167 patients in one placebo-controlled randomized trial (Study 3).
 In Studies 1 and 2, the most common adverse reactions were neutropenia, fatigue, nausea, anemia, cough, and vomiting.*

想要的结果是一个包含三列的数据框

【问题讨论】:

  • 寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。
  • 在问题陈述中添加了更多内容。感谢您的帮助。

标签: r text-mining tidytext


【解决方案1】:

您可以使用来自stringrstr_locate 执行此操作。这很烦人,因为换行符和特殊字符会弄乱您搜索的正则表达式。在这里,我们首先使用str_replace_all 从输入文本中删除换行符,然后取消嵌套标记以确保保留原始文本并防止大小写更改。然后,我们创建一个新的正则表达式列,将特殊字符(此处为 ().)替换为正确转义的版本,并使用 str_locate 添加每个字符串的开头和结尾。

我没有得到与你相同的数字,但我从你的代码中复制了文本,它并不总是保留所有字符,而且你最终的 end 数字无论如何都小于 start

library(tidyverse)
library(tidytext)

raw_text <- tibble(section = "6.1 Clinical Trial Experience
  Because clinical trials are conducted under widely varying conditions, adverse reaction rates observed in clinical trials of a drug cannot be directly compared to rates in the clinical trials of another drug and may not reflect the rates observed in practice.
                   The data below reflect exposure to ARDECRETRIS as monotherapy in 327 patients with classical Hodgkin lymphoma (HL) and systemic anaplastic large cell lymphoma (sALCL), including 160 patients in two uncontrolled single-arm trials (Studies 1 and 2) and 167 patients in one placebo-controlled randomized trial (Study 3).
                   In Studies 1 and 2, the most common adverse reactions were neutropenia, fatigue, nausea, anemia, cough, and vomiting."
)

tidy_text <- raw_text %>%
  mutate(section = str_replace_all(section, "\\n", "")) %>%
  unnest_tokens(
    output = sentence,
    input = section,
    token = "sentences",
    drop = FALSE,
    to_lower = FALSE
    ) %>%
  mutate(
    regex = str_replace_all(sentence, "\\(", "\\\\("),
    regex = str_replace_all(regex, "\\)", "\\\\)"),
    regex = str_replace_all(regex, "\\.", "\\\\.")
  ) %>%
  mutate(
    start = str_locate(section, regex)[, 1],
    end = str_locate(section, regex)[, 2]
  ) %>%
  select(sentence, start, end) %>%
  print()
#> # A tibble: 3 x 3
#>   sentence                                                     start   end
#>   <chr>                                                        <int> <int>
#> 1 6.1 Clinical Trial Experience  Because clinical trials are ~     1   290
#> 2 The data below reflect exposure to ARDECRETRIS as monothera~   310   626
#> 3 In Studies 1 and 2, the most common adverse reactions were ~   646   762

reprex package (v0.2.0) 于 2018 年 2 月 23 日创建。

【讨论】:

  • Calum,惊人的帮助!!。非常欣赏!!当我按原样运行您的代码时,您知道为什么会出现以下错误 - stri_locate_first_regex(string, pattern, opts_regex = opts(pattern)) 中的错误:正则表达式模式中的括号嵌套不正确。 (U_REGEX_MISMATCHED_PAREN)
  • 我不知道,我做了一个可重现的例子。您是否完全复制了我的代码?因为您没有以易于重现的方式(包括星号、任意换行符、打开
  • Calum,是的,我完全复制了代码,包括 raw_text。我正在尝试一些事情,会告诉你。感谢您的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多