Module: HaikuGadget::Dictionary
- Defined in:
- lib/haiku_gadget/dictionary.rb
Constant Summary collapse
- DEFAULT_DICT_PATH =
'../words.yml'- WORD_TYPES =
{ pronoun: WordType.new(:pronoun, true), # pronoun_pre_singular_verb: WordType.new(:pronoun_pre_singular_verb), # pronoun_pre_plural_verb: WordType.new(:pronoun_pre_plural_verb), pronoun_post: WordType.new(:pronoun_post), determiner: WordType.new(:determiner, true), to_be: WordType.new(:to_be, true), adjective: WordType.new(:adjective, true), adjective_adverb: WordType.new(:adjective_adverb), noun: WordType.new(:noun, true, :plural), mass_noun: WordType.new(:mass_noun), mass_noun_determiner: WordType.new(:mass_noun_determiner), verb: WordType.new(:verb, true, :singular), verb_self: WordType.new(:verb_self, true, :singular), verb_adverb: WordType.new(:verb_adverb), transition_join: WordType.new(:transition_join) }
Class Method Summary collapse
- .add_s_to_all(word_list) ⇒ Object
-
.complete_plurality(dict, word_type) ⇒ Object
copies words from _common into _singular and _plural modifies dict structure in place, doesn’t return anything of consequence.
- .contains_valid_data_types? ⇒ Boolean
- .contains_word_anywhere?(word) ⇒ Boolean
-
.get_word(word_type, syllables, plurality = :none) ⇒ Object
if available, returns a random word of the given type and number of syllables.
-
.init ⇒ Object
load dictionary if not yet done, using default dictionary yaml path.
-
.init!(path = DEFAULT_DICT_PATH) ⇒ Object
force load/reload of dictionary, optionally passing a custom path to a dictionary yaml file.
-
.load(path = DEFAULT_DICT_PATH) ⇒ Object
force load of dictionary from yaml file now.
- .suffixed_symbol(base_symbol, suffix_symbol) ⇒ Object
- .words?(word_type, syllables, plurality = :none) ⇒ Boolean
Class Method Details
.add_s_to_all(word_list) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/haiku_gadget/dictionary.rb', line 84 def self.add_s_to_all(word_list) out = [] word_list.each do |word_or_array| if word_or_array.is_a? Array # recursively call again and add the nested array out << Dictionary.add_s_to_all(word_or_array) else out << "#{word_or_array}s" end end out end |
.complete_plurality(dict, word_type) ⇒ Object
copies words from _common into _singular and _plural modifies dict structure in place, doesn’t return anything of consequence
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 79 80 81 82 |
# File 'lib/haiku_gadget/dictionary.rb', line 35 def self.complete_plurality(dict, word_type) # words that have no plurality considerations don't need to do anything here return unless word_type.can_be_plural base_symbol = word_type.base_symbol common_symbol = Dictionary.suffixed_symbol base_symbol, :common plural_symbols = [:singular, :plural] symbols = [ Dictionary.suffixed_symbol(base_symbol, :singular), Dictionary.suffixed_symbol(base_symbol, :plural) ] # make sure there is a common list structure dict[common_symbol] ||= [] # make sure there are singular/plural word list structures symbols.each do |symbol| dict[symbol] ||= [] end plural_symbols.each do |plural_symbol| symbol = Dictionary.suffixed_symbol base_symbol, plural_symbol # make sure singular and plural structures exist dict[symbol] ||= [] # make array lengths equal to the max of the common, singular and plural arrays while dict[symbol].length < [symbols.map { |s| dict[s].length }, dict[common_symbol].length].flatten.max dict[symbol] << [] end # add common words to current word list, respecting syllables and plurality dict[symbol].each_index do |i| if word_type.add_s_target == plural_symbol # currently building a list in which the words should have an 's' added when copying from _common dict[symbol][i] += Dictionary.add_s_to_all(dict[common_symbol][i]) if dict[common_symbol][i] else # not adding an 's' in this case, or building the singular word list dict[symbol][i] += dict[common_symbol][i] if dict[common_symbol][i] end end end end |
.contains_valid_data_types? ⇒ Boolean
104 105 106 107 108 109 110 111 112 |
# File 'lib/haiku_gadget/dictionary.rb', line 104 def self.contains_valid_data_types? @@dict.each do |key, array| array.flatten.each do |item| puts "error: #{item} in #{key.to_s} is a #{item.class}" if item.class != String return false unless item.is_a? String end end true end |
.contains_word_anywhere?(word) ⇒ Boolean
97 98 99 100 101 102 |
# File 'lib/haiku_gadget/dictionary.rb', line 97 def self.contains_word_anywhere?(word) @@dict.each do |key, array| return true if array.flatten.include? word end false end |
.get_word(word_type, syllables, plurality = :none) ⇒ Object
if available, returns a random word of the given type and number of syllables
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/haiku_gadget/dictionary.rb', line 136 def self.get_word(word_type, syllables, plurality = :none) Dictionary.init # validate word_type return nil unless word_type # validate syllables input return nil unless syllables.is_a?(Fixnum) && syllables > 0 dict_symbol = word_type.dict_symbol plurality if @@dict[dict_symbol] && @@dict[dict_symbol][syllables - 1] word_or_array = @@dict[dict_symbol][syllables - 1].sample while word_or_array.is_a? Array # this item contains a nested list of items (to limit chances of nested item being selected) # sample from nested array and try again to find a word ("leaf node") word_or_array = word_or_array.sample end # current item is a word and not a nested list, return it word_or_array else nil end end |
.init ⇒ Object
load dictionary if not yet done, using default dictionary yaml path
126 127 128 |
# File 'lib/haiku_gadget/dictionary.rb', line 126 def self.init @@dict ||= load end |
.init!(path = DEFAULT_DICT_PATH) ⇒ Object
force load/reload of dictionary, optionally passing a custom path to a dictionary yaml file
131 132 133 |
# File 'lib/haiku_gadget/dictionary.rb', line 131 def self.init!(path = DEFAULT_DICT_PATH) @@dict = load path end |
.load(path = DEFAULT_DICT_PATH) ⇒ Object
force load of dictionary from yaml file now
115 116 117 118 119 120 121 122 123 |
# File 'lib/haiku_gadget/dictionary.rb', line 115 def self.load(path = DEFAULT_DICT_PATH) dict = YAML.load_file(File.(path, File.dirname(__FILE__))) WORD_TYPES.each do |k, wt| Dictionary.complete_plurality(dict, wt) end dict end |
.suffixed_symbol(base_symbol, suffix_symbol) ⇒ Object
29 30 31 |
# File 'lib/haiku_gadget/dictionary.rb', line 29 def self.suffixed_symbol(base_symbol, suffix_symbol) "#{base_symbol.to_s}_#{suffix_symbol.to_s}".to_sym end |
.words?(word_type, syllables, plurality = :none) ⇒ Boolean
161 162 163 |
# File 'lib/haiku_gadget/dictionary.rb', line 161 def self.words?(word_type, syllables, plurality = :none) !Dictionary.get_word(word_type, syllables, plurality).nil? end |