string_language 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b276927107e9da6ec0ef1de905944d8a947fafe4
4
- data.tar.gz: 3f55a399e1f40017315c2b4d86c2fc25dc68451e
3
+ metadata.gz: 49ed3266f212bed603081b98b6fa0112fd2cdec6
4
+ data.tar.gz: e955dfed0e10938a089254621dc6449d64094206
5
5
  SHA512:
6
- metadata.gz: ca3842738b186cb5b9440d3b863f63a3efbb0b58c58834baa77ec51c5d57dc404e34825fa589971fdd88bc023b32f72d8121e8177aaa6ed608297c19486d919b
7
- data.tar.gz: 7c05738fa63b786a385712d17c4839d8b143fc946670a99f785654525cd259be22f09a891a34f9466566c10c578cb293dea36b5ef1c6d968d42b46305c43cb0a
6
+ metadata.gz: d110dc727ee65999c5fd31b3931689915801e25b048ff0d7a1fa06b68e4545c8fb6747cb1a34b71e4944c9669a33a9d54e2dbba83dcf45db1d39f90a7a6cf922
7
+ data.tar.gz: dfb0de7569f2bcffd2cc039b8c52b7407d36b25806e911f9f342c7fae4f25216acd1dec7b508e695a309b4e57688b4450430c4ba5a219cd154de7a0fb534f12d
@@ -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
- # Google Translate does not always respect surrounding space and quotes. This gem does.
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
- =begin
103
- english = "this is english text"
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
- =end
83
+ end
@@ -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
- # "'a' ( bla ) (c )" => ["", "'", "a", "' ", "", "( ", "bla )", " (", "c )"]
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
- prefix = matchB.match( /^\s*/ )[0]
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
@@ -37,9 +37,7 @@ class String
37
37
 
38
38
  output_language = (output_language == nil || output_language == 'auto') ? 'nl' : output_language
39
39
 
40
- translation = (self == '') ? '' : @@translator.translate( self, input_language, output_language )
41
-
42
- StringHelpers::space_equally( translation, self )
40
+ (self == '') ? '' : @@translator.translate( self, input_language, output_language )
43
41
  end
44
42
  end
45
43
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_language
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cornelis Adriaan Schippers