【问题标题】:Parsing date from text using Ruby使用 Ruby 从文本中解析日期
【发布时间】:2010-02-17 00:34:16
【问题描述】:

我正在尝试弄清楚如何使用 Ruby 从非结构化文本中提取日期。

例如,我想从这个字符串“不考虑 2010 年 2 月 1 日午夜 (EST) 午夜 12:00 之后开始的申请”中解析日期。

有什么建议吗?

【问题讨论】:

  • 该字符串是否始终采用该格式?日期字符串是否始终采用该格式?
  • 字符串不会。日期字符串将采用该格式。

标签: ruby text-extraction information-extraction


【解决方案1】:

尝试 Chronic (http://chronic.rubyforge.org/),它可能能够解析,否则您将不得不使用 Date.strptime。

【讨论】:

    【解决方案2】:

    假设您只想要日期而不是日期时间:

    require 'date'
    string = "Applications started after 12:00 A.M. Midnight (EST) February 1, 2010 will not be considered."
    r = /(January|February|March|April|May|June|July|August|September|October|November|December) (\d+{1,2}), (\d{4})/
    if string[r]
      date =Date.parse(string[r])
      puts date
    end
    

    【讨论】:

      【解决方案3】:

      您也可以尝试gem,它可以帮助在字符串中查找日期。

      例子:

      input = 'circa 1960 and full date 07 Jun 1941'
      dates_from_string = DatesFromString.new
      dates_from_string.get_structure(input)
      
      #=> return
      # [{:type=>:year, :value=>"1960", :distance=>4, :key_words=>[]},
      # {:type=>:day, :value=>"07", :distance=>1, :key_words=>[]},
      # {:type=>:month, :value=>"06", :distance=>1, :key_words=>[]},
      # {:type=>:year, :value=>"1941", :distance=>0, :key_words=>[]}]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-05
        • 2015-07-22
        • 1970-01-01
        • 2022-10-14
        • 2016-01-01
        相关资源
        最近更新 更多