任务:要从一段文本中提取出手机号、微信号和一些意图信息。其中有一些模块的输入是需要一些模块的输出的,于是就想到可不可以用管道模式,对这一系列操作进行处理。
1.相关库的使用
pipeline的框架,我使用了fastcore库里的管道模式实现类。
pip3 install fastcore
2. 两个基本模块的实现
这里不列出所有模块的实现,只列出了提取手机号和微信号的实现类。
- 提取手机号的实现类
def extract_phone(statement):
"""
:description 从语句中抽取手机号信息
:param statement: 完整的语句
:param base_struct: 基础的结构体
:return: 传递管道信息
"""
phones = re.findall('(13\d{9}|14[5|7]\d{8}|15\d{9}|166{\d{8}|17[3|6|7|8]\d{8}|18\d{9})', statement['data'])
if len(phones) > 0:
statement['phone'] = phones[0]
return statement
- 提取微信号的实现类
class WechatParser:
def __init__(self, file_path)