Module: RussianReversal

Defined in:
lib/russian-reversal.rb

Constant Summary collapse

REGEXP_NONWORD =
/[^a-zA-Z-]/
VERBS_IGNORED =
[ 'be', 'have', ]

Class Method Summary collapse

Class Method Details

.infinitive_of(verb) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/russian-reversal.rb', line 15

def self.infinitive_of( verb )
  doc = Nokogiri::HTML( open( "http://www.oxfordadvancedlearnersdictionary.com/dictionary/#{verb}" ) )
  doc.search('div#relatedentries > ul > li').each do |e|
    pos = e.at('span.pos')
    if pos && pos.text.gsub( REGEXP_NONWORD, '' ) == 'verb'
      v = e.at('span').children.first.text.strip
      if VERBS_IGNORED.include?( v )
        return nil
      else
        return v
      end
    end
  end
end

.plural_of(noun) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/russian-reversal.rb', line 30

def self.plural_of( noun )
  doc = Nokogiri::HTML( open( "http://www.oxfordadvancedlearnersdictionary.com/dictionary/#{noun}" ) )

  plural_marker = doc.at('span.z_il')
  if plural_marker
    return plural_marker.parent.at('span.if').text
  end

  plural_marker = doc.at('span.xr')
  if plural_marker && plural_marker.text =~ /plural of/
    return noun
  end

  doc.search('div#relatedentries > ul > li').each do |e|
    if e.at('span.pos').text.gsub( REGEXP_NONWORD, '' ) == 'noun'
      return e.at('span').children.first.text.strip + 's'
    end
  end

  nil
end

.reverse(s) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/russian-reversal.rb', line 52

def self.reverse( s )
  sentence = s.en.sentence

  verb_ = nil
  if verb_.nil?
    if sentence.verb
      verb_ = strip( sentence.verb )
    end
  end
  sentence.linkages[0].words.each do |w|
    if w =~ /^(.+)\.v$/
      verb_ = $1
      break
    end
  end
  return  if verb_.nil?

  verb = infinitive_of( verb_ )
  return  if verb.nil?

  return  if sentence.object.nil?
  object = strip( sentence.object )
  return  if object =~ REGEXP_NONWORD
  plural = plural_of( object )
  return  if plural.nil?

  "#{plural} #{verb}"
end

.strip(s) ⇒ Object



11
12
13
# File 'lib/russian-reversal.rb', line 11

def self.strip( s )
  s.gsub( /\..*$/, '' )
end