Skip to content

Commit

Permalink
Refactored NounInflector and project layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
ronhopper committed Jun 29, 2011
1 parent 3536a6f commit e2c58dd
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 216 deletions.
7 changes: 6 additions & 1 deletion lib/homer.rb
@@ -1 +1,6 @@
require 'noun_inflector'
$: << File.dirname(__FILE__)

require 'homer/greek_string'
require 'homer/core_ext'
require 'homer/article_inflector'
require 'homer/noun_inflector'
File renamed without changes.
1 change: 1 addition & 0 deletions lib/homer/core_ext.rb
@@ -0,0 +1 @@
String.class_eval { include GreekString }
71 changes: 71 additions & 0 deletions lib/homer/greek_string.rb
@@ -0,0 +1,71 @@
# coding: utf-8
module GreekString

GREEK_UPPER = 'ΑᾺΆἈἊἌἎἉἋἍἏᾼΒΓΔΕῈΈἘἚἜἙἛἝΖΗῊΉἨἪἬἮἩἫἭἯῌΘΙῚΊἸἺἼἾἹἻἽἿΚΛΜΝΞΟῸΌὈὊὌὉὋὍΠΡῬΣΣΤΥῪΎὙὛὝὟΦΧΨΩῺΏὨὪὬὮὩὫὭὯῼ'
GREEK_LOWER = 'αὰάἀἂἄἆἁἃἅἇᾳβγδεὲέἐἒἔἑἓἕζηὴήἠἢἤἦἡἣἥἧῃθιὶίἰἲἴἶἱἳἵἷκλμνξοὸόὀὂὄὁὃὅπρῥςστυὺύὑὓὕὗφχψωὼώὠὢὤὦὡὣὥὧῳ'

GREEK_ACCENTED = 'ὰάᾶἂἄἆἃἅἇᾲᾴᾷὲέἒἔἓἕὴήῆἢἤἦἣἥἧῂῄῇὶίῖἲἴἶἳἵἷὸόὂὄὃὅὺύῦὒὔὖὓὕὗὼώῶὢὤὦὣὥὧῲῴῷ'
GREEK_UNACCENTED = 'αααἀἀἀἁἁἁᾳᾳᾳεεἐἐἑἑηηηἠἠἠἡἡἡῃῃῃιιιἰἰἰἱἱἱοοὀὀὁὁυυυὐὐὐὑὑὑωωωὠὠὠὡὡὡῳῳῳ'

GREEK_VOWELS_UNACCENTED = 'αἀἁᾳεἐἑηἠἡῃιἰἱοὀὁυὐὑωὠὡῳ'
GREEK_VOWELS_WITH_OXIA = 'άἄἅᾴέἔἕήἤἥῄίἴἵόὄὅύὔὕώὤὥῴ'
GREEK_VOWELS_WITH_PERISPOMENE = 'ᾶἆἇᾷ***ῆἦἧῇῖἶἷ***ῦὖὗῶὦὧῷ'

GREEK_DIPTHONG_REGEXP = /[αᾳεο][ιἰἱυὐὑ]|υ[ιἰἱ]|[ηῃ][υὐὑ]/

def downcase_greek
tr(GREEK_UPPER, GREEK_LOWER).sub(/σ$/, 'ς')
end

def upcase_greek
tr(GREEK_LOWER, GREEK_UPPER)
end

def remove_greek_accent
tr(GREEK_ACCENTED, GREEK_UNACCENTED)
end

def add_greek_oxytone
s = remove_greek_accent
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/)
s[i] = s[i].tr(GREEK_VOWELS_UNACCENTED, GREEK_VOWELS_WITH_OXIA)
s
end

def add_greek_paroxytone
s = remove_greek_accent
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/) - 1
i -= 1 if s[i, 2] =~ GREEK_DIPTHONG_REGEXP
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/, i)
s[i] = s[i].tr(GREEK_VOWELS_UNACCENTED, GREEK_VOWELS_WITH_OXIA)
s
end

def add_greek_proparoxytone
s = remove_greek_accent
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/) - 1
i -= 1 if s[i, 2] =~ GREEK_DIPTHONG_REGEXP
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/, i) - 1
i -= 1 if s[i, 2] =~ GREEK_DIPTHONG_REGEXP
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/, i)
s[i] = s[i].tr(GREEK_VOWELS_UNACCENTED, GREEK_VOWELS_WITH_OXIA)
s
end

