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

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



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

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



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

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



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

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

#has_prefix?(translitered) ⇒ Boolean

  • Check if translitered word has prefix

  • translitered

    Translitered word to be checked

Returns:

  • (Boolean)


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

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)


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

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)


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

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

#prefixesObject

  • Returns the prefixes table



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

def prefixes
  @@prefixes
end

#prefixes=(prefixes) ⇒ Object



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

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)


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

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)


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

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

#stemsObject

  • Returns Stems Dictionary



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

def stems
  @@stems
end

#stems=(stems) ⇒ Object



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

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)


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

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

#suffixesObject

  • Returns Suffixes Dictionary



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

def suffixes
  @@suffixes
end

#suffixes=(suffixes) ⇒ Object



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

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