Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/jldrill/model/DeinflectionRules.rb | 215 | 169 | 86.98%
|
83.43%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require 'jldrill/model/DataFile' |
2 require 'Context/Log' |
3 |
4 module JLDrill |
5 module Deinflection |
6 |
7 # Represents a deinflection reason. |
8 # In other words when you deinflect the verb using a rule, what |
9 # rule reason was it. For example "past negative" |
10 class Reason < String |
11 def Reason::isReason?(string) |
12 # All of the rules in the deinflection file are tab separated. |
13 # Therefore if a line doesn't contain a tab then it is a reason |
14 !string.include?("\t") |
15 end |
16 |
17 def Reason::parse(string) |
18 if Reason::isReason?(string) |
19 return Reason.new(string.chomp) |
20 else |
21 return nil |
22 end |
23 end |
24 end |
25 |
26 # Represents a deinflection rule. It contains the string that |
27 # will be found in the original text, the text that will be used |
28 # to replace the original text, and an index into the reason |
29 # array. |
30 class Rule |
31 |
32 attr_reader :original, :replaceWith, :reason |
33 |
34 RULE_RE = /^([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)/ |
35 |
36 def initialize(original, replaceWith, reason) |
37 @original = original |
38 @replaceWith = replaceWith |
39 @reason = reason |
40 end |
41 |
42 def Rule::parse(string, reasons) |
43 retVal = nil |
44 if RULE_RE.match(string) |
45 retVal = Rule.new($1, $2, reasons[$4.to_i]) |
46 end |
47 return retVal |
48 end |
49 |
50 def to_s |
51 @original + "\t" + @replaceWith + "\t" + reason |
52 end |
53 end |
54 |
55 class Transform |
56 |
57 attr_reader :original, :root, :rule |
58 |
59 def initialize(orig, root, rule) |
60 @orignal = orig |
61 @root = root |
62 @rule = rule |
63 end |
64 |
65 def Transform.start(string) |
66 Transform.new(string, string, nil) |
67 end |
68 |
69 def dictionary |
70 retVal = @root |
71 if !@rule.nil? |
72 retVal = @root + @rule.replaceWith |
73 end |
74 return retVal |
75 end |
76 |
77 def to_s |
78 @orignal + " " + @root + " " + @rule.to_s |
79 end |
80 end |
81 |
82 class Match < Array |
83 |
84 def initialize(transform, history=nil) |
85 if !history.nil? |
86 super(history) |
87 else |
88 super() |
89 end |
90 push(transform) |
91 end |
92 |
93 def Match.start(string) |
94 Match.new(Transform.start(string)) |
95 end |
96 |
97 def hasReason(rule) |
98 any? do |transform| |
99 if !transform.nil? && !transform.rule.nil? |
100 transform.rule.reason.eql?(rule.reason) |
101 else |
102 false |
103 end |
104 end |
105 end |
106 |
107 def apply(rule) |
108 retVal = nil |
109 if !last.nil? && !hasReason(rule) |
110 re = Regexp.new("(.*)#{rule.original}") |
111 if re.match(last.dictionary) |
112 transform = Transform.new(last.dictionary, $1, rule) |
113 retVal = Match.new(transform, self) |
114 end |
115 end |
116 return retVal |
117 end |
118 |
119 def transforms |
120 collect do |transform| |
121 if !transform.rule.nil? |
122 "(#{transform.root}#{transform.rule.original}) #{transform.rule.reason}: #{transform.dictionary}" |
123 else |
124 transform.dictionary |
125 end |
126 end.join(" > ") |
127 end |
128 |
129 def to_s |
130 transforms |
131 end |
132 end |
133 end |
134 |
135 # An array of Deinflection Rules. |
136 class DeinflectionRules |
137 |
138 attr_reader :reasons, :rules |
139 |
140 def initialize |
141 @reasons = [] |
142 @rules = [] |
143 @readHeader = false |
144 end |
145 |
146 def parse(string) |
147 if !@readHeader |
148 # The first line of the file must be discarded |
149 @readHeader = true |
150 else |
151 entry = Deinflection::Rule.parse(string, @reasons) |
152 if(!entry.nil?) |
153 @rules.push(entry) |
154 else |
155 reason = Deinflection::Reason.parse(string) |
156 if !reason.nil? |
157 @reasons.push(reason) |
158 else |
159 Context::Log::warning("JLDrill::DeinflectionRules", |
160 "Could not parse #{string}") |
161 end |
162 end |
163 end |
164 end |
165 |
166 def size |
167 return @rules.size + @reasons.size |
168 end |
169 |
170 def match(string) |
171 retVal = [Deinflection::Match.start(string)] |
172 i = 0 |
173 while(i < retVal.size) do |
174 @rules.each do |rule| |
175 new = retVal[i].apply(rule) |
176 if !new.nil? && !retVal.any? do |match| |
177 match.last.dictionary.eql?(new.last.dictionary) |
178 end |
179 retVal.push(new) |
180 end |
181 end |
182 i += 1 |
183 end |
184 return retVal |
185 end |
186 |
187 def to_s |
188 self.join("\n") |
189 end |
190 end |
191 |
192 class DeinflectionRulesFile < DataFile |
193 attr_reader :deinflectionRules |
194 attr_writer :deinflectionRules |
195 |
196 def initialize |
197 super |
198 @deinflectionRules = DeinflectionRules.new |
199 @stepSize = 20 |
200 end |
201 |
202 def dataSize |
203 @deinflectionRules.size |
204 end |
205 |
206 def parser |
207 @deinflectionRules |
208 end |
209 |
210 def match(string) |
211 @deinflectionRules.match(string) |
212 end |
213 end |
214 |
215 end |
Generated on Mon May 23 16:17:46 +0900 2011 with rcov 0.9.8