ruby-jdict 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,81 @@
1
+ # The sense element will record the translational equivalent
2
+ # of the Japanese word, plus other related information. Where there
3
+ # are several distinctly different meanings of the word, multiple
4
+ # sense elements will be employed.
5
+ module JDict
6
+ class Sense
7
+ PART_OF_SPEECH_RE = /^\[\[([^\]]+)\]\]\s+/
8
+
9
+ attr_reader :parts_of_speech, :glosses, :language
10
+ #
11
+ # Create a new +Sense+
12
+ def initialize(parts_of_speech, glosses, language)
13
+ @parts_of_speech, @glosses, @language = parts_of_speech, glosses, language
14
+ end
15
+
16
+ def to_s
17
+ parts_of_speech_to_s(@parts_of_speech) + glosses_to_s(@glosses)
18
+ end
19
+
20
+ def to_sql
21
+ str = ""
22
+ str << serialize_parts_of_speech
23
+ str << serialize_glosses
24
+ str
25
+ end
26
+
27
+ def self.from_sql(txt)
28
+ parts_of_speech = deserialize_parts_of_speech(txt)
29
+ glosses, language = deserialize_glosses(txt)
30
+
31
+ Sense.new(parts_of_speech, glosses, language)
32
+ end
33
+
34
+ private
35
+
36
+ def serialize_parts_of_speech
37
+ "[[#{@parts_of_speech.join(SerialConstants::PART_OF_SPEECH_SENTINEL)}]] "
38
+ end
39
+
40
+ # FIXME: it fails when retrieving entries from an existing index, because only one language is retrieved and the 'lang' field is nil
41
+ def serialize_glosses
42
+ @lang.to_s + SerialConstants::LANGUAGE_SENTINEL + @glosses.join(SerialConstants::MEANING_SENTINEL)
43
+ end
44
+
45
+
46
+ def self.deserialize_parts_of_speech(txt)
47
+ ary = txt.scan(PART_OF_SPEECH_RE)
48
+ if ary.size == 1
49
+ ary[0][0].split(SerialConstants::PART_OF_SPEECH_SENTINEL)
50
+ else
51
+ []
52
+ end
53
+ end
54
+
55
+ def self.deserialize_glosses(txt)
56
+ ary = txt.scan(PART_OF_SPEECH_RE)
57
+ str = if ary.size == 1
58
+ txt[(ary.to_s.length-1)..-1]
59
+ else
60
+ txt[5..-1]
61
+ end
62
+
63
+ str = str.force_encoding("UTF-8");
64
+
65
+ language, gloss_strings = str.split(SerialConstants::LANGUAGE_SENTINEL)
66
+ language = language.to_sym
67
+ glosses = gloss_strings.split(SerialConstants::MEANING_SENTINEL)
68
+
69
+ [glosses, language]
70
+ end
71
+
72
+
73
+ def glosses_to_s(glosses)
74
+ glosses.join('; ')
75
+ end
76
+
77
+ def parts_of_speech_to_s(parts_of_speech)
78
+ parts_of_speech.nil? ? '' : '[' + parts_of_speech.join(',') + '] '
79
+ end
80
+ end
81
+ end
@@ -1,3 +1,3 @@
1
- module JDict
2
- Version = '0.0.7'
3
- end
1
+ module JDict
2
+ Version = '0.0.8'
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+
4
+ require File.dirname(__FILE__) + '/spec_helper'
5
+ require BASE_PATH + '/lib/ruby-jdict'
6
+
7
+ describe JDict::Convert do
8
+ it "converts halfwidth katakana to hiragana" do
9
+ str = "ハヒフヘホウカキクケコサシスセソタチツテトアイエオナニヌネノマミムメモヤユヨラリルレロワヲンァィゥェォャュョッ"
10
+ Convert.kata_to_hira(str).should equal("はひふへほうかきくけこさしすせそたちつてとあいえおなにぬねのまみむめもやゆよらりるれろわをんぁぃぅぇぉゃゅょっ")
11
+ end
12
+
13
+ it "converts halfwidth katakana with nigori mark to hiragana" do
14
+ str = "バビブベボヴガギグゲゴザジズゼゾダヂヅデド"
15
+ Convert.kata_to_hira(str).should equal("ばびぶべぼヴがぎぐげござじずぜぞだぢづでど")
16
+ end
17
+
18
+ it "converts halfwidth katakana with maru mark to hiragana" do
19
+ str = "パピプペポ"
20
+ Convert.kata_to_hira(str).should equal("ぱぴぷぺぽ")
21
+ end
22
+
23
+ it "converts invalid halfwidth katakana to hiragana" do
24
+ str = "ト゚ァ゙゚"
25
+ Convert.kata_to_hira(str).should equal("とぁ")
26
+ end
27
+ end
@@ -1,113 +1,113 @@
1
- require 'spec_helper'
2
- require BASE_PATH + '/lib/dictionary'
3
- #require BASE_PATH + '/lib/jmdict'
4
-
5
- module DictionarySpecHelper
6
- JMDICT_PATH = File.join(BASE_PATH+'/dictionaries/JMdict')
7
- INDEX_PATH = File.join(BASE_PATH+'/index')
8
-
9
- def mock_index
10
- end
11
-
12
- class Increase
13
- def initialize(&measure_proc) # + args
14
- @measure_proc = measure_proc
15
- end
16
-
17
- def matches?(target)
18
- @target = target
19
- @original_value = @measure_proc.call
20
- target.call
21
- @new_value = @measure_proc.call
22
- return @new_value.to_i > @original_value.to_i
23
- end
24
-
25
- def failure_message
26
- "expected #{@new_value} to be greater than #{@original_value}"
27
- end
28
-
29
- def negative_failure_message
30
- "expected #{@new_value} to not be greater than #{@original_value}"
31
- end
32
-
33
- def description
34
- "increase #{@original_value}"
35
- end
36
- end
37
-
38
- def increase(&measure_proc) # + args
39
- Increase.new(&measure_proc)
40
- end
41
- end
42
-
43
- module DictionarySpec
44
- include DictionarySpecHelper
45
-
46
- describe JDict::Dictionary do
47
- before do
48
- @dictionary = JDict::Dictionary.new(INDEX_PATH)
49
- end
50
-
51
- it "is searchable" do
52
- @dictionary.should respond_to(:search)
53
- end
54
-
55
- it "can tell you whether or not it's loaded" do
56
- @dictionary.should respond_to(:loaded?)
57
- end
58
-
59
- it "should generate fixtures" do
60
- pending
61
- @dictionary.should respond_to(:generate_fixtures)
62
- end
63
- end
64
-
65
- describe JDict::Dictionary, "after initialization" do
66
- before do
67
- @dictionary = JDict::Dictionary.new(INDEX_PATH)
68
- end
69
-
70
- it "has no entries" do
71
- @dictionary.size.should == 0
72
- end
73
- end
74
-
75
- describe JDict::Dictionary, "when loading from a dictionary file" do
76
- before do
77
- @dictionary = JDict::Dictionary.new(INDEX_PATH)
78
- end
79
-
80
- it "has at least 1 entry" do
81
- pending("implement loading from index")
82
- @dictionary.load(JMDICT_PATH)
83
- @dictionary.size.should > 0
84
- end
85
-
86
- it "says it's loaded" do
87
- pending("implement loading from index")
88
- @dictionary.load(JMDICT_PATH)
89
- # @dictionary.loaded?.should == true
90
- @dictionary.loaded?.should equal(true)
91
- end
92
- end
93
-
94
- describe JDict::Dictionary, "when loading from a dictionary file (already loaded)" do
95
- before do
96
- @dictionary = JDict::Dictionary.new(INDEX_PATH)
97
- end
98
-
99
- it "has the same size as it did before being loaded"
100
- end
101
-
102
- describe JDict::Dictionary, "when searching" do
103
- before do
104
- @dictionary = JDict::Dictionary.new(INDEX_PATH)
105
- end
106
-
107
- it "should raise an error if an index isn't built yet"
108
- it "should give no results if the search phrase is empty" do
109
- @dictionary.search('').should be_empty
110
- end
111
- end
112
-
113
- end
1
+ require 'spec_helper'
2
+ require BASE_PATH + '/lib/ruby-jdict'
3
+ #require BASE_PATH + '/lib/jmdict'
4
+
5
+ module DictionarySpecHelper
6
+ JMDICT_PATH = File.join(BASE_PATH+'/dictionaries/JMdict')
7
+ INDEX_PATH = File.join(BASE_PATH+'/index')
8
+
9
+ def mock_index
10
+ end
11
+
12
+ class Increase
13
+ def initialize(&measure_proc) # + args
14
+ @measure_proc = measure_proc
15
+ end
16
+
17
+ def matches?(target)
18
+ @target = target
19
+ @original_value = @measure_proc.call
20
+ target.call
21
+ @new_value = @measure_proc.call
22
+ return @new_value.to_i > @original_value.to_i
23
+ end
24
+
25
+ def failure_message
26
+ "expected #{@new_value} to be greater than #{@original_value}"
27
+ end
28
+
29
+ def negative_failure_message
30
+ "expected #{@new_value} to not be greater than #{@original_value}"
31
+ end
32
+
33
+ def description
34
+ "increase #{@original_value}"
35
+ end
36
+ end
37
+
38
+ def increase(&measure_proc) # + args
39
+ Increase.new(&measure_proc)
40
+ end
41
+ end
42
+
43
+ module DictionarySpec
44
+ include DictionarySpecHelper
45
+
46
+ describe JDict::Dictionary do
47
+ before do
48
+ @dictionary = JDict::Dictionary.new(INDEX_PATH)
49
+ end
50
+
51
+ it "is searchable" do
52
+ @dictionary.should respond_to(:search)
53
+ end
54
+
55
+ it "can tell you whether or not it's loaded" do
56
+ @dictionary.should respond_to(:loaded?)
57
+ end
58
+
59
+ it "should generate fixtures" do
60
+ pending
61
+ @dictionary.should respond_to(:generate_fixtures)
62
+ end
63
+ end
64
+
65
+ describe JDict::Dictionary, "after initialization" do
66
+ before do
67
+ @dictionary = JDict::Dictionary.new(INDEX_PATH)
68
+ end
69
+
70
+ it "has no entries" do
71
+ @dictionary.size.should == 0
72
+ end
73
+ end
74
+
75
+ describe JDict::Dictionary, "when loading from a dictionary file" do
76
+ before do
77
+ @dictionary = JDict::Dictionary.new(INDEX_PATH)
78
+ end
79
+
80
+ it "has at least 1 entry" do
81
+ pending("implement loading from index")
82
+ @dictionary.load(JMDICT_PATH)
83
+ @dictionary.size.should > 0
84
+ end
85
+
86
+ it "says it's loaded" do
87
+ pending("implement loading from index")
88
+ @dictionary.load(JMDICT_PATH)
89
+ # @dictionary.loaded?.should == true
90
+ @dictionary.loaded?.should equal(true)
91
+ end
92
+ end
93
+
94
+ describe JDict::Dictionary, "when loading from a dictionary file (already loaded)" do
95
+ before do
96
+ @dictionary = JDict::Dictionary.new(INDEX_PATH)
97
+ end
98
+
99
+ it "has the same size as it did before being loaded"
100
+ end
101
+
102
+ describe JDict::Dictionary, "when searching" do
103
+ before do
104
+ @dictionary = JDict::Dictionary.new(INDEX_PATH)
105
+ end
106
+
107
+ it "should raise an error if an index isn't built yet"
108
+ it "should give no results if the search phrase is empty" do
109
+ @dictionary.search('').should be_empty
110
+ end
111
+ end
112
+
113
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ require 'rubygems'
3
+
4
+ require File.dirname(__FILE__) + '/spec_helper'
5
+ require BASE_PATH + '/lib/ruby-jdict'
6
+
7
+ describe JDict::Entry do
8
+ it "is serializable" do
9
+ entry = JDict::Entry.new(1, ["感じ", "漢字"], ["かんじ", "カンジ"], [JDict::Sense.new(["&n;", "&vs;"], ["feeling", "kanji"])])
10
+ entry.to_sql.should equal("")
11
+ end
12
+
13
+ it "is deserializable" do
14
+ fail
15
+ end
16
+
17
+ it "is serializable with no parts of speech" do
18
+ entry = JDict::Entry.new(1, ["感じ", "漢字"], ["かんじ", "カンジ"], [JDict::Sense.new([], ["feeling", "kanji"])])
19
+ entry.to_sql.should equal("")
20
+ end
21
+
22
+ it "is deserializable with no parts of speech" do
23
+ fail
24
+ end
25
+ end
@@ -1,33 +1,33 @@
1
- <JMdict>
2
- <entry>
3
- <ent_seq>1171270</ent_seq>
4
- <k_ele>
5
- <keb>右翼</keb>
6
- <ke_pri>ichi1</ke_pri>
7
- <ke_pri>news1</ke_pri>
8
- <ke_pri>nf04</ke_pri>
9
- </k_ele>
10
- <r_ele>
11
- <reb>うよく</reb>
12
- <re_pri>ichi1</re_pri>
13
- <re_pri>news1</re_pri>
14
- <re_pri>nf04</re_pri>
15
- </r_ele>
16
- <sense>
17
- <pos>&n;</pos>
18
- <gloss>right-wing</gloss>
19
- <gloss g_lang="fr">aile droite (oiseau, armée, parti politique, base-ball)</gloss>
20
- <gloss g_lang="ru">пра́вое крыло́</gloss>
21
- <gloss g_lang="ru">пра́вый фланг</gloss>
22
- <gloss g_lang="de">rechter Flügel</gloss>
23
- </sense>
24
- <sense>
25
- <gloss g_lang="de">{Sport}</gloss>
26
- <gloss g_lang="de">rechte Flanke</gloss>
27
- <gloss g_lang="de">rechter Flügel</gloss>
28
- </sense>
29
- <sense>
30
- <gloss g_lang="de">die Rechte</gloss>
31
- </sense>
32
- </entry>
1
+ <JMdict>
2
+ <entry>
3
+ <ent_seq>1171270</ent_seq>
4
+ <k_ele>
5
+ <keb>右翼</keb>
6
+ <ke_pri>ichi1</ke_pri>
7
+ <ke_pri>news1</ke_pri>
8
+ <ke_pri>nf04</ke_pri>
9
+ </k_ele>
10
+ <r_ele>
11
+ <reb>うよく</reb>
12
+ <re_pri>ichi1</re_pri>
13
+ <re_pri>news1</re_pri>
14
+ <re_pri>nf04</re_pri>
15
+ </r_ele>
16
+ <sense>
17
+ <pos>&n;</pos>
18
+ <gloss>right-wing</gloss>
19
+ <gloss g_lang="fr">aile droite (oiseau, armée, parti politique, base-ball)</gloss>
20
+ <gloss g_lang="ru">пра́вое крыло́</gloss>
21
+ <gloss g_lang="ru">пра́вый фланг</gloss>
22
+ <gloss g_lang="de">rechter Flügel</gloss>
23
+ </sense>
24
+ <sense>
25
+ <gloss g_lang="de">{Sport}</gloss>
26
+ <gloss g_lang="de">rechte Flanke</gloss>
27
+ <gloss g_lang="de">rechter Flügel</gloss>
28
+ </sense>
29
+ <sense>
30
+ <gloss g_lang="de">die Rechte</gloss>
31
+ </sense>
32
+ </entry>
33
33
  </JMdict>
