r - 带有R(tm包?)的PDF中句子中单词的共现
问题描述
因此,我的目标是使用 R 编写一些可以擦洗 PDF 并在一起提到事物时拉取的东西——例如,每当在https://pdfs.semanticscholar.org/403c/fd873feb7055c9140b7abfa4584fa7ee1c7f.pdf中一起提到加压素和前下丘脑时或类似的东西。我发现的大多数文本分析教程通常会在文本分析之前去掉标点符号和所有这些,因此无法检查何时在同一个句子中提及事物。这是可能的事情吗?
谢谢!
解决方案
您可能不得不说得更详细,并给出真实的示例数据,但原则上这是非常可行的。这是一个希望对您有所帮助的示例:
# 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
.
推荐阅读
- asp.net-core-mvc - 如何在 .net core 2.0 ActionExecutingContext 中分配 Http 响应
- r - R:在 CRAN 中搜索包代码或本地安装
- c# - 如何防止人们不断循环 HTTP POST 到函数?
- ruby - 在 ruby 中读取的文件将输出作为字符中的空格
- android - 属性顺序乱了
- c# - 让用户键入位置 C#
- python - 将标题列表连接到熊猫中的数据框
- python - 来自网络摄像头的实时帧
- python - 在python中加入两个带有条件的df
- uicollectionview - Swift:以编程方式在 UICollectionViewController 的第一行之前设置一个视图