Module: RussianReversal

Defined in:
lib/russian-reversal.rb

Constant Summary collapse

REGEXP_NONWORD =
/[^a-zA-Z -]/
VERBS_IGNORED =
[ 'be', 'become', 'can', 'do', 'get', 'go', 'have', 'put', 'say',  ]
NOUNS_IGNORED =
[ 'it', 'him', 'her', 'them', 'me', 'you', 'us', 'this', 'that', 'these', 'those', ]

Class Method Summary collapse

Class Method Details

.infinitive_of(verb) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/russian-reversal.rb', line 16

def self.infinitive_of( verb )
  return  if verb =~ REGEXP_NONWORD

  doc = Nokogiri::HTML( open( "http://www.oxfordadvancedlearnersdictionary.com/dictionary/#{verb}" ) )
  doc.search('div#relatedentries > ul > li').each do |e|
    pos_element = e.at('span.pos')
    next  if pos_element.nil?

    pos = pos_element.text.gsub( REGEXP_NONWORD, '' )
    return  if pos == 'modal verb' || pos == 'auxiliary verb'

    next  if pos != 'verb'

    v = e.at('span').children.first.text.strip
    if VERBS_IGNORED.include?( v )
      return
    else
      return v
    end
  end
end

.plural_of(noun) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/russian-reversal.rb', line 38

def self.plural_of( noun )
  return nil  if NOUNS_IGNORED.include?( noun.downcase )

  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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/russian-reversal.rb', line 62

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



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

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