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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ MedievalLatina

Returns a new instance of MedievalLatina.



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

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)
       = 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)


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

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

.adjectivesObject



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

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

.adverb?(word) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.adverbsObject



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

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

.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)


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

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

.nounsObject



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

def self.nouns
  DICTIONARY.select do |word, |
    ["part"] == "Noun"
  end
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



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

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



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

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

.verb?(word) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.verbsObject



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

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

.word?(string) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.wordsObject



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

def self.words
  DICTIONARY.keys.to_set
end

Instance Method Details

#callObject



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

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