Class: MedievalLatina
- Inherits:
-
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
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)
metadata = DICTIONARY.fetch(string, {})
if metadata.key?("pronunciation")
metadata["pronunciation"]
else
new(string).call
end
else
string
end
end
rejoin_words(prepared_words)
end
|
.adjective?(word) ⇒ Boolean
50
51
52
|
# File 'lib/medieval_latina.rb', line 50
def self.adjective?(word)
adjectives.key?(prepare_word(word))
end
|
.adjectives ⇒ Object
66
67
68
69
70
|
# File 'lib/medieval_latina.rb', line 66
def self.adjectives
DICTIONARY.select do |word, metadata|
metadata["part"] == "Adjective"
end
end
|
.adverb?(word) ⇒ Boolean
54
55
56
|
# File 'lib/medieval_latina.rb', line 54
def self.adverb?(word)
adverbs.key?(prepare_word(word))
end
|
.adverbs ⇒ Object
72
73
74
75
76
|
# File 'lib/medieval_latina.rb', line 72
def self.adverbs
DICTIONARY.select do |word, metadata|
metadata["part"] == "Adverb"
end
end
|
.dictionary ⇒ Object
27
28
29
|
# File 'lib/medieval_latina.rb', line 27
def self.dictionary
@data ||= load_data
end
|
.load_data ⇒ Object
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
58
59
60
|
# File 'lib/medieval_latina.rb', line 58
def self.noun?(word)
nouns.key?(prepare_word(word))
end
|
.nouns ⇒ Object
78
79
80
81
82
|
# File 'lib/medieval_latina.rb', line 78
def self.nouns
DICTIONARY.select do |word, metadata|
metadata["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|
metadata = DICTIONARY[word]
if metadata && metadata["ipa"]
hash[word] = metadata["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
62
63
64
|
# File 'lib/medieval_latina.rb', line 62
def self.verb?(word)
verbs.key?(prepare_word(word))
end
|
.verbs ⇒ Object
103
104
105
106
107
|
# File 'lib/medieval_latina.rb', line 103
def self.verbs
DICTIONARY.select do |word, metadata|
metadata["part"] == "Verb"
end
end
|
.word?(string) ⇒ Boolean
109
110
111
|
# File 'lib/medieval_latina.rb', line 109
def self.word?(string)
string.match?(/\w/)
end
|
.words ⇒ Object
113
114
115
|
# File 'lib/medieval_latina.rb', line 113
def self.words
DICTIONARY.keys.to_set
end
|
Instance Method Details
#call ⇒ Object
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
|