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.2.0".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ MedievalLatina

Returns a new instance of MedievalLatina.



127
128
129
130
# File 'lib/medieval_latina.rb', line 127

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

Class Method Details

.[](text) ⇒ Object



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

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

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

  rejoin_words(prepared_words)
end

.adjective?(word) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.adjective?(word)
  part?(word, "Adjective")
end

.adjectivesObject



76
77
78
79
80
# File 'lib/medieval_latina.rb', line 76

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

.adverb?(word) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.adverb?(word)
  part?(word, "Adverb")
end

.adverbsObject



82
83
84
85
86
# File 'lib/medieval_latina.rb', line 82

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

.dictionaryObject



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

def self.dictionary
  @data ||= load_data
end

.entry_for(word) ⇒ Object

Exact spelling first, then a macron-stripped fallback.



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

def self.entry_for(word)
  key = word.to_s.downcase
  DICTIONARY[key] || NORMALIZED[I18n.transliterate(key)]
end

.load_dataObject



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

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)


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

def self.noun?(word)
  part?(word, "Noun")
end

.nounsObject



88
89
90
91
92
# File 'lib/medieval_latina.rb', line 88

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

.part?(word, part) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.part?(word, part)
  entry = entry_for(prepare_word(word))
  !entry.nil? && entry["part"] == part
end

.prepare_text(text) ⇒ Object



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

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



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

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

.pronunciations_for(words) ⇒ Object



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

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

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

.rejoin_words(array) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/medieval_latina.rb', line 104

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

.verb?(word) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.verb?(word)
  part?(word, "Verb")
end

.verbsObject



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

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

.word?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.wordsObject



123
124
125
# File 'lib/medieval_latina.rb', line 123

def self.words
  DICTIONARY.keys.to_set
end

Instance Method Details

#callObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/medieval_latina.rb', line 132

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