【问题标题】:Extracting text from PDF and compare to dictionary从 PDF 中提取文本并与字典进行比较
【发布时间】:2018-09-05 08:13:27
【问题描述】:

我目前正在从事一个项目,我想从 PDF 中提取文本,然后检查提取文本中的某个单词是否出现在某个字典中。 如果是这样,我希望我们使用 example.replace(file, x, y) 将文本中的单词替换为字典中的值。

我正在努力使用循环来检查文本中的所有单词并自动将它们与字典进行比较。目标是我不必自己输入“旧”和“新”,但程序会检查文本中的所有单词,如果它在字典中找到一个“旧”应该是文本中的单词和“新”键的值。手动版本有效。

这是我的代码

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO

def convert_pdf_to_txt(path):

rsrcmgr = PDFResourceManager()

retstr = StringIO()
codec = 'utf-8'

laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()

for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
    interpreter.process_page(page)

text = retstr.getvalue()

fp.close()
device.close()
retstr.close()
return text

dictionary = {"Die" : "Der", "Arbeitsfläche":"Platz"}


def convert(file, old, new):

translation = convert_pdf_to_txt(file).replace(old, new)
return translation

print(convert('mytest.pdf','Die' ,'Der'))

感谢您的帮助!

【问题讨论】:

    标签: python python-3.x python-2.7 pdf text


    【解决方案1】:

    假设您能够阅读 pdf 文件。您可以使用

    将数据存储在列表中
    list_voc = []
    
    list_voc.extend(text.split())
    

    现在使用一个简单的循环,您可以检查列表元素是否属于字典,如果是则替换它。

    indx=0
    for i in pdf_vocab:
        if i in dictionary.keys():
            pdf_vocab[indx] = dictionary[i]
        indx = indx + 1
    

    indx 变量存储列表的索引,只要元素(或单词)在字典中,我们就可以在该特定索引处替换该单词。

    【讨论】:

    • 您好,谢谢您的回答。这个解决方案是我一直在寻找的。我在我的最后一个函数(“convert(...)”)中实现了它,但不幸的是,代码在文本和字典中没有找到任何相似的单词
    • 很抱歉回复晚了。在上述解决方案中,它还检查大小写(大写或小写),如果您不希望它区分大小写,可以尝试 i.lower() 中的 dictionary.keys().lower() 。否则它对我来说很好。
    【解决方案2】:

    如果您的意图只是将提取的文本 PDF 中的单词替换为 Dictionary 值,那么该解决方案可能会对您有所帮助。 只需挑选出与 Dictionary 键相交的单词,然后将值一一替换即可。

    import re
    #text = Extracted text from PDF
    text = r" with the loop for Die checking all words in my text and compare them to the dictionary automatically"
    for key in set(text.split(' ')).intersection(dictionary.keys()):
        text = re.sub(key,dictionary[key],text)
    

    【讨论】:

      【解决方案3】:

      因为我不能发表评论...

      这个循环应该可以帮助你。

      for old, new in dictionary.items():
          # update text by replacing old with new
      

      替换时,您应该确保只交换单词,否则可能会发生 'book': 'shoe' 将单词 'bookmarket' 转换为 'shoemarket'。模块 re 可以在这里为您提供帮助。 https://docs.python.org/3/library/re.html

      其实这个人也解决了同样的问题。 Search and replace with "whole word only" option

      如果您还想交换短语,字典的顺序可能很重要;字典 {'I': 'you', 'I like': 'chicken'} 会将 'I like' 转换为 'you like',尽管这可能不是我们想要的。

      【讨论】:

        猜你喜欢
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        • 2016-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-14
        • 2021-04-21
        相关资源
        最近更新 更多