从R中的语句中提取数值数据?

时间:2017-08-30 10:59:35

标签: r machine-learning opennlp

我正在使用(带有openNLP的R)从给定语句中提取数值数据。

声明为"The room temperature is 37 to 39 C. The Air flow is near 80 cfm".

此处为预期输出"Temperature > 37 - 39c", "Air flow -> 80cfm"

你能否在POS标签上建议任何正则表达式模式来获取Noun(NN)和下一个可用数字数据(CD)?

是否有其他方法可以提取类似数据?

2 个答案:

答案 0 :(得分:0)

从自然文本中提取数据很难!我希望这个解决方案能够很快打破。但这是一个让你入门的方法。你没有提供整个标记的句子,所以我插入了自己的标签。您可能需要为标记集更改此设置。此代码既不高效也不向量化,只适用于单个字符串。

library(stringr)

text <- "The_DT room_NN temperature_NN is_VBZ 37_CD to_PRP 39_CD C_NNU. The_DT Air_NN flow_NN is_VBZ near_ADV 80_CD cfm_NNU"

# find the positions where a Number appears; it may be followed by prepositions, units and other numbers
matches <- gregexpr("(\\w+_CD)+(\\s+\\w+_(NNU|PRP|CD))*", text, perl=TRUE)

mapply(function(position, length) {
  # extract all NN sequences
  nouns <- text %>% str_sub(start = 1, end = position) %>% 
      str_extract_all("\\w+_NN(\\s+\\w+_NN)*")
  # get Numbers
  nums <- text %>% str_sub(start=position, end = position + length)
  # format output string
  result <- paste(tail(nouns[[1]], n=1), nums, sep = " > ")
  # clean tags
  gsub("_\\w+", "", result)
}, matches[[1]], attr(matches[[1]], "match.length"))
# output: [1] "room temperature > 37 to 39 C." "Air flow > 80 cfm"

答案 1 :(得分:0)

也许你可以从下面的方法开始。希望这有帮助!

[1] "temperature 37 to 39 C. flow 80 cfm"

输出是:

{{1}}
相关问题