首页 > 解决方案 > 带有R(tm包?)的PDF中句子中单词的共现

问题描述

因此,我的目标是使用 R 编写一些可以擦洗 PDF 并在一起提到事物时拉取的东西——例如,每当在https://pdfs.semanticscholar.org/403c/fd873feb7055c9140b7abfa4584fa7ee1c7f.pdf中一起提到加压素和前下丘脑时或类似的东西。我发现的大多数文本分析教程通常会在文本分析之前去掉标点符号和所有这些,因此无法检查何时在同一个句子中提及事物。这是可能的事情吗?

谢谢!

标签: r

解决方案


您可能不得不说得更详细,并给出真实的示例数据,但原则上这是非常可行的。这是一个希望对您有所帮助的示例:

# here are some 'documents' -- just text strings
doc1 <- "hello. apple horse."
doc2 <- "hello. banana legislature"
doc3 <- "hello, apple banana. horse legislature"

# store them in a list...
list_of_docs <- list(doc1, doc2, doc3)

# ...so we can apply a custom function to this list
lapply(list_of_docs, function(d) {

  # split each document on the '.' character 
  # (fixed=T means interprest this as plain text, not regex)
  phrases_in_d <- unlist(strsplit(d, '.', fixed=T))

  # now here's a regex pattern to search for:
  #   apple followed by anything followed by banana, 
  #     OR 
  #   banana followed by anything followed by apple
  search_regex <- 'apple.*banana|banana.*apple'

  # grepl() returns a logical vector (TRUE or FALSE) to say if there's a match
  # for 'search regex' among 'phrases in document d'
  # any() returns true if any phrases match
  any(grepl(search_regex, phrases_in_d))
})

如您所料,结果是一个false, false, true.


推荐阅读