jedict 0.1.0 → 0.2.2

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jedict.rb +187 -134
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b2d3ccd5333f78b5caca6e7d3b73abee141e79a
4
- data.tar.gz: 6ace75ccb126b0e6e7b90476c635726f6e26d832
3
+ metadata.gz: 7fa2481254a6a05fb482d77d9bc88817649488d7
4
+ data.tar.gz: d35c0fdc8ce2b5aac5c92096abb4adb40fee2780
5
5
  SHA512:
6
- metadata.gz: d980a64a4143a5b849f5abc3871afc8151affa3d17ae1d482ef65b8a54b9624d62b95f9118a3d328908858d2adee8220bebdd15400e00e02985b2e72cb881628
7
- data.tar.gz: 24df4af66e5e5cbc80f3d4e12e0e11ce067248bfed82548313f9a5bd9978c33e626b18e9811ddfd79d695a738c09c2735abcdb92b6fb5429e05e3b47543f7356
6
+ metadata.gz: 094391c021ade59ddcdc82600980453d582049653072259f66c6704a30446285a7c0bc1a0380a71da44c0f1f167f163844246cd9e99b092e80b2155e687c2f00
7
+ data.tar.gz: fb2ef060835a71b8b9ecf956e52cdab7019751387dcd96015dcfde25c28788b536877348846f385ca20a775262db3a3537a93e9b141f5a09b0b9bedf14bd8826
@@ -19,140 +19,193 @@ require "nokogiri"
19
19
 
20
20
  module JEDICT
21
21
 
