string_language 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/google_translator.rb +7 -31
- data/lib/string_helpers.rb +26 -12
- data/lib/string_language.rb +1 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 49ed3266f212bed603081b98b6fa0112fd2cdec6
|
4
|
+
data.tar.gz: e955dfed0e10938a089254621dc6449d64094206
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d110dc727ee65999c5fd31b3931689915801e25b048ff0d7a1fa06b68e4545c8fb6747cb1a34b71e4944c9669a33a9d54e2dbba83dcf45db1d39f90a7a6cf922
|
7
|
+
data.tar.gz: dfb0de7569f2bcffd2cc039b8c52b7407d36b25806e911f9f342c7fae4f25216acd1dec7b508e695a309b4e57688b4450430c4ba5a219cd154de7a0fb534f12d
|
data/lib/google_translator.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'curb'
|
4
4
|
require 'open-uri'
|
5
|
+
require_relative 'string_helpers'
|
5
6
|
|
6
7
|
class GoogleTranslator
|
7
8
|
|
@@ -23,7 +24,7 @@ class GoogleTranslator
|
|
23
24
|
ask_google
|
24
25
|
|
25
26
|
# return the untranslated input when it is translated to white space only
|
26
|
-
self.output = translation.match( /^\s*$/ ) ? input : translation
|
27
|
+
self.output = translation.match( /^\s*$/ ) ? input : StringHelpers::space_equally( translation, input )
|
27
28
|
end
|
28
29
|
|
29
30
|
def language input
|
@@ -39,32 +40,7 @@ class GoogleTranslator
|
|
39
40
|
|
40
41
|
def translation
|
41
42
|
|
42
|
-
|
43
|
-
prefix + translated_middle + suffix
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def prefix
|
49
|
-
|
50
|
-
input.match( /^(\s*['"]\s*|\s*)/ )[0]
|
51
|
-
end
|
52
|
-
|
53
|
-
def suffix
|
54
|
-
|
55
|
-
input.match( /(\s*['"]\s*|\s*)$/ )[0]
|
56
|
-
end
|
57
|
-
|
58
|
-
def middle
|
59
|
-
|
60
|
-
start = prefix.length
|
61
|
-
finish = input.length - suffix.length
|
62
|
-
input[start,finish]
|
63
|
-
end
|
64
|
-
|
65
|
-
def translated_middle
|
66
|
-
|
67
|
-
google_array[0][0][0].gsub( /^\s*/, '' ).gsub( /\s*$/, '' )
|
43
|
+
google_array[0][0][0]
|
68
44
|
end
|
69
45
|
|
70
46
|
def ask_google
|
@@ -99,9 +75,9 @@ class GoogleTranslator
|
|
99
75
|
end
|
100
76
|
|
101
77
|
# Quick Testing
|
102
|
-
|
103
|
-
english = "
|
78
|
+
begin
|
79
|
+
english = "'none' and 'ad' Articles"
|
104
80
|
translator = GoogleTranslator.new
|
105
|
-
translation = translator.translate english
|
81
|
+
translation = translator.translate english, 'en', 'nl'
|
106
82
|
puts translation
|
107
|
-
|
83
|
+
end
|
data/lib/string_helpers.rb
CHANGED
@@ -4,12 +4,32 @@ module StringHelpers
|
|
4
4
|
# the i-th bracket/quote in stringB
|
5
5
|
def self.space_equally stringA, stringB
|
6
6
|
|
7
|
-
|
7
|
+
intermediateA = space_equally_quotes_and_brackets( stringA, stringB )
|
8
|
+
|
9
|
+
space_equally_surrounding( intermediateA, stringB )
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
# Return second string, with outer surrounding spacing like first string
|
15
|
+
# @example
|
16
|
+
# space_equally_surrounding( "gnu ", " gnat " ) => " gnu "
|
17
|
+
#
|
18
|
+
def self.space_equally_surrounding stringA, stringB
|
19
|
+
|
20
|
+
prefix = stringB.match( /^\s*/ )[0]
|
21
|
+
suffix = stringB.match( /\s*$/ )[0]
|
22
|
+
middle = stringA.gsub( /^\s*|\s*$/, '' )
|
23
|
+
|
24
|
+
prefix + middle + suffix
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.space_equally_quotes_and_brackets stringA, stringB
|
8
28
|
|
9
29
|
brackets_and_quotes = "{(\['\"\])}"
|
10
30
|
regex = /(\s*[#{brackets_and_quotes}]\s*)/
|
11
31
|
|
12
|
-
splitA = stringA.split( regex )
|
32
|
+
splitA = stringA.split( regex ) # "'a' ( bla ) (c )" => ["", "'", "a", "' ", "", "( ", "bla )", " (", "c )"]
|
13
33
|
splitB = stringB.split( regex )
|
14
34
|
|
15
35
|
i=0; while space_ith_match( splitA, splitB, regex, i ) do ; i+=1 ; end
|
@@ -17,8 +37,6 @@ module StringHelpers
|
|
17
37
|
splitA.join('')
|
18
38
|
end
|
19
39
|
|
20
|
-
private
|
21
|
-
|
22
40
|
def self.space_ith_match listA, listB, regex, i
|
23
41
|
|
24
42
|
matchA, indexA = *ith_match( listA, regex, i )
|
@@ -26,11 +44,7 @@ module StringHelpers
|
|
26
44
|
|
27
45
|
return false if (matchA == nil or matchB == nil)
|
28
46
|
|
29
|
-
|
30
|
-
suffix = matchB.match( /\s*$/ )[0]
|
31
|
-
middle = matchA.gsub( /^\s*|\s*$/, '' )
|
32
|
-
|
33
|
-
listA[ indexA ] = prefix + middle + suffix
|
47
|
+
listA[ indexA ] = space_equally_surrounding( listA[ indexA ], listB[ indexB ] )
|
34
48
|
end
|
35
49
|
|
36
50
|
def self.ith_match list, regex, i
|
@@ -52,7 +66,7 @@ end
|
|
52
66
|
a = "(Dit is een (test) 'van' de spacing)"
|
53
67
|
b = "( Deze was niet goed gespaced, zoals ( test) ' van ' dittum)"
|
54
68
|
|
55
|
-
puts a
|
56
|
-
puts b
|
57
|
-
puts StringHelpers::space_equally( b, a )
|
69
|
+
puts "correct: >#{a}<"
|
70
|
+
puts "wrong: >#{b}<"
|
71
|
+
puts "test: >#{StringHelpers::space_equally( b, a )}<"
|
58
72
|
=end
|
data/lib/string_language.rb
CHANGED
@@ -37,9 +37,7 @@ class String
|
|
37
37
|
|
38
38
|
output_language = (output_language == nil || output_language == 'auto') ? 'nl' : output_language
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
StringHelpers::space_equally( translation, self )
|
40
|
+
(self == '') ? '' : @@translator.translate( self, input_language, output_language )
|
43
41
|
end
|
44
42
|
end
|
45
43
|
|