【发布时间】:2019-12-03 02:22:02
【问题描述】:
我正在尝试从我在 R 中的一些文本中提取 Lakes 的名称。 Lakes 是正确的(大写),但需要我在“Lake”一词的两侧提取几个单词。
我尝试了一些方法,但都没有达到我想要的效果……在某些情况下,句子或文章可能以“Lake”开头,因此之前没有文字。在某些情况下,正确的名称可能是 3 个单词(Lake St. Clair 或 Red Hawk Lake)。
使用的示例代码:
text <- paste("Lake Erie is located on the border of the United States and Canada.",
"It is located nearby to Lake St. Clair and Lake Michigan.",
"All three lakes have a history of high levels of Phosphorus.",
"One lake that has not yet been impacted is Lake Ontario.")
这可能是我得到的最接近的——从另一个堆栈溢出中提取,但它仍然没有解决。
context <- function(text){splittedText <-strsplit(text,'',T) print(splitted Text) data.frame(before = head(c('',splittedText),-1),words=splittedText,after=tail(c(splittedText,''),-1))}
info <- context(text)
print(subset(info, words == 'Lake')
我想获得:1)提取的正确湖泊名称(“伊利湖”、“圣克莱尔湖”等)或 2)在“湖”之前和之后包含单词的数据框。理想情况下是第一个,但我现在很灵活。
before <- c("","nearby to", "Clair and","impacted is")
Lake <- c("Lake","Lake","Lake","Lake")
after <- c("Erie is","St. Clair", "Michigan ","Ontario ")
output <- data.frame(cbind(before,Lake,after)); print(output)
提前感谢您的帮助!
【问题讨论】:
标签: r text nlp capitalization phrase