Class: Text::Hyphen::Dictionary
- Inherits:
-
Object
- Object
- Text::Hyphen::Dictionary
- Defined in:
- lib/text/hyphen.rb,
ext/text/text_hyphen.c
Class Method Summary collapse
Instance Method Summary collapse
- #analyze(string) ⇒ Object (also: #analyse)
- #best_chance(word) ⇒ Object
- #best_chance_before(pos, word) ⇒ Object
- #first_chance(word) ⇒ Object
- #hyphenate_before(pos, word) ⇒ Object
- #hyphenate_best(word) ⇒ Object
- #hyphenate_best_before(pos, word) ⇒ Object
- #hyphenate_first(word) ⇒ Object
- #last_chance_before(pos, word) ⇒ Object
Class Method Details
.new(filename) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'ext/text/text_hyphen.c', line 8 VALUE th_dict_new(VALUE class, VALUE filename) { VALUE rb_dict; HyphenDict * dictionary; const char * fname; fname = StringValuePtr(filename); if(!(dictionary = hnj_hyphen_load(fname))) { rb_sys_fail(fname); } rb_dict = Data_Wrap_Struct(class, 0, hnj_hyphen_free, dictionary); rb_obj_call_init(rb_dict, 0, 0); return rb_dict; } |
Instance Method Details
#analyze(string) ⇒ Object Also known as: analyse
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'ext/text/text_hyphen.c', line 21 VALUE th_dict_analyze(VALUE self, VALUE string) { HyphenDict * dict; const char * word; int word_size, hyph_status, hyph_weight, idx; char * hyphens; VALUE rb_hyphens, downcase; ID s_downcase; s_downcase = rb_intern("downcase"); downcase = rb_funcall(string, s_downcase, 0); word = StringValuePtr(downcase); word_size = strlen(word); hyphens = malloc(word_size * 2); Data_Get_Struct(self, HyphenDict, dict); hnj_hyphen_hyphenate(dict, word, word_size, hyphens); /* * This used to work with another version of libhyphen - but which? * if(hnj_hyphen_hyphenate(dict, word, word_size, hyphens)) { rb_funcall(rb_mKernel, rb_intern("puts"), 1, rb_str_new2("bailing out due to hyphenate error...")); free(hyphens); rb_raise(rb_eRuntimeError, "Hyphenation Error"); } */ rb_hyphens = rb_ary_new(); for(idx = 0; idx < word_size; idx++) { hyph_status = hyphens[idx] - 48; if(hyph_status & 1) { hyph_weight = (hyph_status - 1) / 2 + 1; } else { hyph_weight = (hyph_status / 2) * -1; } rb_ary_push(rb_hyphens, INT2FIX(hyph_weight)); } free(hyphens); return rb_hyphens; } |
#best_chance(word) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/text/hyphen.rb', line 11 def best_chance(word) weights = analyze(word) best = weights.max if(best > 0) weights.index(best) + 1 end end |
#best_chance_before(pos, word) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/text/hyphen.rb', line 18 def best_chance_before(pos, word) weights = analyze(word) valid = weights[0,pos] best = valid.max if(best > 0) valid.rindex(best) + 1 end end |
#first_chance(word) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/text/hyphen.rb', line 26 def first_chance(word) idx = 0 analyze(word).any? { |weight| idx += 1 if(weight > 0) return idx end } nil end |
#hyphenate_before(pos, word) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/text/hyphen.rb', line 57 def hyphenate_before(pos, word) hyph = word.dup if(pos = last_chance_before(pos, word)) hyph[pos,0] = '-' end hyph end |
#hyphenate_best(word) ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/text/hyphen.rb', line 36 def hyphenate_best(word) hyph = word.dup if(pos = best_chance(word)) hyph[pos,0] = '-' end hyph end |
#hyphenate_best_before(pos, word) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/text/hyphen.rb', line 43 def hyphenate_best_before(pos, word) hyph = word.dup if(pos = best_chance_before(pos, word)) hyph[pos,0] = '-' end hyph end |
#hyphenate_first(word) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/text/hyphen.rb', line 50 def hyphenate_first(word) hyph = word.dup if(pos = first_chance(word)) hyph[pos,0] = '-' end hyph end |
#last_chance_before(pos, word) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/text/hyphen.rb', line 64 def last_chance_before(pos, word) weights = analyze(word) pos.downto(0) { |pos| if(weights.at(pos - 1) > 0) return pos end } nil end |