【问题标题】:Removing special characters and replacing with relevant text based on conditions根据条件删除特殊字符并替换为相关文本
【发布时间】:2020-05-15 07:31:41
【问题描述】:

当我在某些文档上运行 OCR 时,日期前缀会被 # 或任何特殊字符替换。 结果,我的 NER 需要月份和年份,但没有考虑日期编号。

我尝试过 pyspellchecker、gingerit 和其他拼写正确的库,但没有任何帮助

例如:
1. 15% 2019 年 3 月
2. 2020 年 10 月 3 英寸

我想根据两个条件替换 OCR 输出中的特殊字符:

1. Its starts with a number  
2. Its followed by a month  

需要的输出:

1. 15th March 2019  
2. 3rd oct 2020

你能帮我写同样的python代码吗?

我尝试了正则表达式,但在添加月份条件时遇到了困难。

代码:

r = re.compile(r"(?:^|\s)([0-9?])(\w+)")
items = r.findall(f)[![enter image description here][1]][1]

text = "on 13# november 1990 we sign the deal"
for word in text.split():
    if word.startswith("0" or '1' or '3') or word.endswith("#"):
        print (word)

【问题讨论】:

  • 嗨,请分享一些代码。
  • 添加了代码。

标签: python-3.x regex nlp


【解决方案1】:

这个Regex 短语可以作为一个很好的起点:

([12]\d|3[01]|0?[1-9]).\s+\w+\s+[12][0-9]{3}

解释:

  • ([12]\d|3[01]|0?[1-9]) - 代表月份中的天数 (1-31)
  • [12][0-9]{3} - 代表年份 (1000-2999)
  • . - 恰好是任何字符之一
  • \s+\w+\s+ - 一个或多个完整单词前后的空格

【讨论】:

    猜你喜欢
    • 2017-03-24
    • 2020-03-16
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2022-08-14
    相关资源
    最近更新 更多