微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有邻近搜索的布尔搜索查询

如何解决带有邻近搜索的布尔搜索查询

我管理媒体监控服务。我们使用 lucene 索引和复杂(而且非常冗长)的布尔查询字符串来查找有命中的文档。这些文档中的绝大多数都是基于 PDF 的。

我们遇到的问题是在上下文中显示这些命中。例如,一份文档可能有 100 页长,但唯一的相关文本可能在一页或两页上。我一直在研究一种解决方案,可以自动突出显示 PDF 中的匹配项,以便读者可以轻松查看提及术语的位置。

我已经能够使用 PDFminer 和 PyMuPDF 开发一个非常基本的脚本来完成这项任务。

# -*- coding: utf-8 -*-
from pdfminer.high_level import extract_text
import fitz
import re

### READ IN PDF

regex = r"\b(?:growth\W+(?:\w+\W+){0,5}?hormone|hormone\W+(?:\w+\W+){0,5}?growth)\b"
file = 'test.pdf'
text = extract_text(file)
doc = fitz.open(file)

strings = []
for match in re.finditer(regex,text):
    string = match.group()
    strings.append(string)

for i in range(0,doc.pageCount):
    for j in range(0,len(strings)):
        page = doc[i]
        text_instances = page.searchFor(strings[j])
        print(text_instances)
        for inst in text_instances:
            highlight = page.addHighlightAnnot(inst)


### OUTPUT

doc.save("output.pdf",garbage=4,deflate=True,clean=True)

主要问题是我不能(据我所知)使用布尔查询来定位文本中的匹配词,然后在 PDF 中找到并突出显示这些词的实例。在上面的示例中,我使用正则表达式来搜索与布尔查询“growth w/5 激素”等效的正则表达式。这是有效的,但对我来说将整个布尔查询转换为正则表达式或可用于执行此搜索的任何其他语法对我来说太困难了。或者更确切地说,转换“OR”运算符很容易,但我无法为诸如“W/”或“NEAR/”之类的邻近运算符找出可行的解决方案。

下面是其中一个布尔查询的示例。任何建议或意见将不胜感激。另请注意,我在 Python 方面的经验并不丰富,我所知道的都是自学的。

查询字符串:

((abnorm* OR Abortion OR abuse* OR accidental OR ADR$1 OR "adverse effect*" OR "adverse event*" OR "adverse reaction*" OR AE$1 OR allerg* OR antibod* OR (benefit w/1 risk*) OR "birth defect*" OR carcinogen* OR "clinical failure") OR (complication* OR contamination OR congenital OR death* OR defect* OR deliver* OR "drug dispensing error*" OR (drug w/1 (effect* OR ineffect* OR interaction* OR reaction* OR resistance OR toxicity OR withdrawal))) OR (efficacy OR effectiveness OR embryo* OR epidemiology OR fatal OR fetal OR foetal OR genomic* OR genotoxic* OR hypersensitivity OR "idiosincratic reaction*" OR "idiosincratic toxicit*" OR immunogen* OR "incorrect drug administ*") OR (ineffective OR (lack w/3 (effect* OR efficacy OR effectiveness OR response)) OR lethal OR malformation* OR "medication error*" OR miscarriage* OR misuse* OR mutagen* OR "near miss" OR "occupational exposure" OR "off label") OR (overdos* OR pharmacogenetic* OR pharmacogenomic* OR "pharmacogenomic biomarker*" OR poison* OR pregnan* OR prescribing OR infection* OR SAE$1 OR safety OR "side effect*" OR SUSAR* OR teratogen*) OR ((therapeut* w/3 (decreased OR delay* OR effect OR efficacy OR effectiveness OR failure OR outcome OR response)) OR toxic OR toxicity OR (transmi* w/3 infect*)) OR ((Transmission w/3 (bacter* OR pathogen* OR viral OR virus))) OR ((treatment w/3 (delay* OR effect* OR efficacy OR effectiveness OR failure OR outcome OR response)) OR underdos* OR "undesirable effect*" OR "wrong drug administ*") OR "lack of efficacy")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。