Sha256: 43b0e2081dd3f4b6b23843ee3b5b2e86b0670ae92cfc4763942c3a626b3b917c

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

module Sastrawi
  module Dictionary
    class ArrayDictionary
      attr_reader :words

      def initialize(words = [])
        @words = []

        add_words(words)
      end

      ##
      # Check whether a word is contained in the dictionary

      def contains?(word)
        @words.include?(word)
      end

      ##
      # Count how many words in the dictionary

      def count
        @words.length
      end

      ##
      # Add multiple words to the dictionary

      def add_words(new_words)
        new_words.each do |word|
          add(word)
        end
      end

      ##
      # Add a word to the dictionary

      def add(word)
        return if word == ''

        @words.push(word)
      end

      ##
      # Add words from a text file to the dictionary

      def add_words_from_text_file(file_path)
        words = []

        File.open(file_path, 'r') do |file|
          file.each do |line|
            words.push(line.chomp)
          end
        end

        add_words(words)
      end

      ##
      # Remove a word from the dictionary

      def remove(word)
        @words.delete(word)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sastrawi-0.1.2 lib/sastrawi/dictionary/array_dictionary.rb
sastrawi-0.1.1 lib/sastrawi/dictionary/array_dictionary.rb