一次性进行 R POS 标记和标记化

标签 r tokenize pos-tagger tidytext

我有一段文字如下。

   Section <- c("If an infusion reaction occurs, interrupt the infusion.")
    df <- data.frame(Section)

当我使用 tidytext 和下面的代码进行标记时,

AA <- df %>%
  mutate(tokens = str_extract_all(df$Section, "([^\\s]+)"),
         locations = str_locate_all(df$Section, "([^\\s]+)"),
         locations = map(locations, as.data.frame)) %>%
  select(-Section) %>%
  unnest(tokens, locations)

它给了我 token 、开始和结束位置。如何在取消嵌套的同时获取 POS 标签。如下所示(下图中的 POSTags 可能不正确)

enter image description here

最佳答案

您可以使用包 udpipe 来获取您的 POS 数据。 Udpipe 自动标记标点符号。

Section <- c("If an infusion reaction occurs, interrupt the infusion.")
df <- data.frame(Section, stringAsFactors = FALSE)

library(udpipe)
library(dplyr)
udmodel <- udpipe_download_model(language = "english")
udmodel <- udpipe_load_model(file = udmodel$file_model)


x <- udpipe_annotate(udmodel, 
                     df$Section)
x <- as.data.frame(x)

x %>% select(token, upos)
       token  upos
1         If SCONJ
2         an   DET
3   infusion  NOUN
4   reaction  NOUN
5     occurs  NOUN
6          , PUNCT
7  interrupt  VERB
8        the   DET
9   infusion  NOUN
10         . PUNCT

现在将 previous question 的结果结合起来你问。我采纳了其中一个答案。

library(stringr)
library(purrr)
library(tidyr)

df %>% mutate(
  tokens = str_extract_all(Section, "\\w+|[[:punct:]]"),
  locations = str_locate_all(Section, "\\w+|[[:punct:]]"),
  locations = map(locations, as.data.frame)) %>%
  select(-Section) %>%
  unnest(tokens, locations) %>% 
  mutate(POS = purrr::map_chr(tokens, function(x) as.data.frame(udpipe_annotate(udmodel, x = x, tokenizer = "vertical"))$upos))

       tokens start end  upos
1         If     1   2 SCONJ
2         an     4   5   DET
3   infusion     7  14  NOUN
4   reaction    16  23  NOUN
5     occurs    25  30  NOUN
6          ,    31  31 PUNCT
7  interrupt    33  41  VERB
8        the    43  45   DET
9   infusion    47  54  NOUN
10         .    55  55 PUNCT

编辑:更好的解决方案

但最好的解决方案是从 udpipe 开始,然后再做剩下的事情。请注意,我使用的是 stringi 而不是 stringr 包。 stringr 是基于 stringi 的,但是 stringi 有更多的选择。

x <- udpipe_annotate(udmodel, x = df$Section)

x %>% 
  as_data_frame %>% 
  select(token, POSTag = upos) %>% # select needed columns
  # add start/end locations
  mutate(locations = map(token, function(x) data.frame(stringi::stri_locate(df$Section, fixed = x)))) %>% 
  unnest

  # A tibble: 10 x 4
   token     POSTag start   end
   <chr>     <chr>  <int> <int>
 1 If        SCONJ      1     2
 2 an        DET        4     5
 3 infusion  NOUN       7    14
 4 reaction  NOUN      16    23
 5 occurs    NOUN      25    30
 6 ,         PUNCT     31    31
 7 interrupt VERB      33    41
 8 the       DET       43    45
 9 infusion  NOUN       7    14
10 .         PUNCT     55    55

关于一次性进行 R POS 标记和标记化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51861346/

相关文章:

r - 从 DateTime 向量中获取小时

r - gitbook 代码块中出现丑陋的深色条纹,突出显示 : espresso (R bookdown)

python - 标记字段之间空格数不等的字符串

python - CESS_ESP 标签的定义

machine-learning - 即使对于用于训练它的文件,libSVM 也会给出非常不准确的预测

r - 如何在R中指定顶点的标签

r - 如何用r中变量中的值替换数据框中的特定字符

Android 和 CommaTokenizer

parsing - 在 Shopify Liquid 中将字符串解析为标记

python - NLTK Perceptron Tagger - 它识别什么是 FW(外来词)?