如何在PDF文档中搜索公司名称或股票代码?

时间:2019-04-26 14:15:08

标签: python regex pdf full-text-search

我正在寻找一堆PDF文档中提到的股票/公司。有时,使用公司名称(例如,美国运通卡),而其他时候,PDF仅包含股票代码(例如,AXP)。

通过以下代码,我可以很容易地找到符号:

import PyPDF2
import os
import re

tickers = ['AMZN', 'V', 'ETSY', 'AXP', 'AA', 'FB'] 

source_dir = '/Users/person/folder/'
for dir, subdir, files in os.walk(source_dir):
    for file in files:
        if file.endswith('.pdf'):
            file = os.path.join(dir, file)
            pdfFileObj = open(file, 'rb')
            pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
            if pdfReader.isEncrypted:
                print("Skipping " + file)
                pass
            else:
                num_pages = pdfReader.numPages

                count = 0
                text = " "

                while count < num_pages:
                    pageObj = pdfReader.getPage(count)
                    count += 1
                    text += pageObj.extractText()

                print("\n" + file)
                # print(text)

                matches = re.findall(regex, text)
                matches = list(dict.fromkeys(matches))
                for match in matches:
                    print("- " + match)

但有时PDF仅提及公司名称。我将如何搜索股票代码或公司的公司名称,然后同时返回两者?因此所需的输出将是:

/Users/person/folder/file.pdf
- AMZN (Amazon)
- AXP (American Express)
- AA (American Airlines)
- V (Visa)

我有一个csv文件,可将股票代号映射到公司名称,但如果愿意,可以将其设置为数据库或字典。

这是样本行情/公司名称数据:

| Ticker | Company Name     |
-----------------------------
|  AMZN  | Amazon Inc       |
-----------------------------
|   V    | Visa Inc.        |
-----------------------------
|  ETSY  | Etsy             |
-----------------------------
|  AXP   | American Express |
----------------------------- 
|  AA   | American Airlines |
----------------------------- 
|  FB    | Facebook         |
----------------------------- 

这是一些示例文本:

- Etsy (ETSY): Etsy do eiusmod tempor incid is Duis aute irure Etsy dolor in reprehenderit in volup 
- AXP: Excepteur sint occaecat cupidatat non proident, sunt in culpa AXP deserunt
- AA: American Airlines id est laborum. 
- V: enim ad minim veniam (V) est. 

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您会发现这种方法很有用:

import re

# Create dict from tickers and company
# Can be done from csv file
s = {'AMZN' : 'Amazon Inc.',
'V' : 'Visa Inc.',
'ETSY' : 'Etsy',
'AXP' : 'American Express',
'AA' : 'American Airlines',
'FB' : 'Facebook'}


# Create regex from dict s
regex = ''
for key, item in s.items():
    regex = regex + r'\b' + key + r'|\b' + item + r'|'

# Remove last '|' (or)
regex = regex[:-1]

现在您的正则表达式将如下所示:

regex = '\bAMZN|\bAmazon Inc.|\bV|\bVisa Inc.|   ..etc..   |\bFB|\bFacebook'

搜索后,您可以创建发现的自动报价字典:

matches = re.findall(regex, text)

# Create an empty dictionary of matches
matches_d = dict()

for m in matches:
    if m in s.keys():
        matches_d[m] = s[m]
    if m in s.values():
        # Find key from value
        key = list(s.keys())[list(s.values()).index(m)]
        matches_d[key] = s[key]

匹配项是这样的列表:

['Etsy', 'ETSY', 'AXP', 'Facebook', 'AA', 'American Airlines', 'V']

matches_d(最后的字典)将像这样:

{'ETSY': 'Etsy',
 'AXP': 'American Express',
 'AA': 'American Airlines',
 'V': 'Visa Inc.'}