LoginSignup
0
0

More than 3 years have passed since last update.

gooの時刻情報正規化APIを使う

Last updated at Posted at 2019-05-01

概要

目的

文書から和暦、西暦を含めた日付を標準化して抽出する。

手順

  • gooでapiキーを取得します。Githubのアカウントが必要なので取得します
    https://labs.goo.ne.jp/jp/apiregister/

  • httpclient gemをインストールします

  • 下記のclassを読み込みます。 APP_ID はご自分のIDに置き換えてください。

goo_chrono.rb
require 'httpclient'
require 'json'

# gooの時刻情報正規化APIを使うためclass
# https://labs.goo.ne.jp/api/jp/time-normalization

class GooChrono
  REQUEST_URL = 'https://labs.goo.ne.jp/api/chrono'
  APP_ID = ENV['GOO_API_KEY'] # YOUR GOO API KEY

  attr_reader :response, :sentence

  def initialize(attr = {})
    @request_id = attr[:request_id] || nil
    @sentence = tr_sentence(attr[:sentence])
    @doc_time = doc_time(attr[:doc_time])
  end

  def post
    client = HTTPClient.new
    content = params.to_json
    res = client.post_content(REQUEST_URL, content, 'Content-Type' => 'application/json')
    @response = JSON.parse(res)
  end

  def first_date_time
    raise StandardError, '先にpostしてください' unless @response.present?
    return nil if @response['datetime_list'].first.nil?
    Time.zone.parse(@response['datetime_list'].first[1])
  end

  private

  def params
    hs = { app_id: APP_ID,
           sentence: @sentence,
           doc_time: @doc_time.strftime("%Y-%m-%dT%H:%M:%S") } # "%Y-%m-%dT%H:%M:%S"
    hs.merge(request_id: @request_id) if @request_id.present?
    hs
  end

  def doc_time(str)
    if str.present?
      Time.parse(str)
    else
      Time.now
    end
  end

  # 2019.01.01 などを2019/01/01 に変換しないと認識しなかったので。適宜前処理。
  def tr_sentence(str)
    str = str.gsub(/( | )/, '')
    str = str.gsub(/(\t|\n)/, '')
    str = str.tr('0-9a-zA-Z', '0-9a-zA-Z')
    str = str.gsub(/(\.|・)/, '/')
    str = str.gsub(/(-|ー)/, '/')
    str.strip
  end
end

試す

pry(main)> goo_chrono = GooChrono.new(sentence: '平成30年11月2日')
=> #<GooChrono:0x00005630896fe1f8 @doc_time=Wed, 01 May 2019 07:56:41 UTC +00:00, @request_id=nil, @sentence="平成30年11月2日">
pry(main)> goo_chrono.post
=> {"request_id"=>"labs.goo.ne.jp\t1556697406\t0", "doc_time"=>"2019-05-01T07:56:41", "datetime_list"=>[["平成30年11月2日", "2018-11-02"]]}
pry(main)> goo_chrono.first_date_time 
=> Fri, 02 Nov 2018 00:00:00 UTC +00:00

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0