r - R POS 标记和标记化一气呵成
问题描述
我有如下文字。
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)
它给了我标记,开始和结束位置。如何在取消嵌套的同时获取 POS 标签。如下所示(下图中的 POSTtags 可能不正确)
解决方案
您可以使用包 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
现在将其与您提出的上一个问题的结果结合起来。我选择了其中一个答案。
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
推荐阅读
- date - Omnifaces of:secondsBetween EL 函数无法通过 of:formatDate 转换为 HH:mm:ss
- php - 如何在 php Laravel 中爬升变量?
- find - Episerver FIND 结果不在 CMS 中
- javascript - 如何在谷歌地图折线的中心设置折线标记?
- python - 在 ODOO 中覆盖 Create / Write 方法中的返回操作
- javascript - 根据来自另一个的事件过滤一个 ag-grid
- database - Neo4j:如何匹配关系?
- java - 在计算年复利时,为什么 Java 计算 interestRate/100 而不仅仅是 interestRate?
- react-native - 如何在每 1 分钟内获取坐标
- typescript - 打字稿装饰器 + 反映元数据