language_list 0.1.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +11 -0
- data/data/dump +0 -0
- data/lib/language_list.rb +30 -15
- data/lib/language_list/version.rb +1 -1
- data/test/benchmark.rb +9 -0
- data/test/benchmark_lookup.rb +20 -0
- data/test/language_list_test.rb +9 -9
- metadata +14 -2
data/.travis.yml
ADDED
data/data/dump
ADDED
Binary file
|
data/lib/language_list.rb
CHANGED
@@ -3,7 +3,7 @@ require 'yaml'
|
|
3
3
|
module LanguageList
|
4
4
|
class LanguageInfo
|
5
5
|
attr_reader :name, :iso_639_3, :iso_639_1, :type
|
6
|
-
|
6
|
+
|
7
7
|
def initialize(options)
|
8
8
|
@name = options[:name]
|
9
9
|
@iso_639_3 = options[:iso_639_3]
|
@@ -11,49 +11,64 @@ module LanguageList
|
|
11
11
|
@common = options[:common]
|
12
12
|
@type = options[:type]
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def common?
|
16
16
|
@common
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def <=>(other)
|
20
20
|
self.name <=> other.name
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def iso_639_1?
|
24
24
|
!@iso_639_1.nil?
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
[:ancient, :constructed, :extinct, :historical, :living, :special].each do |type|
|
28
28
|
define_method("#{type.to_s}?") do
|
29
29
|
@type == type
|
30
30
|
end
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def to_s
|
34
34
|
"#{@iso_639_3}#{" (#{@iso_639_1})" if @iso_639_1} - #{@name}"
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
def self.find_by_iso_639_1(code)
|
38
|
-
LanguageList::
|
38
|
+
LanguageList::BY_ISO_639_1[code]
|
39
39
|
end
|
40
|
-
|
40
|
+
|
41
41
|
def self.find_by_iso_639_3(code)
|
42
|
-
LanguageList::
|
42
|
+
LanguageList::BY_ISO_639_3[code]
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
def self.find_by_name(name)
|
46
|
-
LanguageList::
|
46
|
+
LanguageList::BY_NAME[name]
|
47
47
|
end
|
48
48
|
|
49
49
|
def self.find(code)
|
50
50
|
find_by_iso_639_1(code) || find_by_iso_639_3(code) || find_by_name(code)
|
51
51
|
end
|
52
52
|
end
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
|
54
|
+
ALL_LANGUAGES = begin
|
55
|
+
Marshal.load(File.read(File.expand_path('../../data/dump', __FILE__)))
|
56
|
+
rescue => e
|
57
|
+
warn "Reverting to hash load: #{e.message}"
|
58
|
+
yaml_data = YAML.load_file(File.expand_path(File.join(File.dirname(__FILE__),'..', 'data', 'languages.yml')))
|
59
|
+
yaml_data.map{|e| LanguageInfo.new(e) }
|
60
|
+
end
|
56
61
|
ISO_639_1 = ALL_LANGUAGES.select(&:iso_639_1?)
|
57
62
|
LIVING_LANGUAGES = ALL_LANGUAGES.select(&:living?)
|
58
63
|
COMMON_LANGUAGES = ALL_LANGUAGES.select(&:common?)
|
64
|
+
|
65
|
+
BY_NAME = {}
|
66
|
+
BY_ISO_639_1 = {}
|
67
|
+
BY_ISO_639_3 = {}
|
68
|
+
ALL_LANGUAGES.each do |lang|
|
69
|
+
BY_NAME[lang.name] = lang
|
70
|
+
BY_ISO_639_1[lang.iso_639_1] = lang if lang.iso_639_1
|
71
|
+
BY_ISO_639_3[lang.iso_639_3] = lang if lang.iso_639_3
|
72
|
+
end
|
73
|
+
|
59
74
|
end
|
data/test/benchmark.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "benchmark"
|
4
|
+
$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
|
5
|
+
require 'language_list'
|
6
|
+
|
7
|
+
|
8
|
+
count = LanguageList::ALL_LANGUAGES.count
|
9
|
+
|
10
|
+
time = Benchmark.measure do
|
11
|
+
|
12
|
+
|
13
|
+
1000.times do
|
14
|
+
target = LanguageList::ALL_LANGUAGES[rand(count)]
|
15
|
+
|
16
|
+
LanguageList::LanguageInfo.find_by_name(target.name)
|
17
|
+
LanguageList::LanguageInfo.find(target.iso_639_3)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
puts time
|
data/test/language_list_test.rb
CHANGED
@@ -8,33 +8,33 @@ class LanguageListTest < Test::Unit::TestCase
|
|
8
8
|
def test_all_languages
|
9
9
|
assert_equal 7707, LanguageList::ALL_LANGUAGES.length
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
def test_common_languages
|
13
13
|
assert_equal 71, LanguageList::COMMON_LANGUAGES.length
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def test_living_languages
|
17
17
|
assert_equal 6986, LanguageList::LIVING_LANGUAGES.length
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def test_iso_iso_639_1_languages
|
21
21
|
assert_equal 184, LanguageList::ISO_639_1.length
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def test_find_by_iso_639_1
|
25
25
|
english = LanguageList::LanguageInfo.find_by_iso_639_1('en')
|
26
26
|
assert_equal 'en', english.iso_639_1
|
27
27
|
assert_equal 'eng', english.iso_639_3
|
28
28
|
assert_equal 'English', english.name
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def test_find_by_iso_639_3
|
32
32
|
english = LanguageList::LanguageInfo.find_by_iso_639_3('eng')
|
33
33
|
assert_equal 'en', english.iso_639_1
|
34
34
|
assert_equal 'eng', english.iso_639_3
|
35
35
|
assert_equal 'English', english.name
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def test_find_by_name
|
39
39
|
english = LanguageList::LanguageInfo.find_by_name('English')
|
40
40
|
assert_equal 'en', english.iso_639_1
|
@@ -48,14 +48,14 @@ class LanguageListTest < Test::Unit::TestCase
|
|
48
48
|
assert_equal 'eng', english.iso_639_3
|
49
49
|
assert_equal 'English', english.name
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
def test_find_with_iso_639_3_code
|
53
53
|
english = LanguageList::LanguageInfo.find('eng')
|
54
54
|
assert_equal 'en', english.iso_639_1
|
55
55
|
assert_equal 'eng', english.iso_639_3
|
56
56
|
assert_equal 'English', english.name
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
def test_find_with_name
|
60
60
|
english = LanguageList::LanguageInfo.find('English')
|
61
61
|
assert_equal 'en', english.iso_639_1
|
@@ -66,7 +66,7 @@ class LanguageListTest < Test::Unit::TestCase
|
|
66
66
|
def test_sort_should_order_by_name
|
67
67
|
sorted = LanguageList::COMMON_LANGUAGES.sort
|
68
68
|
expected = LanguageList::COMMON_LANGUAGES.map(&:name).sort
|
69
|
-
|
69
|
+
|
70
70
|
assert_equal expected, sorted.map(&:name)
|
71
71
|
end
|
72
72
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: language_list
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-08 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A list of languages based upon ISO-639-1 and ISO-639-3 with functions
|
15
15
|
to retrieve only common languages.
|
@@ -20,13 +20,17 @@ extensions: []
|
|
20
20
|
extra_rdoc_files: []
|
21
21
|
files:
|
22
22
|
- .gitignore
|
23
|
+
- .travis.yml
|
23
24
|
- Gemfile
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
27
|
+
- data/dump
|
26
28
|
- data/languages.yml
|
27
29
|
- language_list.gemspec
|
28
30
|
- lib/language_list.rb
|
29
31
|
- lib/language_list/version.rb
|
32
|
+
- test/benchmark.rb
|
33
|
+
- test/benchmark_lookup.rb
|
30
34
|
- test/language_list_test.rb
|
31
35
|
homepage: https://github.com/scsmith/language_list
|
32
36
|
licenses: []
|
@@ -40,12 +44,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
44
|
- - ! '>='
|
41
45
|
- !ruby/object:Gem::Version
|
42
46
|
version: '0'
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
hash: 4409535527429711707
|
43
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
51
|
none: false
|
45
52
|
requirements:
|
46
53
|
- - ! '>='
|
47
54
|
- !ruby/object:Gem::Version
|
48
55
|
version: '0'
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
hash: 4409535527429711707
|
49
59
|
requirements: []
|
50
60
|
rubyforge_project: language_list
|
51
61
|
rubygems_version: 1.8.23
|
@@ -53,4 +63,6 @@ signing_key:
|
|
53
63
|
specification_version: 3
|
54
64
|
summary: A list of languages and methods to find and work with these languages.
|
55
65
|
test_files:
|
66
|
+
- test/benchmark.rb
|
67
|
+
- test/benchmark_lookup.rb
|
56
68
|
- test/language_list_test.rb
|