Class: LittleWeasel::Dictionary
- Inherits:
-
Object
- Object
- LittleWeasel::Dictionary
- Includes:
- Filters::WordFilterManagable, Modules::Configurable, Modules::DictionaryCacheServicable, Modules::DictionaryKeyable, Modules::DictionaryMetadataServicable, Preprocessors::WordPreprocessorManagable
- Defined in:
- lib/LittleWeasel/dictionary.rb
Instance Attribute Summary collapse
-
#dictionary_metadata_object ⇒ Object
Returns the value of attribute dictionary_metadata_object.
-
#dictionary_words ⇒ Object
Returns the value of attribute dictionary_words.
Attributes included from Preprocessors::WordPreprocessable
Attributes included from Modules::DictionaryMetadataServicable
#dictionary_cache, #dictionary_key, #dictionary_metadata
Attributes included from Modules::DictionaryKeyable
Attributes included from Modules::DictionaryCacheServicable
#dictionary_cache, #dictionary_key
Attributes included from Filters::WordFilterable
Class Method Summary collapse
Instance Method Summary collapse
- #block_results(word_block) ⇒ Object
-
#count ⇒ Object
This method returns a count of VALID words in the dictionary.
-
#count_all_words ⇒ Object
This method returns a count of all VALID and INVALID words in the dictionary.
-
#count_invalid_words ⇒ Object
This method returns a count of all INVALID words in the dictionary.
- #create_dictionary_metadata ⇒ Object private
-
#detached? ⇒ Boolean
This method returns true if this dictionary object is detached from the dictionary cache; this can happen if the dictionary object is unloaded from the dictionary cache(DictionaryManager#unload_dictionary) or the dictionary is killed (DictionaryManager#kill_dictionary).
-
#initialize(dictionary_key:, dictionary_words:, dictionary_cache:, dictionary_metadata:, word_filters: nil, word_preprocessors: nil) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #word_results(word) ⇒ Object
Methods included from Preprocessors::WordPreprocessorManagable
#add_preprocessors, #clear_preprocessors, #concat_and_sort_word_preprocessors!, #preprocess, #preprocessed_word, #preprocessed_words, #preprocessors_on=, #replace_preprocessors, #word_preprocessors
Methods included from Preprocessors::WordPreprocessorsValidatable
#validate_word_preprocessors, validate_word_preprocessors
Methods included from Modules::DictionaryMetadataServicable
Methods included from Modules::DictionaryMetadataValidatable
validate, #validate_dictionary_metadata
Methods included from Modules::DictionaryCacheValidatable
validate, #validate_dictionary_cache
Methods included from Modules::DictionaryKeyValidatable
validate, #validate_dictionary_key
Methods included from Modules::DictionaryCacheServicable
Methods included from Modules::Configurable
Methods included from Filters::WordFilterManagable
#add_filters, #clear_filters, #filter_match?, #filters_matched, #filters_on=, #replace_filters, #word_filters
Methods included from Filters::WordFiltersValidatable
validate, #validate_word_filters
Methods included from Filters::WordFilterValidatable
valid_word_filter?, validate, #validate_word_filter
Constructor Details
#initialize(dictionary_key:, dictionary_words:, dictionary_cache:, dictionary_metadata:, word_filters: nil, word_preprocessors: nil) ⇒ Dictionary
Returns a new instance of Dictionary.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/LittleWeasel/dictionary.rb', line 24 def initialize(dictionary_key:, dictionary_words:, dictionary_cache:, dictionary_metadata:, word_filters: nil, word_preprocessors: nil) validate_dictionary_key dictionary_key: dictionary_key self.dictionary_key = dictionary_key validate_dictionary_cache dictionary_cache: dictionary_cache self.dictionary_cache = dictionary_cache dictionary_metadata: self. = unless dictionary_words.is_a?(Array) raise ArgumentError, "Argument dictionary_words is not an Array: #{dictionary_words.class}" end # Set up the dictionary metadata object and observers self.dictionary_words = self.class.to_hash(dictionary_words: dictionary_words) self. = .add_observers add_filters word_filters: word_filters || [] add_preprocessors word_preprocessors: word_preprocessors || [] end |
Instance Attribute Details
#dictionary_metadata_object ⇒ Object
Returns the value of attribute dictionary_metadata_object.
22 23 24 |
# File 'lib/LittleWeasel/dictionary.rb', line 22 def @dictionary_metadata_object end |
#dictionary_words ⇒ Object
Returns the value of attribute dictionary_words.
22 23 24 |
# File 'lib/LittleWeasel/dictionary.rb', line 22 def dictionary_words @dictionary_words end |
Class Method Details
.to_hash(dictionary_words:) ⇒ Object
50 51 52 |
# File 'lib/LittleWeasel/dictionary.rb', line 50 def to_hash(dictionary_words:) dictionary_words.each_with_object(Hash.new(false)) { |word, hash| hash[word] = true; } end |
Instance Method Details
#block_results(word_block) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/LittleWeasel/dictionary.rb', line 74 def block_results(word_block) # TODO: Make max word_block size configurable. raise ArgumentError, "Argument word_block is not a String: #{word_block.class}" unless word_block.is_a?(String) raise ArgumentError, "Argument word_block is empty: #{word_block.class}" unless word_block.present? BlockResults.new(original_word_block: word_block).tap do |block_results| word_block.scan(config.word_block_regex)&.map do |word| block_results << word_results(word) end end end |
#count ⇒ Object
This method returns a count of VALID words in the dictionary.
95 96 97 |
# File 'lib/LittleWeasel/dictionary.rb', line 95 def count dictionary_words.each_pair.count { |_word, valid| valid } end |
#count_all_words ⇒ Object
This method returns a count of all VALID and INVALID words in the dictionary.
101 102 103 |
# File 'lib/LittleWeasel/dictionary.rb', line 101 def count_all_words dictionary_words.count end |
#count_invalid_words ⇒ Object
This method returns a count of all INVALID words in the dictionary.
106 107 108 |
# File 'lib/LittleWeasel/dictionary.rb', line 106 def count_invalid_words dictionary_words.each_pair.count { |_word, valid| !valid } end |
#create_dictionary_metadata ⇒ Object (private)
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/LittleWeasel/dictionary.rb', line 114 def # We unconditionally attach metadata to this dictionary. DictionaryMetadata # only attaches the metadata services that are turned "on". Metadata::DictionaryMetadata.new( dictionary_words: dictionary_words, dictionary_key: dictionary_key, dictionary_cache: dictionary_cache, dictionary_metadata: ) end |
#detached? ⇒ Boolean
This method returns true if this dictionary object is detached from the dictionary cache; this can happen if the dictionary object is unloaded from the dictionary cache(DictionaryManager#unload_dictionary) or the dictionary is killed (DictionaryManager#kill_dictionary).
90 91 92 |
# File 'lib/LittleWeasel/dictionary.rb', line 90 def detached? !dictionary_cache_service.dictionary_object? end |
#word_results(word) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/LittleWeasel/dictionary.rb', line 55 def word_results(word) # TODO: Make max word size configurable. raise ArgumentError, "Argument word is not a String: #{word.class}" unless word.is_a?(String) preprocessed_words = preprocess(word: word) preprocessed_word = preprocessed_words.preprocessed_word filters_matched = filters_matched(preprocessed_word || word) word_results = WordResults.new(original_word: word, filters_matched: filters_matched, preprocessed_words: preprocessed_words, word_cached: dictionary_words.include?(preprocessed_word || word), word_valid: dictionary_words[preprocessed_word || word] || false) .notify(action: :word_search, params: { word_results: word_results }) word_results end |