Class: Text::Hyphen
- Inherits:
-
Object
- Object
- Text::Hyphen
- Defined in:
- lib/text/hyphen.rb
Overview
Introduction
Text::Hyphen -- hyphenate words using modified versions of TeX hyphenation patterns.
Usage
require 'text/hyphen'
hh = Text::Hyphen.new(:language => 'en_us', :left => 2, :right => 2)
# Defaults to the above
hh = TeX::Hyphen.new
word = "representation"
points = hyp.hyphenate(word) #=> [3, 5, 8, 10]
puts hyp.visualize(word) #=> rep-re-sen-ta-tion
en = Text::Hyphen.new(:left => 0, :right => 0)
fr = Text::Hyphen.new(:language = "fr", :left => 0, :right => 0)
puts en.visualise("organiser") #=> or-gan-iser
puts fr.visualise("organiser") #=> or-ga-ni-ser
Description
Creates a new Hyphen object and loads the language patterns into memory. The hyphenator can then be asked for the hyphenation of a word. If no language is specified, then the language en_us (EN_US) is used by default.
Copyright |
Copyright (c) 2004 - 2005 Austin Ziegler |
Version |
1.0.2 |
Based On |
TeX::Hyphen 0.4 Copyright ?? 2003 - 2004 Martin DeMello and Austin Ziegler, in turn based on Perl's TeX::Hyphen
|
Licence
Licensing for Text::Hyphen is unfortunately complex because of the various copyrights and licences of the source hyphenation files. Some of these files are available only under the TeX licence and others are available only under the GNU GPL while others are public domain. Each language file has these licences embedded within the file. Please consult each file's licence to ensure that it is compatible with your application.
The copyright on the Text::Hyphen application/library and the Ruby translations of hyphenation files belongs to Austin Ziegler. All other copyrights on original versions still stand; Text::Hyphen is a derivative work of these and other projects.
Application and Compilation Licences
Text::Hyphen, the application/library is licensed under the same terms as Ruby. Note that this specifically refers to the contents of bin/hyphen, lib/text/hyphen.rb, and lib/text/hyphen/language.rb.
Individual language hyphenation files are NOT licensed under these terms, but under the following MIT-style licence and the original hyphenation pattern licenses. The copyright for the original TeX hyphenation files is held by the original authors; any mistakes in conversion of these files to Ruby is attributable to the contributors to the Text::Hyphen package only.
The compilation package Text::Hyphen is licensed under the same terms as Ruby.
Blanket Language Hyphenation File Licence
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Defined Under Namespace
Classes: Language
Constant Summary
- DEBUG =
false- VERSION =
'1.0.2'- DEFAULT_MIN_LEFT =
2- DEFAULT_MIN_RIGHT =
2
Instance Attribute Summary (collapse)
-
- (Object) iso_language
readonly
Returns the language's ISO 639 ID, e.g., "en_us" or "pt".
-
- (Object) language
The name of the language to be used in hyphenating words.
-
- (Object) left
No fewer than this number of letters will show up to the left of the hyphen.
-
- (Object) right
No fewer than this number of letters will show up to the right of the hyphen.
Instance Method Summary (collapse)
- - (Object) clear_cache!
-
- (Object) hyphenate(word)
Returns a list of places where the word can be divided, as.
-
- (Object) hyphenate_to(word, size)
This function will hyphenate a word so that the first point is at most size characters.
-
- (Hyphen) initialize(options = {}) {|_self| ... }
constructor
The following initializations are equivalent:.
-
- (Object) stats
Returns statistics.
-
- (Object) visualise(word)
(also: #visualize)
Returns a visualization of the hyphenation points, so:.
Constructor Details
- (Hyphen) initialize(options = {}) {|_self| ... }
The following initializations are equivalent:
hyp = TeX::Hyphenate.new(:language => "EU")
hyp = TeX::Hyphenate.new { |h| h.language = "EU" }
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/text/hyphen.rb', line 129 def initialize( = {}) # :yields self: @iso_language = [:language] @left = [:left] @right = [:right] @language = nil @cache = {} @vcache = {} @hyphen = {} @begin_hyphen = {} @end_hyphen = {} @both_hyphen = {} @exception = {} @first_load = true yield self if block_given? @first_load = false load_language @left ||= DEFAULT_MIN_LEFT @right ||= DEFAULT_MIN_RIGHT end |
Instance Attribute Details
- (Object) iso_language (readonly)
Returns the language's ISO 639 ID, e.g., "en_us" or "pt".
123 124 125 |
# File 'lib/text/hyphen.rb', line 123 def iso_language @iso_language end |
- (Object) language
The name of the language to be used in hyphenating words. This will be a two or three character ISO 639 code, with the two character form being the canonical resource name. This will load the language hyphenation definitions from text/hyphen/language/<code> as a Ruby class. The resource 'text/hyphen/language/en_us' defines the language class Text::Hyphen::Language::EN_US. It also defines the secondary forms Text::Hyphen::Language::EN and Text::Hyphen::Language::ENG_US.
Minimal transformations will be performed on the language code provided, such that any dashes are converted to underscores (e.g., 'en-us' becomes 'en_us') and all characters are regularised. Resource names will be downcased and class names will be upcased (e.g., 'Pt' for the Portuguese language becomes 'pt' and 'PT', respectively).
The language may also be specified as an instance of Text::Hyphen::Language.
109 110 111 |
# File 'lib/text/hyphen.rb', line 109 def language @language end |
- (Object) left
No fewer than this number of letters will show up to the left of the hyphen. This overrides the default specified in the language.
89 90 91 |
# File 'lib/text/hyphen.rb', line 89 def left @left end |
- (Object) right
No fewer than this number of letters will show up to the right of the hyphen. This overrides the default specified in the language.
92 93 94 |
# File 'lib/text/hyphen.rb', line 92 def right @right end |
Instance Method Details
- (Object) clear_cache!
220 221 222 223 |
# File 'lib/text/hyphen.rb', line 220 def clear_cache! @cache.clear @vcache.clear end |
- (Object) hyphenate(word)
Returns a list of places where the word can be divided, as
hyp.hyphenate('representation')
returns [3, 5, 8, 10]. If the word has been hyphenated previously, it will be returned from a per-instance cache.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/text/hyphen.rb', line 160 def hyphenate(word) word = word.downcase $stderr.puts "Hyphenating #{word}" if DEBUG return @cache[word] if @cache.has_key?(word) res = @language.exceptions[word] return @cache[word] = make_result_list(res) if res letters = word.scan(@language.scan_re) $stderr.puts letters.inspect if DEBUG word_size = letters.size result = [0] * (word_size + 1) right_stop = word_size - @right updater = Proc.new do |hash, str, pos| if hash.has_key?(str) $stderr.print "#{pos}: #{str}: #{hash[str]}" if DEBUG hash[str].scan(@language.scan_re).each_with_index do |cc, ii| cc = cc.to_i result[ii + pos] = cc if cc > result[ii + pos] end $stderr.print ": #{result.inspect}\n" if DEBUG end end # Walk the word (0..right_stop).each do |pos| rest_length = word_size - pos (1..rest_length).each do |length| substr = letters[pos, length].join('') updater[@language.hyphen, substr, pos] updater[@language.start, substr, pos] if pos.zero? updater[@language.stop, substr, pos] if (length == rest_length) end end updater[@language.both, word, 0] if @language.both[word] (0..@left).each { |i| result[i] = 0 } ((-1 - @right)..(-1)).each { |i| result[i] = 0 } @cache[word] = make_result_list(result) end |
- (Object) hyphenate_to(word, size)
This function will hyphenate a word so that the first point is at most size characters.
227 228 229 230 231 232 233 234 |
# File 'lib/text/hyphen.rb', line 227 def hyphenate_to(word, size) point = hyphenate(word).delete_if { |e| e >= size }.max if point.nil? [nil, word] else [word[0 ... point] + "-", word[point .. -1]] end end |
- (Object) stats
Returns statistics
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/text/hyphen.rb', line 237 def stats _b = @language.both.size _s = @language.start.size _e = @language.stop.size _h = @language.hyphen.size _x = @language.exceptions.size _T = _b + _s + _e + _h + _x s = <<-EOS The language '%s' contains %d total hyphenation patterns. % 6d patterns are word start patterns. % 6d patterns are word stop patterns. % 6d patterns are word start/stop patterns. % 6d patterns are normal patterns. % 6d patterns are exceptions. EOS s % [ @iso_language, _T, _s, _e, _b, _h, _x ] end |
- (Object) visualise(word) Also known as: visualize
Returns a visualization of the hyphenation points, so:
hyp.visualize('representation')
returns rep-re-sen-ta-tion, at least for English patterns. If the word has been visualised previously, it will be returned from a per-instance cache.
210 211 212 213 214 215 216 217 |
# File 'lib/text/hyphen.rb', line 210 def visualise(word) return @vcache[word] if @vcache.has_key?(word) w = word.dup hyphenate(w).each_with_index do |pos, n| w[pos.to_i + n, 0] = '-' if pos != 0 end @vcache[word] = w end |