def add_greek_perispomenon
s = remove_greek_accent
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/)
s[i] = s[i].tr(GREEK_VOWELS_UNACCENTED, GREEK_VOWELS_WITH_PERISPOMENE)
s
end

def add_greek_properispomenon
s = remove_greek_accent
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/) - 1
i -= 1 if s[i, 2] =~ GREEK_DIPTHONG_REGEXP
i = s.rindex(/[#{GREEK_VOWELS_UNACCENTED}]/, i)
s[i] = s[i].tr(GREEK_VOWELS_UNACCENTED, GREEK_VOWELS_WITH_PERISPOMENE)
s
end

end
94 changes: 94 additions & 0 deletions lib/homer/noun_inflector.rb
@@ -0,0 +1,94 @@
# coding: utf-8
module NounInflector

def self.inflect(lemma, inflection)
case lemma
when /[αάηή]ς?/ then inflect_d1(lemma, inflection)
when /[οό][ςν]$/ then inflect_d2(lemma, inflection)
end
end

private

def self.inflect_d1(lemma, inflection)
case kind = lemma[-2, 2].remove_greek_accent
when /.η/ then kind = 'Xη'
when /[βγδζθκλμνξπστφχψ]α/ then kind = 'Cα'
when /.α/ then kind = 'Vα'
end
ivow = (kind[-1] == 'ς' ? -2 : -1)
form = lemma[0...ivow] + D1_ENDINGS[kind.to_sym][inflection]
if 'άή'.include?(lemma[ivow])
if [:gs, :ds, :gp, :dp].include?(inflection)
form.add_greek_perispomenon
else
form.add_greek_oxytone
end
elsif inflection == :gp
form.add_greek_perispomenon
elsif [:gs, :ds, :dp].include?(inflection) || (kind == 'Vα' && inflection == :ap)
form.add_greek_paroxytone
elsif kind == 'Xη' && inflection == :np
form.add_greek_properispomenon
else
form
end
end

def self.inflect_d2(lemma, inflection)
form = lemma[0..-3] + D2_ENDINGS[lemma[-1].to_sym][inflection]
if lemma[-2] == 'ό'
if [:gs, :ds, :gp, :dp].include?(inflection)
form.add_greek_perispomenon
else
form.add_greek_oxytone
end
elsif [:gs, :ds, :gp, :dp].include?(inflection) || (lemma[-1] == 'ς' && inflection == :ap)
form.add_greek_paroxytone
else
form
end
end

D1_ENDINGS = {
:Vα => { :ns => 'α', :np => 'αι',
:gs => 'ας', :gp => 'ων',
:ds => 'ᾳ', :dp => 'αις',
:as => 'αν', :ap => 'ας',
:vs => 'α', :vp => 'αι' },
:Xη => { :ns => 'η', :np => 'αι',
:gs => 'ης', :gp => 'ων',
:ds => 'ῃ', :dp => 'αις',
:as => 'ην', :ap => 'ας',
:vs => 'η', :vp => 'αι' },
:Cα => { :ns => 'α', :np => 'αι',
:gs => 'ης', :gp => 'ων',
:ds => 'ῃ', :dp => 'αις',
:as => 'αν', :ap => 'ας',
:vs => 'α', :vp => 'αι' },
:ας => { :ns => 'ας', :np => 'αι',
:gs => 'ου', :gp => 'ων',
:ds => 'ᾳ', :dp => 'αις',
:as => 'αν', :ap => 'ας',
:vs => 'α', :vp => 'αι' },
:ης => { :ns => 'ης', :np => 'αι',
:gs => 'ου', :gp => 'ων',
:ds => 'ῃ', :dp => 'αις',
:as => 'ην', :ap => 'ας',
:vs => 'α', :vp => 'αι' }
}

D2_ENDINGS = {
=> { :ns => 'ος', :np => 'οι',
:gs => 'ου', :gp => 'ων',
:ds => 'ῳ', :dp => 'οις',
:as => 'ον', :ap => 'ους',
:vs => 'ε', :vp => 'οι' },
=> { :ns => 'ον', :np => 'α',
:gs => 'ου', :gp => 'ων',
:ds => 'ῳ', :dp => 'οις',
:as => 'ον', :ap => 'α',
:vs => 'ον', :vp => 'α' }
}

end
212 changes: 0 additions & 212 deletions lib/noun_inflector.rb

This file was deleted.

0 comments on commit e2c58dd

Please sign in to comment.