Module: Conjugue::VerbDatabase

Defined in:
lib/conjugue/verb_database.rb

Constant Summary collapse

COMMENT_RX =
/^#/
PARADIGM_RX =
/paradigma:([^:]*):?([^:]*)?/
PARADIGM_END_RX =
/^$/
CONJUGATIONS =
%w(FN IP PI II EI MI TI FI PS IS FS IA IN)
CONJUGATION_RX =
/^(#{CONJUGATIONS.join('|')}):(.*)/
TIMES =

class << self

{
  :p => 'PI',
  :pi => 'II',
  :pp => 'EI',
  :pm => 'MI',
  :f => 'TI',
  :fp => 'FI',
  :ps => 'PS',
  :pis => 'IS',
  :i => 'IA',
  :in => 'IN'
}
PRONOUNS =
[
  'Eu',
  'Tu',
  'Ele',
  'Nos',
  'Vos',
  'Eles'
]

Class Method Summary collapse

Class Method Details

.find_by_paradigm(verb) ⇒ Object



85
86
87
88
89
# File 'lib/conjugue/verb_database.rb', line 85

def find_by_paradigm(verb)
  paradigm = nil
  puts "found paradigm: #{paradigm[:name]}" if paradigm = VerbDatabase.paradigms[verb]
  return paradigm
end

.find_by_sufix(verb) ⇒ Object



91
92
93
94
95
# File 'lib/conjugue/verb_database.rb', line 91

def find_by_sufix(verb)
  sufix = VerbDatabase.sufixes.keys.find_all{ |s| verb =~ Regexp.new(s+'$') }.max_by{|s| s.size }
  puts "found sufix: #{sufix}" if sufix
  VerbDatabase.sufixes[sufix]
end

.find_by_verb(verb) ⇒ Object

def parse



80
81
82
83
# File 'lib/conjugue/verb_database.rb', line 80

def find_by_verb(verb)
  puts "found verb: #{verb}" if paradigm = VerbDatabase.verbs[verb]
  return paradigm
end

.initObject



4
5
6
7
8
9
10
11
12
# File 'lib/conjugue/verb_database.rb', line 4

def init
  puts "Verb database initialized."
  
  @paradigms = {}
  @verbs = {}
  @sufixes = {}
  
  parse
end

.paradigmsObject



97
98
99
# File 'lib/conjugue/verb_database.rb', line 97

def paradigms
  @paradigms
end

.parseObject



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/conjugue/verb_database.rb', line 21

def parse
  state = nil
  paradigm = nil

  database_file = File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'data', 'verbos')
  
  File.open(database_file, 'r:ISO-8859-1') do |file|
    file.each_line do |line|
      next if line =~ COMMENT_RX # comments
      
      ########################################
      # nil state
      ########################################
      if state == nil
        
        # enter paradigm state
        ########################################
        if line_match = line.match(PARADIGM_RX)
          state = :paradigm
          paradigm = {}
          paradigm[:name] = line_match[1].strip
          paradigm[:sufix] = line_match[2].strip
          paradigm[:verbs] = []
          paradigm[:conjugations] = {}
          
          @paradigms[paradigm[:name]] = paradigm

          @sufixes[paradigm[:sufix]] = paradigm unless paradigm[:sufix].to_s.empty?
        end # line.match(PARADIGM_RX)
        
      ########################################
      # paradigm state
      ########################################
      elsif state == :paradigm
        
        # exit paradigm state
        ########################################
        if line =~ PARADIGM_END_RX
          state = nil
          next
        end
        
        # conjugation
        ########################################
        if line_match = line.match(CONJUGATION_RX)
          paradigm[:conjugations][line_match[1].strip] = line_match[2].strip.split(':')
          
        # verb
        ########################################
        else
          verb = line.strip
          paradigm[:verbs] << verb
          @verbs[verb] = paradigm
        end
      end
    end # file.each_line
  end # File.open
end

.sufixesObject



105
106
107
# File 'lib/conjugue/verb_database.rb', line 105

def sufixes
  @sufixes
end

.verbsObject



101
102
103
# File 'lib/conjugue/verb_database.rb', line 101

def verbs
  @verbs
end