python - 当日期可以输入为 04-14-2021 和 04-2022 和 4-2021 时,在 spacy 中指定正则表达式
问题描述
当日期可以输入为 04-14-2021 和 04-2022 和 4-2021 时,如何在 spacy 中指定正则表达式。
对于连字符,我正在使用这个正则表达式:
import spacy
from spacy.tokens.doc import Doc
from spacy.matcher import Matcher
nlp = spacy.load('en_core_web_sm')
matcher = Matcher(nlp.vocab)
doc: Doc = nlp("4-15-2024 02-26 02 6-2021 05-2021")
pattern_9 = [{'TEXT':{'REGEX':r'^\d{1,2}$'}}, {'TEXT':{'REGEX':r'^-$'}}, {'TEXT':{'REGEX':r'^\d{2,4}$'}}] #e.g. Jun-2021
matcher.add('DATE_PATTERN_9', [pattern_9])
matches = matcher(doc)
print(f"matches = {matches}")
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id] # Get string representation
span = doc[start:end] # The matched span
print( span.text)
文档:
Doc = nlp("4-15-2024 02-26 02 6-2021 05-2021")
它分成4-15-2024
&4-15
但15-2024
我只想要mm-yy
或mm-yyyy
作为输出。
现在,我得到低于上述模式的输出:
4-15
15-2024
02-26
6-2021
05-2021
解决方案
推荐阅读
- php - 使用 PHP 将类添加到输入
- javascript - Firebase Cloud Function,触发 onWrite 方法并在检索用户数据时遇到问题
- apache-spark - 高效批处理 Spark 数据帧以调用 API
- reactjs - 如何避免 redux-toolkit 中出现重复的 reducer
- html - 什么是错误无法从给定项目获取上下文?
- ios - 当我尝试在视图之间转换时,某些数据没有出现这样的 UIImageViews:Xcode
- linux - 如何为 Docker 使用制作跨操作系统的 sh 脚本?
- php - Laravel:仅对 Intranet 应用程序使用密码身份验证
- c# - .NET 实体框架和存储过程
- python-3.8 - 检查什么程序创建了一个文件