konjak 0.0.21 → 0.0.22

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f16761c5f15cb70d150d182162126a265ec0fa1b
4
- data.tar.gz: 3441bf4468073d0612fbb175e8f757702d8d9edb
3
+ metadata.gz: c6d01369d26197b67672a4260930011ef66880bd
4
+ data.tar.gz: 9e2596af227e206039d32f3107fb3ee318adbb25
5
5
  SHA512:
6
- metadata.gz: 1e04e5a40af148be17c0d535e823eec36effa2edd02faae8165e6d3475f8f410b287e7a4fb5ba1b59fd287167ff5d3ec8f1ecfa89f8d1b643c33e51b472132f5
7
- data.tar.gz: 521d6ff59812122d838e191fcfa45815879500726d44ea217a96addf50491aa3c7e0f97b087113ad4716e7c55fe9d595e293c5c166c0d26c8fcbb36e63ee7b07
6
+ metadata.gz: 5248cd97806b0f7ad9b487838dc03ae65d631ec85d69dac8b8c1d5f84cc2265e8b9303ce9be543537939349a94dd13f465fe953cc6fc2ff2de640f26320c7ec6
7
+ data.tar.gz: 0e37939c78bfd23a9be0f86f89c06a0edfbf83140a0dd0d5d1c9debd4ef0beeb7f59c610c3b278115249bc852492122ea1887cf8ea91e3b5da00fad4300cab43
@@ -8,7 +8,15 @@ module Konjak
8
8
  class TmxSegmentor < Segmentor
9
9
  class Strategy
10
10
  Edge = Struct.new(:prev, :current)
11
- Node = Struct.new(:range, :segment)
11
+ Node = Struct.new(:range, :segments) do
12
+ def <=>(other)
13
+ [range.begin, -max_segment_size] <=> [other.range.begin, -other.max_segment_size]
14
+ end
15
+
16
+ def max_segment_size
17
+ segments.max_by {|s| s.text.size }.text.size
18
+ end
19
+ end
12
20
  Node::None = -1
13
21
 
14
22
  include Mem
@@ -25,19 +33,21 @@ module Konjak
25
33
  return [@text] if nodes.empty?
26
34
 
27
35
  segments = []
28
- prev_text_index = 0
36
+ prev_text_end = 0
37
+ prev_segment = nil
29
38
  max_cost_path.each do |node|
30
39
  range = node.range
31
- segment = node.segment
32
- prev_text = @text[prev_text_index...range.begin]
40
+ segment = select_next_segment(prev_segment, node)
41
+ prev_text = @text[prev_text_end...range.begin]
33
42
 
34
43
  segments << prev_text unless prev_text.empty?
35
44
 
36
45
  segments << SegmentString.new(@text[range.begin, range.size], segment)
37
46
 
38
- prev_text_index = range.end
47
+ prev_segment = segment
48
+ prev_text_end = range.end
39
49
  end
40
- after_text = @text[prev_text_index..-1]
50
+ after_text = @text[prev_text_end..-1]
41
51
  segments << after_text unless after_text.empty?
42
52
  segments
43
53
  end
@@ -48,10 +58,11 @@ module Konjak
48
58
  costs = nodes.map {|node| [node, node.range.size] }.to_h
49
59
 
50
60
  edges.each do |edge|
51
- node, node2 = edge.prev, edge.current
61
+ node, node2 = edge.prev, edge.current
62
+ node2_cost = costs[node2]
52
63
  new_node2_cost = costs[node] + calc_edge_cost(edge)
53
64
 
54
- if costs[node2] < new_node2_cost
65
+ if node2_cost < new_node2_cost
55
66
  costs[node2] = new_node2_cost
56
67
  prev_nodes[node2] = node
57
68
  end
@@ -59,7 +70,7 @@ module Konjak
59
70
 
60
71
  node, _ = costs.max_by {|_, cost| cost }
61
72
 
62
- max_cost_node_indexes = Enumerator.new {|y|
73
+ Enumerator.new {|y|
63
74
  loop do
64
75
  break if node == Node::None
65
76
  y << node
@@ -90,16 +101,18 @@ module Konjak
90
101
  def nodes
91
102
  return @nodes if @nodes
92
103
 
93
- @nodes = []
104
+ @nodes = {}
94
105
 
95
106
  translation_units.each {|tu|
96
107
  segment = tu.variant(@lang).segment
97
108
  @text.scan(compile_pattern(segment)) {
98
- @nodes << Node.new(($~.begin(0)...$~.end(0)), segment)
109
+ r = $~.begin(0)...$~.end(0)
110
+ @nodes[r] ||= []
111
+ @nodes[r] << segment
99
112
  }
100
113
  }
101
-
102
- @nodes.sort_by! {|node| [node.range.begin, -node.segment.text.size] }
114
+ @nodes = @nodes.map {|(r, segments)| Node.new(r, segments) }
115
+ @nodes.sort!
103
116
 
104
117
  @nodes
105
118
  end
@@ -107,7 +120,8 @@ module Konjak
107
120
  def default_options
108
121
  {
109
122
  translation_unit_filter: -> (tu) { true },
110
- calc_edge_cost: -> (edge) { edge.current.size }
123
+ calc_edge_cost: -> (edge) { edge.current.max_segment_size },
124
+ select_next_segment: -> (_, node) { node.segments.first }
111
125
  }
112
126
  end
113
127
 
@@ -119,6 +133,10 @@ module Konjak
119
133
  @options[:translation_unit_filter]
120
134
  end
121
135
 
136
+ def select_next_segment(prev_segment, node)
137
+ @options[:select_next_segment].call(prev_segment, node)
138
+ end
139
+
122
140
  def translation_units
123
141
  @translation_units ||= @tmx.body.translation_units.select(&translation_unit_filter)
124
142
  end
@@ -1,3 +1,3 @@
1
1
  module Konjak
2
- VERSION = "0.0.21"
2
+ VERSION = "0.0.22"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: konjak
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.21
4
+ version: 0.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seiei Higa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-16 00:00:00.000000000 Z
11
+ date: 2015-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport