class String def clean2 gsub(/\[([^\[]*)\([^\)]*\)\]/){ $1 }.gsub(/[\[\],]/,'') end def clean gsub(/\[([^\[]*)\([^\)]*\)\]/){ $1 }.gsub(/[\[\],]/,'').gsub(/\.\.\./,'') end def replace_yomi gsub(/\[[^\[]*\(([^\)]*)\)\]/){ "[#{$1}]" } end end class PermutedIndex require 'jsort' def initialize(dictfile, input=STDIN, index='index', header='', footer='') @dicwords = {} @key = {} @yomi = {} @done = {} @wordyomi = {} @tag = {} @index = index @header = header @footer = footer if dictfile then File.open(dictfile,"r"){ |f| f.each { |line| next if line =~ /^[#\s]/ word, subst = line.split subst = word if subst == '' @dicwords[word] = subst } } end @input = input @input = STDIN if input.nil? @input = File.open(input,"r") if input.class == String end def header(f) f.puts(@header) end def trailer(f) f.puts(@footer) end def permute(key,str) @key[str.clean] = key a = str.split(/\[/) len = a.length (1...len).each{ |i| pre = a[0...i].join('[') post = '[' + a[i...len].join('[') entry = pre.clean + "," + post.clean @yomi[entry] = post.replace_yomi.clean.yomi_for_sort a[i] =~ /^([^\]]*)\]/ w = $1 word = "[#{w}]" @tag[entry] = word.replace_yomi.clean.roma @wordyomi[word.clean2] = word.replace_yomi.clean } end def expand(key,prefix,data) @dicwords.keys.each { |w| if data =~ /^(\[#{w}\])(.*)$/ then r = $2 expand(key,prefix+'['+w+']',r); @dicwords[w].split(/,/).each { |s| expand(key,prefix+'['+s+']',r); } return end } md = /^(.[^\[]*)(\[.*)$/.match(data) if md then expand(key,prefix+md[1],md[2]) else permute(key,prefix+data) unless @done[prefix+data] @done[prefix+data] = true end end def body1(f) @input.each { |line| next if line =~ /^[#\s]/ key, str = line.split(/\t+/) expand(key,'',str) } @prevkey = '' @wordyomi.keys.sort { |a,b| @wordyomi[a].yomi_for_sort.upcase <=> @wordyomi[b].yomi_for_sort.upcase }.each { |key| found = midashi2(@wordyomi[@prevkey].to_s.yomi_for_sort,@wordyomi[key].to_s.yomi_for_sort) @prevkey = key if found then f.puts "

#{found}

" end tag = @wordyomi[key].roma f.puts "#{key} " } end def midashi(prev,cur) aiu = [ 'A-Z', 'あ', 'い', 'う', 'え', 'お', 'か', 'き', 'く', 'け', 'こ', 'さ', 'し', 'す', 'せ', 'そ', 'た', 'ち', 'つ', 'て', 'と', 'な', 'に', 'ぬ', 'ね', 'の', 'は', 'ひ', 'ふ', 'へ', 'ほ', 'ま', 'み', 'む', 'め', 'も', 'や', 'ゆ', 'よ', 'ら', 'り', 'る', 'れ', 'ろ', 'わ', 'を', 'ん' ] found = nil aiu.each { |c| if prev < c && cur >= c then found = c end } found end def midashi2(prev,cur) aiu = [ 'A-Z', 'あ', 'か', 'さ', 'た', 'な', 'は', 'ま', 'や', 'ら', 'わ', ] found = nil aiu.each { |c| if prev < c && cur >= c then found = c end } found end def th(f,prev,cur) found = midashi(prev,cur) if found then f.puts "" f.puts "\n" f.puts "
 #{found}
" end end def body2(f) @prevkey = '' @preventry = '' @prevtag = '' @yomi[@prevkey] = '' @yomi.keys.sort { |a,b| @yomi[a].upcase <=> @yomi[b].upcase }.each { |entry| next if entry == '' th(f,@yomi[@preventry],@yomi[entry]) tag = @tag[entry] tagout = nil if @prevtag != tag then f.puts "" @prevtag = tag tagout = true end key = @key[entry.clean] if (@prevkey && @prevkey != key) || tagout then s1, s2 = entry.split(/,/) f.puts "" f.puts "" f.puts "" f.puts "
・#{s1}#{s2}
" end @prevkey = key @preventry = entry } end def output File.open("#{@index}1.html","w"){ |f| header(f) body1(f) trailer(f) } File.open("#{@index}2.html","w"){ |f| header(f) body2(f) trailer(f) } end end