Class: InMemoryDictionaryHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/raramorph/in_memory_dictionary_handler.rb

Constant Summary collapse

@@prefixes =

Dictionaries are HASH OF ARRAYS #####

{}
@@stems =

Dictionary of Prefixes

{}
@@suffixes =

Dictionary of Stems

{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.createObject

  • Loads Dictionaries and initiate variables



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
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 23

def self.create 
  
  ### Variables #####
    @@handler  = nil
    @@regex = Regexp.compile(".*" + "<pos>(.+?)</pos>" + ".*")
    @@morphology_regexs=[Regexp.compile("^(Pref-0|Suff-0)$") , 
                         Regexp.compile("^F" + ".*") ,
                         Regexp.compile("^IV" + ".*") ,
                         Regexp.compile("^PV" + ".*") ,
                         Regexp.compile("^CV" + ".*") ,
                         Regexp.compile("^N" + ".*") , 
                         Regexp.compile("^[A-Z]" + ".*") , 
                         Regexp.compile(".*" + "iy~$") 
                         ]  
    @@compatability_stpliter = Regexp.compile("\\s+")            
    @@vocalization_array =["/FUNC_WORD" ,  
                           "/VERB_IMPERFECT" , 
                          "/VERB_PERFECT" , 
                          "/VERB_IMPERATIVE" , 
                          "/NOUN_PROP" ,
                          "/NOUN" , 
                          "/NOUN"
                             ]

    @@prefixes_stems_compatibility = Set.new
  #Changed
  #Compatibility table for prefixes-stems combinations.

    @@prefixes_suffixes_compatibility = Set.new
  #Changed
  #Compatibility table for prefixes-suffixes combinations.

    @@stems_suffixes_compatibility = Set.new
    
  #Changed
  #Compatibility table for stem-suffixes combinations.

     puts "Initializing in-memory dictionary handler..."
     Thread.abort_on_exception = true
     load_dictionary( @@prefixes , "dictPrefixes"  ,  File.dirname(__FILE__) + "/../dictionaries/dictPrefixes"  )
     load_stems_marshaled_dictionary
     load_dictionary(@@suffixes, "dictSuffixes" ,  File.dirname(__FILE__) + "/../dictionaries/dictSuffixes")
     load_compatibility_table(@@prefixes_stems_compatibility , "prefixes_stems_compatibility" ,  File.dirname(__FILE__) + "/../dictionaries/tableAB")
     load_compatibility_table(@@prefixes_suffixes_compatibility , "prefixes_suffixes_compatibility" ,  File.dirname(__FILE__) + "/../dictionaries/tableAC")
     load_compatibility_table(@@stems_suffixes_compatibility , "stems_suffixes_compatibility" ,  File.dirname(__FILE__) + "/../dictionaries/tableBC")
     puts "... Done ... "
           @@handler = new unless @@handler
end

.load_stems_marshaled_dictionaryObject

  • load the marshaled stems dictionary if avalaible or load from the origin dictionary if not avalaible



73
74
75
76
77
78
79
80
81
82
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 73

def self.load_stems_marshaled_dictionary 
   if File.exists?( File.dirname(__FILE__) + '/../dictionaries/marshal_stems' ) 
    File.open( File.dirname(__FILE__) + '/../dictionaries/marshal_stems') do |f|  
       @@stems =  Marshal.load(f)
     end
      puts("#{@@stems.length}  entries totalizing")
   else
     reload_stems_dictionary
   end         
end

.marshal_stemsObject

  • Marshal the stems dictionary into a file



85
86
87
88
89
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 85

def self.marshal_stems
   File.open( File.dirname(__FILE__) + '/../dictionaries/marshal_stems' , 'w+') do |f|  
      Marshal.dump(@@stems, f)  
    end   
end

.reload_stems_dictionaryObject

  • Loads Stem dictionary from original file then marshal the dictionary for faster access



93
94
95
96
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 93

def self.reload_stems_dictionary
  load_dictionary(@@stems, "dictStems",  File.dirname(__FILE__) + "/../dictionaries/dictStems") #File.open("dictionaries/dictStems" ,  "r:UTF-8" ))
  marshal_stems
end

Instance Method Details

#analyze_word_in_dictionaries(segmented_word, word_solutions, verbose, count) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 165

def analyze_word_in_dictionaries(segmented_word , word_solutions , verbose  , count)
     #Is prefix known ?
     if has_prefix?(segmented_word.prefix)
       #Is stem known ?
       # puts "has prefix"
       if has_stem?(segmented_word.stem)
        # puts "has stem"
         #Is suffix known ?
         if has_suffix?(segmented_word.suffix)
         #  puts "has suffix"
           #Compatibility check
            @@prefixes[segmented_word.prefix].each{|prefix|
              @@stems[segmented_word.stem].each {|stem|
                #Prefix/Stem compatibility
                  if prefixes_stems_compatible?(prefix.morphology ,stem.morphology )
                    # puts "has A B Com" 
                    @@suffixes[segmented_word.suffix].each {|suffix|
                     # Prefix/Suffix compatiblity
                     if prefixes_suffixes_compatible?(prefix.morphology , suffix.morphology)
                       # puts "has A C Com"
                        # Stems/Suffixes compatiblity
                       if stems_suffixes_compatible?(stem.morphology , suffix.morphology)
                        # puts "has  B  C COM"
                          #All tests passed : it is a solution
                          count = count + 1
                          word_solutions << Solution.new(verbose , count , prefix , stem , suffix )
                       end
                     end
                    }
                  end
              }
            }
         end
       end
     end
  return count
end

#has_prefix?(translitered) ⇒ Boolean

  • Check if translitered word has prefix

  • translitered

    Translitered word to be checked

Returns:

  • (Boolean)


100
101
102
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 100

def has_prefix?(translitered)
 	@@prefixes.has_key?(translitered)
end

#has_stem?(translitered) ⇒ Boolean

  • Check if translitered word has stem

  • translitered

    Translitered word to be checked

Returns:

  • (Boolean)


106
107
108
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 106

def has_stem?(translitered)
  @@stems.has_key?(translitered)
end

#has_suffix?(translitered) ⇒ Boolean

  • Check if translitered word has suffix

  • translitered

    Translitered word to be checked

Returns:

  • (Boolean)


112
113
114
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 112

def has_suffix?(translitered)
  @@suffixes.has_key?(translitered)
end

#prefixesObject

  • Returns the prefixes table



138
139
140
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 138

def prefixes
  @@prefixes
end

#prefixes=(prefixes) ⇒ Object



142
143
144
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 142

def prefixes=(prefixes)
  @@prefixes = prefixes
end

#prefixes_stems_compatible?(prefix, stem) ⇒ Boolean

  • Check if prefix and stem are compatible

  • prefix

    prefix to be checked

  • stem

    stem to be checked

Returns:

  • (Boolean)


119
120
121
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 119

def prefixes_stems_compatible?(prefix , stem) #String , #String
  @@prefixes_stems_compatibility.member?(prefix + " " + stem)
end

#prefixes_suffixes_compatible?(prefix, suffix) ⇒ Boolean

  • Check if prefix and suffix are compatible

  • prefix

    prefix to be checked

  • suffix

    suffix to be checked

Returns:

  • (Boolean)


126
127
128
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 126

def prefixes_suffixes_compatible?(prefix , suffix)
  @@prefixes_suffixes_compatibility.member?(prefix + " " + suffix)
end

#stemsObject

  • Returns Stems Dictionary



147
148
149
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 147

def stems
  @@stems
end

#stems=(stems) ⇒ Object



151
152
153
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 151

def stems=(stems)
  @@stems = stems
end

#stems_suffixes_compatible?(stem, suffix) ⇒ Boolean

  • Check if stem and suffix are compatible

  • stem

    stem to be checked

  • suffix

    suffix to be checked

Returns:

  • (Boolean)


133
134
135
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 133

def stems_suffixes_compatible?(stem , suffix)
  @@stems_suffixes_compatibility.member?(stem + " " + suffix)
end

#suffixesObject

  • Returns Suffixes Dictionary



157
158
159
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 157

def suffixes
  @@suffixes
end

#suffixes=(suffixes) ⇒ Object



161
162
163
# File 'lib/raramorph/in_memory_dictionary_handler.rb', line 161

def suffixes=(suffixes)
  @@suffixes = suffixes
end