Class: FrequencyTagger

Inherits:
ListTagger show all
Defined in:
lib/list_taggers/frequency_tagger.rb

Instance Method Summary collapse

Methods inherited from ListTagger

#parse_main, #parse_text

Methods inherited from Tagger

#parse_main, #parse_text

Constructor Details

#initializeFrequencyTagger

Returns a new instance of FrequencyTagger.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/list_taggers/frequency_tagger.rb', line 19

def initialize
    @name = 'freq'
    @list = [/q\.?\s?(?:a\.?m\.?|morning)/i,
            /q\.?\s?(?:p\.?m\.?|afternoon)/i,
            /q\.?\s?h\.?(?:s\.?)?/i,
            /(?:at\s*)?night(?:ly)?/i,
            /(?:at\s*)bedtime/i,
            /(?:at\s*)midday/i,
            /in the morning/i,
            /immediately/i,
            /hs/i,
            /(?x)(?:(?:every|q\.?)\s*(?:\d\-)?
            (?:2|3|4|5|6|7|8|9|10|11|12|24|36|48|72|
            two|four|six|eight|twelve)(?:(?:-|\s+to\s+)
            (?:2|3|4|5|6|7|8|9|10|11|12|24|36|48|72|
            two|four|six|eight|twelve))?\s*
            (?:h(?:rs?|(?:ours?))?\.?))/i,
            /q\.?\s?d(?:\.|ay)?/i,
            /q-day/i,
            /(?x)(?:(?:every|each)\s*\d?\s*
            (?:day|night|week|morning|afternoon|evening))/i,
            /(?:(?:once|twice)\s+(?:(?:a|per)\s+(?:day|week|night|month)))/i,
            /(?x)(?:(?:(?:(?:\d\-)?(?:two|three|four|five|\d)
            \s+times?\s+)|(once|twice)\s+)?(?:daily|weekly|hourly))/i,
            /qod/i,
            /every other day/i,
            /[bqt]\.i\.d\./i,
            /[bqt]idh?\b/i,
            /(?x)(?:(?:\d\-?)?(?:one|two|three|four|five|\d)\s+
            (?:times?|days?|nights?)?\s?
            (?:a|per|\/)\s?(?:day|week|night|month))/i,
            /(?:once|twice|(?:(?:\d+|one|two|three|four|five)\s*times))?monthly/i,
            /(?:as|when)\s+needed/i,
            /p\.?r\.?n\.?/i]
end

Instance Method Details

#normalize(freq) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/list_taggers/frequency_tagger.rb', line 55

def normalize(freq)
  numwords = {"one"=>"1","two"=>"2","three"=>"3","four"=>"4","five"=>"5","six"=>"6","seven"=>"7","eight"=>"8"}
  case freq
    when /prn.?/i, /as needed/i
      return :prn
    when /qod/i
      return :qod
    when /(\d+(-\d+)?|[a-z]+) times? ((a|per) )?(day|week|daily|weekly)/i
      return :range if $2
      num, unit = $1, $5.downcase
      subs = {"1" => "q", "2" => "bi", "3" => "ti", "4" => "qi"}
      num = numwords[num] || num
      num = subs[num] || num+"i"
      return (num + unit[0]).to_sym
    when /(once |twice )(a )?(day|week|daily|weekly)/i
      num, unit = $1, ($3).downcase
      return ("bi"+unit[0]).to_sym if ($1).downcase == "twice "
      return ("q"+unit[0]).to_sym
    when /daily/i #we've checked other contexts for daily already
      return :qd
    when /(q|every|each) ?(\d+(-\d+)?|[a-z]+)? ?(days?|ho?u?rs?|morning|night)/i
      return :qod if $2 && ["other","two","2"].include?(($2).downcase) && $4 == "day"
      return :range if $3
      subs = {"morning" => "am", "afternoon" => "pm", "night" => "hs"}
      num, unit = $2, $4
      num = numwords[num] || num
      unit = subs[unit.downcase] || unit[0].downcase
      return ("q" + num.to_s + unit).to_sym
    when /q(\d+(-\d+)?)?(am|d|hs|pm|w|h)/i
      return :range if $2
      return $&.downcase.to_sym
    when /(b|t|q|(\d+(-\d+)?))i(w|h|d)/i
      return :range if $3
      return $&.downcase.to_sym
    end
        
end