Skip to content

Commit

Permalink
Merge pull request #50 from AnnaZav/japanese_corrections
Browse files Browse the repository at this point in the history
Japanese corrections
  • Loading branch information
aviks authored Jul 26, 2023
2 parents 2a6b785 + f572c35 commit 7523996
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
65 changes: 54 additions & 11 deletions src/whatlang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,64 @@ function detect_script(text::AbstractString)
[SinhalaScript() , 0],
[KhmerScript() , 0]
]
mn_hi_ka_list = [MandarinScript(), HiraganaScript(), KatakanaScript()]

half = length(text) / 2
local found
for ch in text
if is_stop_char(ch); continue; end
for i in 1:length(script_counters)
(script, count) = script_counters[i]
script, _ = script_counters[i]
found = false
if is_script(script, ch)
script_counters[i][2] += 1
found=true
if script_counters[i][2] > half; return script; end
# This check is true for all scripts except Mandarin, Hiragana and Katakana
# that all can be Japanese language
if script_counters[i][2] > half && !(script in mn_hi_ka_list) ; return script; end
end
# If script was found, move it closer to the front.
# If the text contains largely 1 or 2 scripts, this will
# cause these scripts to be eventually checked first.
if found && i>1
t=script_counters[i-1]
t = script_counters[i-1]
script_counters[i-1] = script_counters[i]
script_counters[i] = t
end
end
end

sort!(script_counters, lt=(x,y)->x[2]<y[2])
if script_counters[1][2] > 0
return script_counters[1][2]
# Count the number of Mandarin, Hiragana and Katakana characters
mandarin_count, hiragana_count, katakana_count =
map(mn_hi_ka_list) do scr
script_counters[indexin([scr], first.(script_counters))[1]][2]
end

# sort!(script_counters, lt=(x,y)->x[2]<y[2])
# if script_counters[1][2] > 0
# return script_counters[1][2]

# Special check if Mandarin, Hiragana and Katakana script found
# detection script from
# https://github.com/greyblake/whatlang-rs/blob/0c03d281a8d327558ab89632d2d997e644a8c7dd/src/core/detect.rs#L70-L100
# See https://github.com/greyblake/whatlang-rs/issues/88
if !iszero(mandarin_count) || !iszero(hiragana_count) || !iszero(katakana_count)
japanese_count = katakana_count + hiragana_count
total = mandarin_count + japanese_count

jpn_pct = japanese_count / total
if jpn_pct > 0.2
return katakana_count > hiragana_count ? KatakanaScript() : HiraganaScript()
elseif jpn_pct > 0.05
return (katakana_count > hiragana_count ? KatakanaScript() : HiraganaScript(), 0.5)
elseif jpn_pct > 0.02
return (MandarinScript(), 0.5)
else
return MandarinScript()
end
else
return nothing
end


end

