russian-reversal 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,73 @@
1
+ require 'linguistics'
2
+ Linguistics::use( :en )
3
+
4
+ require 'open-uri'
5
+ require 'nokogiri'
6
+
7
+ module RussianReversal
8
+ REGEXP_NONWORD = /[^a-zA-Z-]/
9
+
10
+ def self.strip( s )
11
+ s.gsub( /\..*$/, '' )
12
+ end
13
+
14
+ def self.infinitive_of( verb )
15
+ doc = Nokogiri::HTML( open( "http://www.oxfordadvancedlearnersdictionary.com/dictionary/#{verb}" ) )
16
+ doc.search('div#relatedentries > ul > li').each do |e|
17
+ if e.at('span.pos').text.gsub( REGEXP_NONWORD, '' ) == 'verb'
18
+ return e.at('span').children.first.text.strip
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.plural_of( noun )
24
+ doc = Nokogiri::HTML( open( "http://www.oxfordadvancedlearnersdictionary.com/dictionary/#{noun}" ) )
25
+
26
+ plural_marker = doc.at('span.z_il')
27
+ if plural_marker
28
+ return plural_marker.parent.at('span.if').text
29
+ end
30
+
31
+ plural_marker = doc.at('span.xr')
32
+ if plural_marker && plural_marker.text =~ /plural of/
33
+ return noun
34
+ end
35
+
36
+ doc.search('div#relatedentries > ul > li').each do |e|
37
+ if e.at('span.pos').text.gsub( REGEXP_NONWORD, '' ) == 'noun'
38
+ return e.at('span').children.first.text.strip + 's'
39
+ end
40
+ end
41
+
42
+ nil
43
+ end
44
+
45
+ def self.reverse( s )
46
+ sentence = s.en.sentence
47
+
48
+ verb_ = nil
49
+ if verb_.nil?
50
+ if sentence.verb
51
+ verb_ = strip( sentence.verb )
52
+ end
53
+ end
54
+ sentence.linkages[0].words.each do |w|
55
+ if w =~ /^(.+)\.v$/
56
+ verb_ = $1
57
+ break
58
+ end
59
+ end
60
+ return if verb_.nil?
61
+
62
+ verb = infinitive_of( verb_ )
63
+ return if verb.nil?
64
+
65
+ return if sentence.object.nil?
66
+ object = strip( sentence.object )
67
+ return if object =~ REGEXP_NONWORD
68
+ plural = plural_of( object )
69
+ return if plural.nil?
70
+
71
+ "#{plural} #{verb}"
72
+ end
73
+ end
@@ -0,0 +1,9 @@
1
+ __DIR__ = File.dirname( File.expand_path( __FILE__ ) )
2
+ lib_dir = "#{__DIR__}/../lib"
3
+ if $LOAD_PATH[ 0 ] != lib_dir
4
+ $LOAD_PATH.unshift lib_dir
5
+ end
6
+
7
+ require 'rubygems'
8
+ require 'bacon'
9
+ require 'russian-reversal'
data/spec/reverse.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'spec/preparation'
2
+
3
+ describe 'RussianReversal' do
4
+ it 'can make the direct object the subject' do
5
+ RussianReversal.reverse( 'They help Americans' ).should.equal 'Americans help'
6
+ RussianReversal.reverse( 'He eliminated enemies' ).should.equal 'enemies eliminate'
7
+ RussianReversal.reverse( 'He shot an apple' ).should.equal 'apples shoot'
8
+ RussianReversal.reverse( 'He fired the rocket' ).should.equal 'rockets fire'
9
+ RussianReversal.reverse( 'The cats chased the mice' ).should.equal 'mice chase'
10
+ RussianReversal.reverse( 'The cat chases the mouse' ).should.equal 'mice chase'
11
+ RussianReversal.reverse( 'The tanks decimated the platoon.' ).should.equal 'platoons decimate'
12
+ RussianReversal.reverse( 'The chef prepared a delicious meal.' ).should.equal 'meals prepare'
13
+ RussianReversal.reverse( 'The hungry lions were devouring a sheep.' ).should.equal 'sheep devour'
14
+ RussianReversal.reverse( 'he needs a NotARealEnglishWord' ).should.be.nil
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: russian-reversal
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 3
10
+ version: 1.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Pistos
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-18 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: linguistics
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: linkparser
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :runtime
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: nokogiri
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :runtime
62
+ version_requirements: *id003
63
+ description: Takes simple English sentences and provides the Russian Reversal verb phrase
64
+ email:
65
+ executables: []
66
+
67
+ extensions: []
68
+
69
+ extra_rdoc_files: []
70
+
71
+ files:
72
+ - lib/russian-reversal.rb
73
+ - spec/preparation.rb
74
+ - spec/reverse.rb
75
+ has_rdoc: true
76
+ homepage: https://github.com/Pistos/RussianReversal
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options: []
81
+
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
101
+ version: "0"
102
+ requirements: []
103
+
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.7
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Takes simple English sentences and provides the Russian Reversal verb phrase
109
+ test_files:
110
+ - spec/preparation.rb
111
+ - spec/reverse.rb