【发布时间】: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