Skip to content

Commit

Permalink
外国語変換辞書を,改行も出力できるようにした.
Browse files Browse the repository at this point in the history
外国語辞書ファイルの1行目がタブ文字を含まず,かつコメント行ではない場合は,1行目の文字列を,その辞書データの改行コードの代わりにできる.
それに伴って,クラス Tororo の divide_by_blank メソッドを,改行をスペースと認識しないように修正

あとタイポの修正とか.

Modified   README
Modified   suikyo/suikyo.rb
Modified   tororo.rb
  • Loading branch information
rusutaku committed Oct 4, 2010
1 parent b16f8b2 commit 8ae6874
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README
@@ -1,4 +1,4 @@
tororo is licenced under GPL v.2
Copyright (c) rusutaku

開発に協力していただける方を待っております.
手のひらを蚊に刺されないように注意しましょう
66 changes: 66 additions & 0 deletions suikyo/suikyo.rb
Expand Up @@ -279,6 +279,20 @@ def punctuation?(char)

end

# 改行対応
class TororoSuikyoMore < TororoSuikyo
def initialize (table = nil)
if table.kind_of?(String) then
@table = SuikyoTable2More.new()
@table.loadfile(table)
elsif table then
@table = table
else
@table = SuikyoTable2More.new
end
end
end

class SuikyoTable
attr_reader :table_files

Expand Down Expand Up @@ -484,3 +498,55 @@ def get_word (chars)
end
end

# 改行対応
# 一行目を改行用文字列として,データ内の改行用文字列を改行コードに置き換える
class SuikyoTable2More < SuikyoTable2
def loadfile (filename, tablepath = nil)
filepath = SuikyoTable::loadpath(filename, tablepath)
if FileTest::exist?(filepath) then
@table_files.push(filepath)
else
$stderr.puts "Suikyo.rb: conv-table '#{filepath}' is not found."
return false
end

comment_flag = false

lines = open(filepath, "r").readlines

# 改行のおまじないを読んでみる
newline_string = ""
magic_line = lines[0]
unless magic_line =~ /\t/ then
magic_line.chomp!
unless magic_line =~ /^\#|^\s*$/ then
newline_string = magic_line
lines.slice!(0)
end
end

lines.each{|line|
line.chomp!
## The function 'toeuc' converts half-width Katakana to full-width.
# line = line.toeuc.chomp
if line =~ /^\/\*/ then
comment_flag = true
end
unless line =~ /^\#|^\s*$/ or comment_flag then
(string, result, cont) = line.sub(/^ /, "").split(/\t/)
if result.nil? then
self.unset(string)
else
if newline_string.length > 0 then
result.gsub!(newline_string, "\r\n")
end
self.set(string, result, cont)
end
end
if line =~ /\*\// then
comment_flag = false
end
}
return true
end
end
8 changes: 4 additions & 4 deletions tororo.rb
Expand Up @@ -8,7 +8,7 @@
class Tororo
attr_reader :version
def initialize
@version = "0.1.3"
@version = "0.2.0"
@log_path_in = ""
@log_lines = []
@count = 0 # すでに変換した行数
Expand All @@ -31,7 +31,7 @@ def load_config
}
@charas = build_table(CharacterID, config["character_id_tables"])
@line_allower = build_table(LineAllower, config["line_whitelist_tables"])
@foreign_lang = build_table(TororoSuikyo, config["foreign_lang_dics"])
@foreign_lang = build_table(TororoSuikyoMore, config["foreign_lang_dics"])
@nippon = build_table(TororoSuikyo, config["hiragana_to_kanjikana_dics"])
@word_denier = build_table(WordDenier, config["word_blacklist_tables"])
@charas.output_file = config["character_id_table_output"]
Expand Down Expand Up @@ -97,7 +97,7 @@ def conv_to_file(str, filename)

def conv(str)
# ASCII-8bit 以外の文字があったらそのまま返す
# ここは Ruby 1.9 だとエラーになる /[^\u0000-\x00FF]/ だといい?
# ここは Ruby 1.9 だとエラーになる /[^\u0000-\u00FF]/ だといい?
return str if /[^\x00-\xFF]/ =~ str
# フィルターで許可されていれば変換
if @line_allower.apply_filter(str) then
Expand Down Expand Up @@ -179,7 +179,7 @@ def setoff(str)
end

def divide_by_blank(str)
array = str.split(" ")
array = str.split(/ /)
return array
end
end
Expand Down

0 comments on commit 8ae6874

Please sign in to comment.