Class: MedievalLatina

Inherits:
Object
  • Object
show all
Defined in:
lib/medieval_latina.rb,
lib/medieval_latina/lexicon.rb,
lib/medieval_latina/version.rb,
lib/medieval_latina/lexicon_builder.rb

Defined Under Namespace

Classes: Error, Lexicon, LexiconBuilder, Result, Substring

Constant Summary collapse

VERSION =
"3.0.0".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ MedievalLatina

Returns a new instance of MedievalLatina.



117
118
119
120
# File 'lib/medieval_latina.rb', line 117

def initialize(word)
  @index = 0
  @word = I18n.transliterate(word.downcase)
end

Class Method Details

.[](text) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/medieval_latina.rb', line 9

def self.[](text)
  prepared_words = prepare_text(text).map do |string|
    if word?(string)
       = DICTIONARY.fetch(string, {})

      if .key?("pronunciation")
        ["pronunciation"]
      else
        new(string).call
      end
    else
      string
    end
  end

  rejoin_words(prepared_words)
end

.adjective?(word) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/medieval_latina.rb', line 50

def self.adjective?(word)
  adjectives.key?(prepare_word(word))
end

.adjectivesObject



66
67
68
69
70
# File 'lib/medieval_latina.rb', line 66

def self.adjectives
  DICTIONARY.select do |word, |
    ["part"] == "Adjective"
  end
end

.adverb?(word) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/medieval_latina.rb', line 54

def self.adverb?(word)
  adverbs.key?(prepare_word(word))
end

.adverbsObject



72
73
74
75
76
# File 'lib/medieval_latina.rb', line 72

def self.adverbs
  DICTIONARY.select do |word, |
    ["part"] == "Adverb"
  end
end

.dictionaryObject



27
28
29
# File 'lib/medieval_latina.rb', line 27

def self.dictionary
  @data ||= load_data
end

.load_dataObject



31
32
33
34
# File 'lib/medieval_latina.rb', line 31

def self.load_data
  file_path = File.join(File.dirname(__FILE__), "../data/dictionary.json")
  JSON.parse(File.read(file_path))
end

.noun?(word) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/medieval_latina.rb', line 58

def self.noun?(word)
  nouns.key?(prepare_word(word))
end

.nounsObject



78
79
80
81
82
# File 'lib/medieval_latina.rb', line 78

def self.nouns
  DICTIONARY.select do |word, |
    ["part"] == "Noun"
  end
end

.prepare_text(text) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/medieval_latina.rb', line 36

def self.prepare_text(text)
  text.scan(/[\p{Alnum}'-]+|[[:punct:]]+/).map do |string|
    if word?(string)
      prepare_word(string)
    else
      string
    end
  end
end

.prepare_word(word) ⇒ Object



46
47
48
# File 'lib/medieval_latina.rb', line 46

def self.prepare_word(word)
  word.gsub(/\P{Alnum}+/, " ").strip.downcase
end

.pronunciations_for(words) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/medieval_latina.rb', line 84

def self.pronunciations_for(words)
  words.map(&:downcase).each_with_object({}) do |word, hash|
     = DICTIONARY[word]

    if  && ["ipa"]
      hash[word] = ["ipa"]
    end
  end
end

.rejoin_words(array) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/medieval_latina.rb', line 94

def self.rejoin_words(array)
  array
    .join(" ")
    .gsub(/ +?,/, ",")
    .gsub(/ +?;/, ";")
    .gsub(/ +?\./, ".")
    .gsub(/ +?\?/, "?")
end

.verb?(word) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/medieval_latina.rb', line 62

def self.verb?(word)
  verbs.key?(prepare_word(word))
end

.verbsObject



103
104
105
106
107
# File 'lib/medieval_latina.rb', line 103

def self.verbs
  DICTIONARY.select do |word, |
    ["part"] == "Verb"
  end
end

.word?(string) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/medieval_latina.rb', line 109

def self.word?(string)
  string.match?(/\w/)
end

.wordsObject



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

def self.words
  DICTIONARY.keys.to_set
end

Instance Method Details

#callObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/medieval_latina.rb', line 122

def call
  array = []

  until index >= word.length
    substring = Substring.new(word, index)
    result = vowel(substring) || consonant(substring) || Result.new(substring.character, 1)
    array.push(result.substring)
    self.index = index + result.increment_by
  end

  array.join("")
end