在 Openrefine 中匹配不同的日期

K3it4r0

我正在尝试使用 OpenRefine 2.6 中的 value.match 命令将列中的信息拆分为(至少)2 列。然而,数据相当混乱。
我有时有完整的约会:

May 30, 1949

有时完整日期与其他日期和属性组合在一起:

May 30, 1949, published 1979
May 30, 1949 and 1951, published 1979
May 30, 1949, printed 1980
May 30, 1949, print executed 1988
May 30, 1949, prints executed 1988
published 1940

有时你有时间跨度:

1905-05 或者 1905-1906

有时只有一年

1905

有时有属性的年份

August or September 1908

似乎没有遵循任何特定的架构或顺序。
我想提取(至少)ca 开始和结束日期年份,以便有两列:

-----------------------  
|start_date | end_date|  
|1905       | 1906    |   
-----------------------  

没有其余的属性。

我可以找到最后一个日期 using
value.match(/.*(\d{4}).*?/)[0]
和第一个 with ,
value.match(/.*^(\d{4}).*?/)[0]
但我在这两个公式上遇到了一些麻烦。
后者在以下情况下无法匹配:
May 30, 1949 and 1951, published 1979
而在以下情况下:
Paris, winter 1911-12后一个公式无法匹配任何内容,前一个公式匹配1911

有谁知道我该如何解决这个问题?
我需要一个解决方案,将第一个日期作为 start_date,将最终日期作为 end_date,或者更好(不知道是否可能)最早的日期作为 start_date 和最晚的日期作为 end_date。此外,我很高兴有一些关于如何提取其他信息的线索,例如文本中是否存在已发布印刷或已执行-> 将日期复制到新的列名称“执行”。应该类似于创建一个新列if(value.match("string1|string2|string3" + (\d{4}), "perform the operation", do nothing)

埃托雷扎

value.match()是一个非常有用但有时很棘手的功能。为了从文本中提取模式,我更喜欢使用 Python/Jython 的正则表达式:

import re

pattern = re.compile(r"\d{4}")

return pattern.findall(value)

从那里,您可以创建一个连接所有年份的字符串:

return ",".join(pattern.findall(value))

或者只选择第一个:

return pattern.findall(value)[0]

或者最后一个:

return pattern.findall(value)[-1]

等等。

您的子问题也是如此:

import re

pattern = re.compile(r"(published|printed|executed)\s+(\d+)")

return pattern.findall(value)[0][1]

或者 :

import re

pattern = re.compile(r"(published|printed|executed)\s+(\d+)")

m = re.search(pattern, value)

return m.group(2)

例子:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章