| Class | EstraierPure::Document |
| In: |
lib/vendor/estraierpure.rb
|
| Parent: | Object |
Abstraction of document.
Create a document object. `draft’ specifies a string of draft data.
# File lib/vendor/estraierpure.rb, line 159
159: def initialize(draft = "")
160: Utility::check_types({ draft=>String }) if $DEBUG
161: @id = -1
162: @attrs = {}
163: @dtexts = []
164: @htexts = []
165: @kwords = nil
166: if draft.length
167: lines = draft.split(/\n/)
168: num = 0
169: while num < lines.length
170: line = lines[num]
171: num += 1
172: break if line.length < 1
173: if line =~ /^%/
174: if line =~ /^%VECTOR\t/
175: @kwords = {} unless @kwords
176: fields = line.split(/\t/)
177: i = 1
178: while i < fields.length - 1
179: @kwords[fields[i]] = fields[i+1]
180: i += 2
181: end
182: end
183: next
184: end
185: line = line.gsub(/[ \t\r\n\v\f]+/, " ")
186: line = line.strip.squeeze(" ")
187: if idx = line.index("=")
188: key = line[0...idx]
189: value = line[idx+1...line.length]
190: @attrs[key] = value
191: end
192: end
193: while num < lines.length
194: line = lines[num]
195: next unless line.length
196: if line[0] == 0x9
197: @htexts.push(line[1...line.length]) if line.length > 1
198: else
199: @dtexts.push(line)
200: end
201: num += 1
202: end
203: end
204: end
Add an attribute. `name’ specifies the name of an attribute. `value’ specifies the value of the attribute. If it is `nil’, the attribute is removed. The return value is always `nil’.
# File lib/vendor/estraierpure.rb, line 53
53: def add_attr(name, value)
54: Utility::check_types({ name=>String, value=>String }) if $DEBUG
55: name = name.gsub(/[ \t\r\n\v\f]+/, " ")
56: name = name.strip.squeeze(" ")
57: value = value.gsub(/[ \t\r\n\v\f]+/, " ")
58: value = value.strip.squeeze(" ")
59: @attrs[name] = value
60: nil
61: end
Add a hidden sentence. `text’ specifies a hidden sentence. The return value is always `nil’.
# File lib/vendor/estraierpure.rb, line 75
75: def add_hidden_text(text)
76: Utility::check_types({ text=>String }) if $DEBUG
77: text = text.gsub(/[ \t\r\n\v\f]+/, " ")
78: text = text.strip.squeeze(" ")
79: @htexts.push(text) if text.length
80: nil
81: end
Add a sentence of text. `text’ specifies a sentence of text. The return value is always `nil’.
# File lib/vendor/estraierpure.rb, line 65
65: def add_text(text)
66: Utility::check_types({ text=>String }) if $DEBUG
67: text = text.gsub(/[ \t\r\n\v\f]+/, " ")
68: text = text.strip.squeeze(" ")
69: @dtexts.push(text) if text.length
70: nil
71: end
Get the value of an attribute. `name’ specifies the name of an attribute. The return value is the value of the attribute or `nil’ if it does not exist.
# File lib/vendor/estraierpure.rb, line 104
104: def attr(name)
105: Utility::check_types({ name=>String }) if $DEBUG
106: @attrs[name]
107: end
Get a list of attribute names of a document object. The return value is a list object of attribute names.
# File lib/vendor/estraierpure.rb, line 98
98: def attr_names()
99: @attrs.keys.sort
100: end
Concatenate sentences of the text of a document object. The return value is concatenated sentences.
# File lib/vendor/estraierpure.rb, line 115
115: def cat_texts()
116: buf = StringIO::new
117: for i in 0...@dtexts.length
118: buf.write(" ") if i > 0
119: buf.write(@dtexts[i])
120: end
121: buf.string
122: end
Dump draft data of a document object. The return value is draft data.
# File lib/vendor/estraierpure.rb, line 125
125: def dump_draft()
126: buf = StringIO::new
127: keys = @attrs.keys.sort
128: for i in 0...keys.length
129: buf.printf("%s=%s\n", keys[i], @attrs[keys[i]])
130: end
131: if @kwords
132: buf.printf("%%VECTOR");
133: @kwords.each() do |key, value|
134: buf.printf("\t%s\t%s", key, value);
135: end
136: buf.printf("\n");
137: end
138: buf.printf("\n")
139: for i in 0...@dtexts.length
140: buf.printf("%s\n", @dtexts[i])
141: end
142: for i in 0...@htexts.length
143: buf.printf("\t%s\n", @htexts[i])
144: end
145: buf.string
146: end
Get the ID number. The return value is the ID number of the document object. If the object has never been registered, -1 is returned.
# File lib/vendor/estraierpure.rb, line 93
93: def id()
94: @id
95: end
Get attached keywords. The return value is a map object of keywords and their scores in decimal string. If no keyword is attached, `nil’ is returned.
# File lib/vendor/estraierpure.rb, line 150
150: def keywords()
151: @kwords
152: end
Attache keywords. `kwords’ specifies a map object of keywords. Keys of the map should be keywords of the document and values should be their scores in decimal string. The return value is always `nil’.
# File lib/vendor/estraierpure.rb, line 86
86: def set_keywords(kwords)
87: Utility::check_types({ kwords=>Hash }) if $DEBUG
88: @kwords = kwords
89: end