function is_script(::CyrillicScript, ch::Char)
Expand Down Expand Up @@ -232,7 +258,13 @@ detect_lang_based_on_script(text::AbstractString, script::DevanagariScript, opti
detect_lang_based_on_script(text::AbstractString, script::HebrewScript, options) = detect_lang_trigrams(text, script, options)
detect_lang_based_on_script(text::AbstractString, script::EthiopicScript, options) = detect_lang_trigrams(text, script, options)
detect_lang_based_on_script(text::AbstractString, script::ArabicScript, options) = detect_lang_trigrams(text, script, options)
detect_lang_based_on_script(text::AbstractString, script::MandarinScript, options) = ("Cmn", 1.0)
function detect_lang_based_on_script(text::AbstractString, script::Union{MandarinScript, Tuple{MandarinScript, Float64}}, options)
if isequal(typeof(script), MandarinScript)
("Cmn", 1.0)
else
("Cmn", last(script))
end
end
detect_lang_based_on_script(text::AbstractString, script::BengaliScript, options) = ("Ben", 1.0)
detect_lang_based_on_script(text::AbstractString, script::HangulScript, options) = ("Kor", 1.0)
detect_lang_based_on_script(text::AbstractString, script::GeorgianScript, options) = ("Kat", 1.0)
Expand All @@ -248,7 +280,18 @@ detect_lang_based_on_script(text::AbstractString, script::OriyaScript, options)
detect_lang_based_on_script(text::AbstractString, script::MyanmarScript, options) = ("Mya", 1.0)
detect_lang_based_on_script(text::AbstractString, script::SinhalaScript, options) = ("Sin", 1.0)
detect_lang_based_on_script(text::AbstractString, script::KhmerScript, options) = ("Khm", 1.0)
detect_lang_based_on_script(text::AbstractString, script::Union{HiraganaScript, KatakanaScript}, options) = ("Jpn", 1.0)
function detect_lang_based_on_script(
text::AbstractString,
script::Union{HiraganaScript, KatakanaScript, Tuple{Union{HiraganaScript, KatakanaScript}, Float64}},
options
)
@info script
if isequal(typeof(script), Tuple{Union{HiraganaScript, KatakanaScript}, Float64})
("Jpn", last(script))
else
("Jpn", 1.0)
end
end


@enum DetectType Trigram=1 Deep=2
Expand Down Expand Up @@ -344,7 +387,7 @@ end
function(m::LanguageDetector)(text::AbstractString, options=default_options())
if text==""; throw(ArgumentError("Cannot detect language for empty text")); end
script = detect_script(text)
if script == nothing; return (nothing, nothing, 0); end
if isnothing(script); return (nothing, nothing, 0); end
lang, conf = detect_lang_based_on_script(text, script, options)
return (from_code(lang), script, conf)
end
12 changes: 12 additions & 0 deletions test/j_examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"1": "リミティング・ファクターは、トリトン・サブマリンズが建造し、ゲイブ・ニューウェル率いる海洋探査組織インクフィッシュが所有する深海探査艇(DSV)。五大洋全てに於いて有人潜水最深記録を保持している。リミティング・ファクターは、深海冒険家であり、カラディアン・オセアニック社の代表である……",
"2": "松風丸は、日本の海運会社である商船三井が運航する石炭運搬船である。2022年竣工。繊維強化プラスチック製の帆を搭載した世界初の船舶として、シップ・オブ・ザ・イヤー2022を受賞した。最大の特徴である船首の硬翼帆式風力推進装置はウインドチャレンジャーと称し、松風丸が搭載第1船となる。……",
"3": "ハロウィンの仮装とは、毎年10月31日に開かれるハロウィンのお祭りで着用される仮装コスチュームのことである。現存する最も古い言及は1585年のスコットランドのものだが、これより早くから存在した可能性もある。18世紀から19世紀の間、スコットランドのケルト諸語圏やアイルランド、マン島、ウェールズで多くの言及が見られる。……",
"4": "奈良を代表する寺院である東大寺は「古都奈良の文化財」の一部として世界遺産に登録されている。東大寺の仏像には、「奈良の大仏」として知られる盧舎那仏像をはじめ、日本仏教美術史を代表する著名作品が多く存在する……",
"5": "オーストラリアワインは、オセアニアのオーストラリアで生産されるワイン。2010年のブドウ栽培面積は世界第12位、ワイン生産量は世界7位であった。低価格ワインが主体だったが、2000年代以降には高級ワインの生産にも力がいれられるようになり……",
"6": "サマリウムは原子番号62の元素。元素記号は Sm。希土類元素の一つ(ランタノイドにも属す)。単体は灰白色の軟らかい金属であり、空気中では徐々に酸化されて表面に酸化被膜を形成する。1879年にポール・ボアボードランによって……",
"7": "アーバスキュラー菌根は、根に菌類が共生した構造である菌根の1型である。菌根菌が土壌中から吸収した水や無機栄養分、特にリンを植物に供給し、一方で植物は光合成でつくられた有機物を菌根菌に供給するという仕組みになっており、大多数の陸上植物に見られる菌根である。なお共生者となる菌根菌はグロムス類とよばれる菌群であり、植物の細胞内に侵入して栄養交換用の細かく分枝した樹枝状体を形成する。……",
"8": "コケ植物とは、維管束を持たず、胞子散布を行う、単相で有性の配偶体世代が優先する陸上植物の一群である。日本では1665種程度、世界中で2万種ほどが記録されている。ごく最近までは側系統群と考えられていたが、データセットを増やした分子系統解析では、蘚類・苔類・ツノゴケ類の3群が再び単系統群としてまとまり、残りの現生陸上植物(維管束植物)と姉妹群をなすことが明らかになった。……",
"9": "維管束を持つ。維管束は、植物体を維持し、その隅々に水や栄養を運ぶことが出来る。このため維管束植物は非維管束植物より一般に大型である。また、根・茎・葉などの分化した器官を持ち、それぞれにおいて維管束の配置が異なる。",
"10": "維管束の存在は、これらの植物の大型化を助けたものと考えられ、地上が森林で覆われることに寄与したと考えられる。これらの植物は、太い幹を発達させ、セルロースの多大な蓄積を地表にもたらした。これがさらに菌類の進化を誘導したとの考えもある。"
}
8 changes: 8 additions & 0 deletions test/whatlang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,11 @@ for (key, val) in examples
global output = d(val)
@test output[1] == Languages.from_code(key)
end

# Test Mandarin Hiragana Katakana
j_examples = JSON.parse(read(joinpath(dirname(@__FILE__), "j_examples.json"), String))

for (key, val) in j_examples
global output = d(val)
@test output[1] == Languages.Japanese()
end

0 comments on commit 7523996

Please sign in to comment.