data/spec/index_spec.rb CHANGED
@@ -1,84 +1,82 @@
1
- require 'rubygems'
2
-
3
- require File.dirname(__FILE__) + '/spec_helper'
4
- require BASE_PATH + '/lib/dictionary'
5
- require BASE_PATH + '/lib/jmdict'
6
- require BASE_PATH + '/lib/index'
7
-
8
- require 'fileutils'
9
-
10
- module IndexSpecHelper
11
- end
12
-
13
- describe JDict::DictIndex do
14
- include IndexSpecHelper
15
-
16
- before do
17
- @index = JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH)
18
- end
19
-
20
- # Searching
21
- it "is searchable" do
22
- @index.should respond_to(:search)
23
- end
24
-
25
- # Building
26
- it "is buildable" do
27
- @index.should respond_to(:build) # and return an index
28
- end
29
- it "is rebuildable" do
30
- @index.should respond_to(:rebuild)
31
- end
32
- it "tells whether it's built or not" do
33
- @index.should respond_to(:built?)
34
- end
35
-
36
- # Destroying
37
- it "is destroyable" do
38
- @index.should respond_to(:destroy)
39
- end
40
-
41
- it "raises an error if an invalid dictionary path is specified" do
42
- lambda { JDict::DictIndex.new(INDEX_PATH, 'bad_dictionary_path') }.should raise_error
43
- end
44
- end
45
-
46
- describe JDict::DictIndex, "after initialization" do
47
- it "the path should be set" do
48
- @index = JDict::DictIndex.new(INDEX_PATH)
49
- @index.path.should_not be(nil)
50
- @index.path.should_not be('')
51
- end
52
- end
53
-
54
- describe JDict::DictIndex, "when building," do
55
- it "it is created on the file system" do
56
- @index = JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH)
57
- @index.build
58
- File.exists?(INDEX_PATH).should == true
59
- end
60
-
61
- it "its directory on the file system shouldn't be empty" do
62
- @index = JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH,
63
- false) #no lazy loading
64
- @index.build
65
- # .
66
- # ..
67
- # ^-------- an empty directory has only these 2 entries
68
- expect(Dir.entries(INDEX_PATH).size).to be >= 3
69
- end
70
-
71
- it "loads from a dictionary file"
72
- end
73
-
74
- describe JDict::DictIndex, "when rebuilding" do
75
- include FileUtils
76
-
77
- it "raises an error if it doesn't already exist" do
78
- rm_rf(INDEX_PATH)
79
- File.exists?(INDEX_PATH).should == false
80
- lambda {
81
- JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH).rebuild
82
- }.should raise_error
83
- end
84
- end
1
+ require 'rubygems'
2
+
3
+ require File.dirname(__FILE__) + '/spec_helper'
4
+ require BASE_PATH + '/lib/ruby-jdict'
5
+
6
+ require 'fileutils'
7
+
8
+ module IndexSpecHelper
9
+ end
10
+
11
+ describe JDict::DictIndex do
12
+ include IndexSpecHelper
13
+
14
+ before do
15
+ @index = JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH)
16
+ end
17
+
18
+ # Searching
19
+ it "is searchable" do
20
+ @index.should respond_to(:search)
21
+ end
22
+
23
+ # Building
24
+ it "is buildable" do
25
+ @index.should respond_to(:build) # and return an index
26
+ end
27
+ it "is rebuildable" do
28
+ @index.should respond_to(:rebuild)
29
+ end
30
+ it "tells whether it's built or not" do
31
+ @index.should respond_to(:built?)
32
+ end
33
+
34
+ # Destroying
35
+ it "is destroyable" do
36
+ @index.should respond_to(:destroy)
37
+ end
38
+
39
+ it "raises an error if an invalid dictionary path is specified" do
40
+ lambda { JDict::DictIndex.new(INDEX_PATH, 'bad_dictionary_path') }.should raise_error
41
+ end
42
+ end
43
+
44
+ describe JDict::DictIndex, "after initialization" do
45
+ it "the path should be set" do
46
+ @index = JDict::DictIndex.new(INDEX_PATH)
47
+ @index.path.should_not be(nil)
48
+ @index.path.should_not be('')
49
+ end
50
+ end
51
+
52
+ describe JDict::DictIndex, "when building," do
53
+ it "it is created on the file system" do
54
+ @index = JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH)
55
+ @index.build
56
+ File.exists?(INDEX_PATH).should == true
57
+ end
58
+
59
+ it "its directory on the file system shouldn't be empty" do
60
+ @index = JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH,
61
+ false) #no lazy loading
62
+ @index.build
63
+ # .
64
+ # ..
65
+ # ^-------- an empty directory has only these 2 entries
66
+ expect(Dir.entries(INDEX_PATH).size).to be >= 3
67
+ end
68
+
69
+ it "loads from a dictionary file"
70
+ end
71
+
72
+ describe JDict::DictIndex, "when rebuilding" do
73
+ include FileUtils
74
+
75
+ it "raises an error if it doesn't already exist" do
76
+ rm_rf(INDEX_PATH)
77
+ File.exists?(INDEX_PATH).should == false
78
+ lambda {
79
+ JDict::DictIndex.new(INDEX_PATH, JMDICT_PATH).rebuild
80
+ }.should raise_error
81
+ end
82
+ end