根据标签数据从句子(REGEX)中提取标签信息

发布于 2025-02-10 10:20:58 字数 393 浏览 0 评论 0 原文

我有一个输入语料库,可能是以下格式:

名称:ABC DEF日期:8-01-09年龄:5 (名称 +姓氏)//预期输出:ABC DEF

名称:ABC日期:8-01-09年龄:5 (仅出现名称)//预期输出:ABC

名称ABC日期8-01-09年龄5 (没有COLON标签之后)//预期输出:ABC

日期8-01-09名称ABC Def Age 5 (随机位置中的名称标签)//预期输出:ABC DEF

当前解决方案:我能够进行硬码它要搜索名称并以单词为止直到第一个空间。但是我不确定如何在名字+姓氏(本质上是在下一个标签之前)提取。

任何帮助将不胜感激。谢谢!

I have an input corpus which could be of the following formats:

Name: ABC Def Date: 8-01-09 Age: 5 (First Name + Last Name) //Expected Output: ABC Def

Name: Abc Date: 8-01-09 Age: 5 (Only first name present) //Expected Output: Abc

Name ABC Date 8-01-09 Age 5 (No colon after tags) //Expected Output: ABC

Date 8-01-09 Name ABC DEF Age 5 (Name tag in a random location) //Expected Output: ABC DEF

Current Solution: I am able to hardcode it to search for name and take until the word until the first space. But I am not sure how to extract in cases of First Name+Last Name (essentially until the next tag)

Any help will be much appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜访吸血鬼 2025-02-17 10:20:58

尝试( regex101 ):

import re

test_cases = [
    "Name: ABC Def Date: 8-01-09 Age: 5",
    "Name: Abc Date: 8-01-09 Age: 5",
    "Name ABC Date 8-01-09 Age 5",
    "Date 8-01-09 Name ABC DEF Age 5",
]

pat = re.compile(r"Name:?\s*(.*?)\s*(?:(?:Date|Age:?\s*\d)|$)")

for t in test_cases:
    m = pat.search(t)
    print(m.group(1))

打印:

ABC Def
Abc
ABC
ABC DEF

Try (regex101):

import re

test_cases = [
    "Name: ABC Def Date: 8-01-09 Age: 5",
    "Name: Abc Date: 8-01-09 Age: 5",
    "Name ABC Date 8-01-09 Age 5",
    "Date 8-01-09 Name ABC DEF Age 5",
]

pat = re.compile(r"Name:?\s*(.*?)\s*(?:(?:Date|Age:?\s*\d)|$)")

for t in test_cases:
    m = pat.search(t)
    print(m.group(1))

Prints:

ABC Def
Abc
ABC
ABC DEF
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文