22
- def self.instanciate_from filename, eager_load
23
- raise "Invalid path #{filename}" unless FileTest.exist? filename
24
- DictionaryProxy.new(filename, eager_load)
25
- end
26
-
27
- def self.[] filename, eager_load = false
28
- instanciate_from filename, eager_load
29
- end
30
-
31
- def self.new eager_load = false
32
- instanciate_from File.join(__dir__, '../assets/jedict'), eager_load
33
- end
34
-
35
- class DictionaryProxy
36
-
37
- def method_missing sym, *args, &blck
38
- return @dic.send(sym, *args, &blck) if @dic && @dic.respond_to?(sym)
39
- super
40
- end
41
-
42
- def initialize filename, eager_load
43
- @filename = filename
44
- @dic = eager_load ? JEDICT::load_file(filename) : nil
45
- end
46
-
47
- def count
48
- @count ||= (@dic && @dic.length) || JEDICT::load_file(@filename, Parser::Count.new)
49
- end
50
-
51
- def each_entry &blck
52
- raise "Block expected" unless blck
53
- JEDICT::load_file @filename, Parser::Entry.new(blck)
54
- end
55
-
56
- end
57
-
58
- def self.load_file filename, parser = Parser::Full.new
59
- Nokogiri::XML::SAX::Parser.new(parser).parse(File.open(filename))
60
- parser.value
61
- end
62
-
63
- def self.format_node n, pre = ""
64
- if n.is_a? Array
65
- n.reduce("") do |s, e|
66
- s + pre + format_node(e, pre + " ")
67
- end
68
- elsif n.is_a? Hash
69
- n.to_a.reduce("") do |s, e|
70
- key, value = *e
71
- s + "\n" + pre + ((key == :value) ? "" : key.to_s + ": ") + format_node(value, pre + " ")
72
- end
73
- else
74
- n.to_s
75
- end
76
- end
77
-
78
- module NodePrinting
79
- def to_s
80
- JEDICT::format_node(self).strip
81
- end
82
- end
83
-
84
-
85
- module Parser
86
-
87
- class Entry < Nokogiri::XML::SAX::Document
88
- attr_accessor :callback, :parents, :position
89
-
90
- def initialize prc
91
- @callback = prc
92
- @parents = []
93
- @position = {}
94
- end
95
-
96
- def start_element(name, attrs)
97
- return if name == "JMdict"
98
- name = name.to_sym
99
- pos = position[name]
100
- if !pos
101
- self.position[name] = {}
102
- elsif pos.is_a? Array
103
- position[name] << {}
104
- else
105
- self.position[name] = [pos, {}]
106
- end
107
- self.parents << position
108
- self.position = position[name]
109
- self.position = position[-1] if position.is_a? Array
110
- self.position[:attrs] = attrs.flatten if attrs.flatten.length > 0
111
- end
112
-
113
- def characters(string)
114
- string.gsub!(/[\n]/, "")
115
- string.strip!
116
- position[:value] = string unless string == ""
117
- end
118
-
119
- def end_element(name)
120
- self.position = parents.pop
121
- if name == "entry"
122
- position[:entry].extend NodePrinting
123
- callback.call(position[:entry])
124
- self.position = {}
125
- self.parents = []
126
- end
127
- end
128
-
129
- def value
130
- nil
131
- end
132
-
133
- end
134
-
135
- class Full < Entry
136
- attr_accessor :value
137
- def initialize
138
- @value = []
139
- super(proc { |e| value << e })
140
- end
141
- end
142
-
143
- class Count < Nokogiri::XML::SAX::Document
144
- attr_accessor :value
145
-
146
- def initialize
147
- @value = 0
148
- end
149
-
150
- def start_element(name, attrs)
151
- @value += 1 if name == "entry"
152
- end
153
- end
154
-
155
- end
22
+ def self.instanciate_from filename, eager_load
23
+ raise "Invalid path #{filename}" unless FileTest.exist? filename
24
+ DictionaryProxy.new(filename, eager_load)
25
+ end
26
+
27
+ def self.[] filename, eager_load = false
28
+ instanciate_from filename, eager_load
29
+ end
30
+
31
+ def self.new file: File.join(__dir__, '../assets/jedict'), eager: false
32
+ instanciate_from file, eager
33
+ end
34
+
35
+ class DictionaryProxy
36
+
37
+ def method_missing sym, *args, &blck
38
+ return @dic.send(sym, *args, &blck) if @dic && @dic.respond_to?(sym)
39
+ super
40
+ end
41
+
42
+ def initialize filename, eager_load
43
+ @filename = filename
44
+ @dic = eager_load ? JEDICT::load_file(filename) : nil
45
+ end
46
+
47
+ def count
48
+ @count ||= (@dic && @dic.length) || JEDICT::load_file(@filename, Parser::Count.new)
49
+ end
50
+
51
+ def each_entry &blck
52
+ raise "Block expected" unless blck
53
+ JEDICT::load_file @filename, Parser::Entry.new(blck)
54
+ end
55
+
56
+ end
57
+
58
+ def self.load_file filename, parser = Parser::Full.new
59
+ Nokogiri::XML::SAX::Parser.new(parser).parse(File.open(filename))
60
+ parser.value
61
+ end
62
+
63
+ def self.format_node n, pre = ""
64
+ if n.is_a? Array
65
+ n.reduce("") do |s, e|
66
+ s + pre + format_node(e, pre + " ")
67
+ end
68
+ elsif n.is_a? Hash
69
+ n.to_a.reduce("") do |s, e|
70
+ key, value = *e
71
+ s + "\n" + pre + ((key == :value) ? "" : key.to_s + ": ") + format_node(value, pre + " ")
72
+ end
73
+ else
74
+ n.to_s
75
+ end
76
+ end
77
+
78
+ module NodeExtention
79
+ PATHS = {
80
+ meaning: [:sense, :gloss, :value],
81
+ kanji_representation: [:k_ele, :keb, :value],
82
+ reading_representation: [:r_ele, :reb, :value],
83
+ kanji_priority: [:k_ele, :ke_pri, :value],
84
+ reading_priority: [:r_ele, :re_pri, :value],
85
+ kanji_information: [:k_ele, :ke_inf, :value],
86
+ reading_information: [:r_ele, :re_inf, :value],
87
+ sense_information: [:sense, :s_inf, :value],
88
+ gramatical_position: [:sense, :pos, :value],
89
+ lexical_field: [:sense, :field, :value],
90
+ dialect: [:sense, :dial, :value],
91
+ # primary_meaning: [:sense, :gloss, :pri, :value], # In the documentation but doesn't actually appear in the dictionnary at the time of writing (2017/12/17)
92
+ }
93
+ def to_s
94
+ JEDICT::format_node(self).strip
95
+ end
96
+
97
+ def self.elements_at node, path, *args
98
+ if args.length == 0
99
+ if node.is_a? Hash
100
+ [node[path]].flatten.compact
101
+ elsif node.is_a? Array
102
+ node.map { |e| e[path] }.flatten.compact
103
+ else
104
+ [node]
105
+ end
106
+ else
107
+ if node.is_a? Array
108
+ node.map { |e| elements_at e[path], *args }.flatten.compact
109
+ elsif node.is_a? Hash
110
+ elements_at(node[path], *args).flatten.compact
111
+ else
112
+ []
113
+ end
114
+ end
115
+ end
116
+
117
+ PATHS.each do |key, path|
118
+ define_method key do
119
+ NodeExtention::elements_at self, *path
120
+ end
121
+
122
+ define_method "#{key}?" do |param|
123
+ send(key).include?(param)
124
+ end
125
+
126
+ define_method "#{key}_is_one_of?" do |array|
127
+ (send(key) - array).length > 0
128
+ end
129
+
130
+ define_method "#{key}_matches?" do |regex|
131
+ send(key).reduce(false) { |acc, r| acc or r.match(regex) }
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+
138
+ module Parser
139
+
140
+ class Entry < Nokogiri::XML::SAX::Document
141
+ attr_accessor :callback, :parents, :position
142
+
143
+ def initialize prc
144
+ @callback = prc
145
+ @parents = []
146
+ @position = {}
147
+ end
148
+
149
+ def start_element(name, attrs)
150
+ return if name == "JMdict"
151
+ name = name.to_sym
152
+ pos = position[name]
153
+ if !pos
154
+ self.position[name] = {}
155
+ elsif pos.is_a? Array
156
+ position[name] << {}
157
+ else
158
+ self.position[name] = [pos, {}]
159
+ end
160
+ self.parents << position
161
+ self.position = position[name]
162
+ self.position = position[-1] if position.is_a? Array
163
+ self.position[:attrs] = attrs.flatten if attrs.flatten.length > 0
164
+ end
165
+
166
+ def characters(string)
167
+ string.gsub!(/[\n]/, "")
168
+ string.strip!
169
+ position[:value] = string unless string == ""
170
+ end
171
+
172
+ def end_element(name)
173
+ self.position = parents.pop
174
+ if name == "entry"
175
+ position[:entry].extend NodeExtention
176
+ callback.call(position[:entry])
177
+ self.position = {}
178
+ self.parents = []
179
+ end
180
+ end
181
+
182
+ def value
183
+ nil
184
+ end
185
+
186
+ end
187
+
188
+ class Full < Entry
189
+ attr_accessor :value
190
+ def initialize
191
+ @value = []
192
+ super(proc { |e| value << e })
193
+ end
194
+ end
195
+
196
+ class Count < Nokogiri::XML::SAX::Document
197
+ attr_accessor :value
198
+
199
+ def initialize
200
+ @value = 0
201
+ end
202
+
203
+ def start_element(name, attrs)
204
+ @value += 1 if name == "entry"
205
+ end
206
+ end
207
+
208
+ end
156
209
 
157
210
 
158
211
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jedict
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sylvain Leclercq
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.6.8
19
+ version: '1.8'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.6.8
26
+ version: '1.8'
27
27
  description: Parse the JEDICT and run search operations on the fly or load the entire
28
28
  dictionary in memory
29
29
  email: maisbiensurqueoui@gmail.com
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  version: '0'
54
54
  requirements: []
55
55
  rubyforge_project:
56
- rubygems_version: 2.4.5
56
+ rubygems_version: 2.4.8
57
57
  signing_key:
58
58
  specification_version: 4
59
59
  summary: JEDICT custom parser