gpt4 book ai didi

python - 如何在 python 中使用 NLTK 从原始文本中提取地址?

转载 作者:太空狗 更新时间:2023-10-29 22:15:19 25 4
gpt4 key购买 nike

我有这段文字

'''Hi, Mr. Sam D. Richards lives here, 44 West 22nd Street, New York, NY 12345. Can you contact him now? If you need any help, call me on 12345678'''

.如何使用 NLTK 从上述文本中提取地址部分?我试过 Stanford NER Tagger,它只给我 New York 作为位置。如何解决?

最佳答案

绝对是正则表达式:)

有点像

import re

txt = ...
regexp = "[0-9]{1,3} .+, .+, [A-Z]{2} [0-9]{5}"
address = re.findall(regexp, txt)

# address = ['44 West 22nd Street, New York, NY 12345']

解释:

[0-9]{1,3}:1到3位数字,地址编号

(space): 号码和街道名称之间的空格

.+:街道名称,任意字符代表任意次数

,:城市前一个逗号和一个空格

.+:城市,任意字符出现任意次数

,:状态前一个逗号和一个空格

[A-Z]{2}:从 A 到 Z 恰好 2 个大写字符

[0-9]{5}:5 位数字

re.findall(expr, string) 将返回一个数组,其中包含找到的所有事件。

关于python - 如何在 python 中使用 NLTK 从原始文本中提取地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37745801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com