From ccd024f6501b61e6d385571a140b5c1d458f2a3d Mon Sep 17 00:00:00 2001 From: nangxiang Date: Sat, 26 Nov 2011 17:33:23 +0900 Subject: [PATCH] Add comments on radicals --- lib/sw2wxr.rxml | 15 +++++++---- sw2wxr.rb | 82 +++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 69 insertions(+), 28 deletions(-) diff --git a/lib/sw2wxr.rxml b/lib/sw2wxr.rxml index a33cb16..e841a50 100644 --- a/lib/sw2wxr.rxml +++ b/lib/sw2wxr.rxml @@ -18,6 +18,7 @@ + <% chapter_post_id = WXRConverter.get_post_id %> <% (chapter_num, title) = @title %> @@ -25,7 +26,9 @@ <%= chapter_num %> <%= chapter_post_id %> shirasuhiroyuki> - ]]> + + <%= @radicals_in_chapter %> + <%= @volume_wordnum %>]]> open publish @@ -37,7 +40,7 @@ <% menu_order = 0 %> <% @contents.each do |radical_info| %> - <% (radical, words, part, associations) = radical_info %> + <% (radical, words, radical_wordnum, associations) = radical_info %> <% (rword, rhref) = radical %> <% menu_order += 1 %> @@ -47,7 +50,7 @@ <%= rword %> shirasuhiroyuki> - ]]> + <%= radical_wordnum %>]]> open publish @@ -59,9 +62,11 @@ + + <% word_order = 0 %> <% words.each do |word| %> <% (char, wordid, whref, info) = word %> - + <% word_order += 1 %> <%= wordid + ':' + char %> @@ -75,7 +80,7 @@ publish page <%= radical_post_id %> - 1 + <%= word_order %> <% end %> diff --git a/sw2wxr.rb b/sw2wxr.rb index afa3ff0..bdc0c78 100644 --- a/sw2wxr.rb +++ b/sw2wxr.rb @@ -25,6 +25,7 @@ class WXRConverter @sw_seals = SWSeals.new @unicode = UnicodeUtility.new @title = '' + @volume_wordnum = '' # contents = radical_info+ # radical_info = radical, word_info+, part # word_info = word, variants* @@ -33,7 +34,8 @@ class WXRConverter @words = nil @part = nil parse(File.open(source_path)) - test + @radicals_in_chapter = get_radical_associations(@contents) +# test end #--- Dummy @@ -67,26 +69,34 @@ class WXRConverter parse_content(doc) parse_wordnum(doc) end + (_, title, _) = @title + if title == '說文解字第十一篇上一' then + push_radical_wordnum([]) + end end def parse_chapter(doc) if doc['chapter'] then title = doc['chapter'] - title =~ /說文解字第/u + title =~ /說文解字[第弟]/u chapter_num = $~.post_match href = get_chapter_uri(chapter_num) @title = [chapter_num, title, href] + # 例外 + if title == '說文解字第十一篇上二' then + push_radical('水') + end end end def parse_content(doc) if doc['content'] then doc['content'].each do |wordinfo| - # TODO: positionの付加 word = wordinfo['word'] + position = wordinfo['position'] info = wordinfo['content'] wwordid = wordinfo['id'] wordid = wwordid.delete('w') push_radical(word) if radicalp(wordid) - push_word(word, wordid, info) + push_word(word, wordid, position, info) # printf("%s ", word) end end @@ -105,12 +115,12 @@ class WXRConverter @words = Array.new printf(">> %s\n", word) end - def push_word(word, wordid, info) + def push_word(word, wordid, position, info) (chapter_num, _, _) = @title (radical, _) = @radical href = get_word_uri(chapter_num, radical, word, wordid) if info then - info_string = get_info_string(info) + info_string = get_word_info_string(word, position, info) @words.push([word, wordid, href, info_string]) else @words.push([word, wordid, href, []]) # 十三篇上糸部 最後 w4672491 @@ -118,26 +128,45 @@ class WXRConverter end def parse_wordnum(doc) if doc.has_key?('part') then - push_wordnum('part', doc) + push_radical_wordnum(doc['part']) elsif doc.has_key?('volume') - push_wordnum('volume', doc) + push_volume_wordnum(doc['volume']) end end - def push_wordnum(type, doc) - wordnum = [type, doc] + def push_radical_wordnum(doc) + wordnum = get_info_string(doc, "\n\n") associations = get_associations(@words) @contents.push([@radical, @words, wordnum, associations]) end + def push_volume_wordnum(doc) + @volume_wordnum = get_info_string(doc, "\n\n") + end def get_associations(words) - associations = "
    \n" + associations = "

    \n" words.each do |word_info| (word, wordid, href, infostr) = word_info - associations += sprintf("

  1. %s
  2. \n", href, word) + associations += sprintf("%s ", href, word) + end + associations += "

    \n" + return associations + end + def get_radical_associations(contents) + associations = " 部首
      \n" + contents.each do |radical_info| + (radical, _, _, _) = radical_info + (chapter_num, _, _) = @title + (rword, href) = radical +# href = get_radical_uri(chapter_num, rword) + printf(">>> %s %s\n", chapter_num, rword) + associations += sprintf("
    1. %s
    2. ", href, rword) end associations += "
    \n" end - def get_info_string(info) - info_string = "" + def get_word_info_string(word, position, info) + head = sprintf("word: %s\nposition: %s\n\n", word, position) + return get_info_string(info, head) + end + def get_info_string(info, info_string) info.each do |line| if line.has_key?('ex') then info_string += sprintf("ex: %s\n", line['ex']) @@ -161,14 +190,21 @@ class WXRConverter end end -Dir.foreach(SOURCE_DIR) do |filename| -# if filename =~ /^.*\.yml$/ then - if filename =~ /^test.yml$/ then - printf("\n> %s\n", filename) - converter = WXRConverter.new(File.join(SOURCE_DIR, filename)) - outfile = File.basename(filename, 'yml') + 'xml' - out = File.open(File.join(TARGET_DIR, outfile), "w") - out.print converter.render() +unless ARGV.empty? then + filename = ARGV[0] + printf("\n%% %s\n", filename) + converter = WXRConverter.new(File.join(SOURCE_DIR, filename)) + print converter.render() +else + Dir.foreach(SOURCE_DIR) do |filename| + if filename =~ /^.*\.yml$/ then + if filename != 'v29.yml' and filename != 'v30.yml' then + printf("\n> %s\n", filename) + converter = WXRConverter.new(File.join(SOURCE_DIR, filename)) + outfile = File.basename(filename, 'yml') + 'xml' + out = File.open(File.join(TARGET_DIR, outfile), "w") + out.print converter.render() + end + end end end - -- 2.11.0