anglicize 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +8 -4
- data/lib/anglicize.rb +0 -2
- data/lib/anglicize/string.rb +52 -7
- data/lib/anglicize/version.rb +3 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5041976b78645c948b78d7993e034cdd86c064c7
|
4
|
+
data.tar.gz: b77c9c2e65b7abef132867e0aca3e25b304a501a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5a04d592bfec061bfa7b098c3ba15bc3b2316caa88ddd6420ef24d1badf59ff0b4b7aef977c048226ba5a0233c4387399cdc51c22461577da66535ffe8b9682
|
7
|
+
data.tar.gz: b561de108a15d5536d92f7ef7a7fabf935c1c3d4e524f0e7a3c1f8ea450734362b22f5340b5b17d102dbb578079bbe02a293d5929326f44175a25df9e9e5299f
|
data/README.rdoc
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
= Anglicize
|
2
|
+
{<img src="https://badge.fury.io/rb/anglicize.svg" alt="Gem Version" />}[http://badge.fury.io/rb/anglicize]
|
2
3
|
|
3
4
|
Anglicize is a simple gem that converts strings between American and English spellings, based on the list of alternate spellings at [http://www.tysto.com/uk-us-spelling-list.html].
|
4
5
|
|
@@ -15,8 +16,11 @@ Or, install as a plugin:
|
|
15
16
|
|
16
17
|
== Getting Started
|
17
18
|
|
18
|
-
The gem adds
|
19
|
-
* anglicize -
|
19
|
+
The gem adds second methods to the String object:
|
20
|
+
* anglicize - return a copy of the string converted to English spelling
|
21
|
+
* anglicize! - convert the string in-place to English spelling
|
20
22
|
* has_alternate_english_spelling? - returns true if English spelling of string is different
|
21
|
-
* americanize -
|
22
|
-
*
|
23
|
+
* americanize - return a copy of the string converted to American spelling
|
24
|
+
* americanize! - convert the string in-place to American spelling
|
25
|
+
* has_alternate_american_spelling? - returns true if American spelling of string is different
|
26
|
+
* copy_case
|
data/lib/anglicize.rb
CHANGED
data/lib/anglicize/string.rb
CHANGED
@@ -2,51 +2,96 @@ require 'yaml'
|
|
2
2
|
|
3
3
|
# Extensions for string class.
|
4
4
|
class String
|
5
|
+
# @return [String] a copy of the string converted into English spelling.
|
5
6
|
def anglicize
|
6
|
-
|
7
|
+
result = self.dup
|
8
|
+
scan(WORD_RE).uniq.each do |word|
|
9
|
+
result.gsub!(/\b#{word}\b/, get_anglicized_word(word))
|
10
|
+
end
|
11
|
+
result
|
12
|
+
end
|
13
|
+
|
14
|
+
# Converts the string into English spelling.
|
15
|
+
# @return [String] the string converted into English spelling
|
16
|
+
def anglicize!
|
17
|
+
self.replace(self.anglicize)
|
7
18
|
end
|
8
19
|
|
20
|
+
# @return [String] a copy of the string converted into American spelling.
|
9
21
|
def americanize
|
10
|
-
|
22
|
+
result = self.dup
|
23
|
+
scan(WORD_RE).uniq.each do |word|
|
24
|
+
result.gsub!(/\b#{word}\b/, get_americanized_word(word))
|
25
|
+
end
|
26
|
+
result
|
11
27
|
end
|
12
28
|
|
29
|
+
# Converts the string into American spelling.
|
30
|
+
# @return [String] the string converted into American spelling
|
31
|
+
def americanize!
|
32
|
+
self.replace(self.americanize)
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [Boolean] a value indicating whether the English spelling differs.
|
13
36
|
def has_alternate_english_spelling?
|
14
37
|
self != anglicize
|
15
38
|
end
|
16
39
|
|
40
|
+
# @return [Boolean] a value indicating whether the American spelling differs.
|
17
41
|
def has_alternate_american_spelling?
|
18
42
|
self != americanize
|
19
43
|
end
|
20
44
|
|
45
|
+
# @param word [String] used as the case template
|
46
|
+
# @return [String] a copy of the string with the same case as the template.
|
21
47
|
def copy_case(word)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
48
|
+
result = ""
|
49
|
+
if word.upcase == word
|
50
|
+
result = self.upcase
|
51
|
+
elsif word.downcase == word
|
52
|
+
result = self.downcase
|
26
53
|
else
|
27
|
-
|
54
|
+
self.chars.each_with_index do |char, index|
|
55
|
+
word_char = word[index] || "a"
|
56
|
+
result << (word_char.upcase == word_char ? char.upcase : char.downcase)
|
57
|
+
end
|
28
58
|
end
|
59
|
+
result
|
60
|
+
end
|
61
|
+
|
62
|
+
# Changes the string to have the same case as the string parameter.
|
63
|
+
# @param word [String] used as the case template
|
64
|
+
# @return [String] the string with the same case as the template.
|
65
|
+
def copy_case!(word)
|
66
|
+
self.replace(self.copy_case)
|
29
67
|
end
|
30
68
|
|
31
69
|
private
|
32
70
|
|
71
|
+
# A regular expression for identifying words
|
72
|
+
WORD_RE = /\b[a-zA-Z]+\b/
|
73
|
+
|
33
74
|
def get_anglicized_word(word)
|
75
|
+
# Load american to english conversion hash if not already loaded
|
34
76
|
@@american_to_english ||= load_american_to_english
|
35
77
|
key = word.downcase
|
36
78
|
(@@american_to_english[key] || word).copy_case(word)
|
37
79
|
end
|
38
80
|
|
39
81
|
def get_americanized_word(word)
|
82
|
+
# Load english to american hash if not already loaded
|
40
83
|
@@english_to_american ||= load_english_to_american
|
41
84
|
key = word.downcase
|
42
85
|
(@@english_to_american[key] || word).copy_case(word)
|
43
86
|
end
|
44
87
|
|
88
|
+
# Load alternate spellings if not already loaded and insert values and keys
|
45
89
|
def load_american_to_english
|
46
90
|
@@alternate_spellings ||= load_alternate_spellings
|
47
91
|
@@alternate_spellings.invert
|
48
92
|
end
|
49
93
|
|
94
|
+
# Load alternate spellings if not already loaded
|
50
95
|
def load_english_to_american
|
51
96
|
@@alternate_spellings ||= load_alternate_spellings
|
52
97
|
end
|
data/lib/anglicize/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: anglicize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Webb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -65,3 +65,4 @@ signing_key:
|
|
65
65
|
specification_version: 4
|
66
66
|
summary: Converts strings from American spelling to English spelling and vice versa.
|
67
67
|
test_files: []
|
68
|
+
has_rdoc:
|