【问题标题】:How to extract specific information from emails using machine learning?如何使用机器学习从电子邮件中提取特定信息?
【发布时间】:2019-06-03 09:40:39
【问题描述】:

我有多封电子邮件,其中包含库存、价格和数量列表。每天,列表的格式都会有所不同,我希望使用 NLP 来尝试理解读取数据并重新格式化它以以正确的格式显示信息。

以下是我收到的电子邮件示例:

Symbol  Quantity    Rate
AAPL    16        104
MSFT    8.3k      56.24
GS      34        103.1
RM      3,400     -10
APRN    6k        11
NP      14,000    -44

正如我们所见,数量有多种格式,代码始终是标准的,但汇率可以是正数或负数,也可能有小数。另一个问题是标题并不总是相同,因此这不是我可以依赖的标识符。

到目前为止,我已经在网上看到了一些适用于名称的示例,但我无法为股票代码、数量和价格实现此功能。到目前为止我尝试过的代码如下:

import re
import nltk
from nltk.corpus import stopwords
stop = stopwords.words('english')

string = """

To: "Anna Jones" <anna.jones@mm.com>
From: James B.

Hey,
This week has been crazy. Attached is my report on IBM. Can you give it a quick read and provide some feedback.
Also, make sure you reach out to Claire (claire@xyz.com).
You're the best.
Cheers,
George W.
212-555-1234
"""


def extract_phone_numbers(string):
    r = re.compile(r'(\d{3}[-\.\s]??\d{3}[-\.\s]??\d{4}|\(\d{3}\)\s*\d{3}[-\.\s]??\d{4}|\d{3}[-\.\s]??\d{4})')
    phone_numbers = r.findall(string)
    return [re.sub(r'\D', '', number) for number in phone_numbers]


def extract_email_addresses(string):
    r = re.compile(r'[\w\.-]+@[\w\.-]+')
    return r.findall(string)


def ie_preprocess(document):
    document = ' '.join([i for i in document.split() if i not in stop])
    sentences = nltk.sent_tokenize(document)
    sentences = [nltk.word_tokenize(sent) for sent in sentences]
    sentences = [nltk.pos_tag(sent) for sent in sentences]
    return sentences


def extract_names(document):
    names = []
    sentences = ie_preprocess(document)
    for tagged_sentence in sentences:
        for chunk in nltk.ne_chunk(tagged_sentence):
            if type(chunk) == nltk.tree.Tree:
                if chunk.label() == 'PERSON':
                    names.append(' '.join([c[0] for c in chunk]))
    return names


if __name__ == '__main__':
    numbers = extract_phone_numbers(string)
    emails = extract_email_addresses(string)
    names = extract_names(string)

    print(numbers)
    print(emails)
    print(names)

这段代码在数字、电子邮件和姓名方面做得很好,但我无法在我的示例中复制它,而且我真的不知道如何去做。任何提示都会很有帮助。

【问题讨论】:

    标签: python machine-learning nlp


    【解决方案1】:

    您可以构建将检查数字和金额的正则表达式。

    但是,对于棍子,您将不得不做一些不同的事情。我怀疑股票名称在电子邮件中并不总是用大写字母书写。如果他们只是编写一个脚本,该脚本将利用一些证券交易所的 API,并且只运行所有字母都为大写形式的单词。但是,如果电子邮件中的股票名称不是用大写字母写的,您可以做几件事。您可以对照该证券交易所检查电子邮件中的每个字词,如果它是棒名称。如果你想加快这个过程,你可以尝试做依赖解析,只针对 API 运行名词或代词。

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 2012-10-31
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2013-01-18
      • 1970-01-01
      • 2014-12-17
      相关资源
      最近更新 更多