Skip to content

Commit

Permalink
fix(splitter): when merge across punctuation on section stage the sub…
Browse files Browse the repository at this point in the history
…string inside section is not merged
  • Loading branch information
DoodleBears committed Oct 17, 2024
1 parent 4f7718c commit 638d41f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
80 changes: 43 additions & 37 deletions split_lang/split/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ def split_by_lang(
sections = self._merge_substrings_across_punctuation_based_on_sections(
sections=sections
)
# sections = self._smart_merge_all(pre_split_section=pre_split_section)

if self.merge_across_digit: # 合并跨数字的 SubString
sections = self._merge_substrings_across_digit_based_on_sections(
sections=sections
)
# sections = self._smart_merge_all(pre_split_section=pre_split_section)

if self.merge_across_newline:
sections = self._merge_substrings_across_newline_based_on_sections(
sections=sections
)
# sections = self._smart_merge_all(pre_split_section=pre_split_section)

substrings: List[SubString] = []
for section in sections:
Expand Down Expand Up @@ -151,8 +154,7 @@ def add_substring(lang_section_type: LangSectionType):
add_substring(current_lang)
current_lang = LangSectionType.NEWLINE
else:
add_substring(current_lang)
current_lang = LangSectionType.PUNCTUATION
pass
else:
if current_lang != LangSectionType.OTHERS:
add_substring(current_lang)
Expand Down Expand Up @@ -244,7 +246,17 @@ def _split(

section_index += section_len

pre_split_section = self._smart_merge_all(pre_split_section)
if self.debug:
logger.debug("---------after split:")
for section in pre_split_section:
logger.debug(section)

return pre_split_section

# MARK: smart merge substring together

def _smart_merge_all(self, pre_split_section: List[SubStringSection]):
for section in pre_split_section:
if (
section.lang_section_type is LangSectionType.PUNCTUATION
Expand All @@ -259,10 +271,6 @@ def _split(
section.substrings.clear()
section.substrings = smart_concat_result

if self.debug:
logger.debug("---------after split:")
for section in pre_split_section:
logger.debug(section)
return pre_split_section

# MARK: _parse_without_zh_ja
Expand Down Expand Up @@ -338,7 +346,7 @@ def _smart_merge(
lang_text_list = self._merge_substrings(lang_text_list)
lang_text_list = self._merge_middle_substr_to_two_side(lang_text_list)
lang_text_list = self._merge_substrings(lang_text_list)
# FIXME: get_languages 需要根据不同语言进行不同处理
# NOTE: get_languages 需要根据不同语言进行不同处理
lang_text_list = self._get_languages(
lang_text_list=lang_text_list,
lang_section_type=lang_section_type,
Expand Down Expand Up @@ -855,38 +863,29 @@ def _merge_substrings_across_digit_based_on_sections(
logger.debug(section)
return new_sections_merged

# MARK: _merge_substring_across_digit
def _merge_substring_across_digit(
def _merge_substrings_across_punctuation(
self,
substrings: List[SubString],
) -> List[SubString]:
new_substrings: List[SubString] = []
last_lang = ""

for _, substring in enumerate(substrings):
if new_substrings:
if substring.is_digit:
if new_substrings[-1].is_digit:
new_substrings[-1].text += substring.text
new_substrings[-1].length += substring.length
else:
new_substrings[-1].text += substring.text
new_substrings[-1].length += substring.length
lang = ""
for substring in substrings:
if (
substring.lang == "punctuation"
and substring.text.strip() not in self.not_merge_punctuation
):
if new_substrings and new_substrings[-1].lang == lang:
new_substrings[-1].text += substring.text
new_substrings[-1].length += substring.length
else:
if substring.lang == last_lang or last_lang == "":
new_substrings[-1].text += substring.text
new_substrings[-1].length += substring.length
new_substrings[-1].lang = (
substring.lang
if new_substrings[-1].lang == "digit"
else new_substrings[-1].lang
)
else:
new_substrings.append(substring)
last_lang = substring.lang
new_substrings.append(substring)
else:
new_substrings.append(substring)

if substring.lang != lang:
new_substrings.append(substring)
else:
new_substrings[-1].text += substring.text
new_substrings[-1].length += substring.length
lang = substring.lang if substring.lang != "punctuation" else lang
return new_substrings

# MARK: _merge_substrings_across_punctuation based on sections
Expand Down Expand Up @@ -977,8 +976,10 @@ def _merge_substrings_across_punctuation_based_on_sections(
for substr_index, substr in enumerate(section.substrings):
if substr_index == 0:
substr.index = (
new_sections[section_index - 1].substrings[-1].index
+ new_sections[section_index - 1].substrings[-1].length
new_sections_merged[section_index - 1].substrings[-1].index
+ new_sections_merged[section_index - 1]
.substrings[-1]
.length
)
else:
substr.index = (
Expand All @@ -989,7 +990,9 @@ def _merge_substrings_across_punctuation_based_on_sections(
for section in new_sections_merged:
if section.lang_section_type == LangSectionType.ZH_JA:
section.substrings = self._special_merge_for_zh_ja(section.substrings)

section.substrings = self._merge_substrings_across_punctuation(
section.substrings
)
if self.debug:
logger.debug(
"---------------------------------after_merge_punctuation_sections:"
Expand Down Expand Up @@ -1036,7 +1039,7 @@ def _get_languages(
self,
lang_text_list: List[SubString],
lang_section_type: LangSectionType,
lang_map: Dict[str, str],
lang_map: Dict[str, str] = None,
):
if lang_section_type in [
LangSectionType.DIGIT,
Expand All @@ -1045,6 +1048,9 @@ def _get_languages(
]:
return lang_text_list

if lang_map is None:
lang_map = self.lang_map

for _, substr in enumerate(lang_text_list):
cur_lang = detect_lang_combined(
text=substr.text, lang_section_type=lang_section_type
Expand Down
8 changes: 7 additions & 1 deletion tests/data/correct_split.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ Vielen Dank |merci beaucoup |for your help|.
Ich bin müde |je suis fatigué |and I need some rest|.
Ich mag dieses Buch |ce livre est intéressant |and it has a great story|.
Ich mag dieses Buch|, |ce livre est intéressant|, |and it has a great story|.
入学早々隣のクラスの絵奈は男子からの人気者。新入生代表の挨拶も絵奈だったから目立ってしまって余計にあの子誰?ってなった。絵奈と私には近所に住む幼稚園の頃から仲の良い幼なじみの澤村 和樹(さわむら かずき)という同じ中学の同級生でもある男子生徒がいる。絵奈も私もかずくんと呼んでいる。かずくんは小学1年生の頃からサッカーをやっていて、高学年になる頃には県の選抜に選ばれたりと、丸山台中の新入生では期待の星。何でユースとかにいかないで、公立の中学校でサッカー部なのかはわからない。かずくんとは絵奈より私の方が話が合うことが多かった。小学生の頃もよく3人で遊んだりもしたし、中学生になってからも3人で朝一緒に行こうなんて約束していた。迎えた入学式後の初登校日。「忘れ物とかない?気を付けて行くんだよ。急がなくても充分間に合うからね。」私にだけそう言ったママ。絵奈には「いってらっしゃい」だけ。(二人とも一緒に行くのに、私だけ落ち着きないみたいに思われてる…?)「ほら、早く行くよ。ママ、いってきます♪」私の横で姉の絵奈が私に向かってそう言うと、ママには笑顔で挨拶をしていた。そそくさと出てしまう絵奈。「ちょ、ちょっと(汗)充分間に合うからねってママ言ってるじゃん(汗)んじゃいってきま〜す!」そう言って、私も絵奈の後を急いで追う。外に出るとかずくんがちょっと遠くから歩いてきているのが見えたので、私は大声で手を振って呼んだ。「かずく〜ん!!おはよー!!」私の大声に気づいたかずくんは走ってきた。「よぉ!!絵奈と残念な方の妹!(笑)」(こいつ…面白がってやがるな…)絵奈が新入生代表で目立ってしまって、私は自分のクラスで残念な方の妹だと、昨日、ふざけた男子に言われたのだ…そしてかずくんと私は同じクラスなのでそれを面白がって言ってきたのだろう…うるせぇーよって言ってやろうと思ったら、私より先に絵奈が口を開いた。「かずくん、残念な方の妹って何?」絵奈に聞かれるとちょっとバツが悪そうなかずくんが渋々答える。「いや、その、実は俺達のクラスで、ふざけて紗羅が残念な方の妹って男子にからかわれてたんだよ(汗)」「は?で、かずくんも面白がって、そんなこと言ってるわけ?くだらないことやめなよ。そういう時は普通、助ける男がカッコいいんでしょ。一緒になって紗羅が気にしてないからって最悪だよ。」絵奈はどちらかと言うと温厚で優しいタイプなのに少し怒っているように言ったので、かずくんはしょんぼり、私も珍しく絵奈が怒っているので少しびっくりした…3人でちょっと気まずくなって歩くのに耐えられない私が絵奈に言った。「まぁまぁ、そんなツンケンしないでさ(汗)絵奈らしくないよ?かずくんも悪気があって言ったわけじゃないんだし、許してあげなよ(笑)」「私らしいって何?紗羅は悔しくないの?残念な方の妹とか言われて。そもそも紗羅がそんなお気楽な感じだから駄目なんでしょ(怒)私は紗羅が悪口言われてたら自分のことのように面白くないよ(怒)」(怒りの矛先が私に向き始めたな…)絵奈は温厚で優しく優等生タイプだけど、怒るとちょっと面倒くさいタイプ…何日も根に持つし、頑固なところもある。私は対照的でカッとなっても、1日経てばそんなこと忘れてるし、あんまり根に持つこともない。頑固な所は同じかもしれないけど。怒り出すと絵奈をなだめるのは結構大変…普段あんまり怒らないタイプだから、かもしれないけど…「そのさ、絵奈は私と違っておとなしめな品のあるタイプなんだから、あんまりカッカしてるとイメージ崩れるよ?なんちゃって(笑)かずくんも少しびっくりしてるでしょ(汗)ほらほら、せっかく入学して登校初日なのに空気悪いのおしまいにして楽しく行こうよ♪(笑)」一応、私がそう言うとカッカしてた絵奈も機嫌を少し直してくれて、かずくんもごめんって謝って一件落着し、その後は仲良く登校し中学校へ無事に到着。「んじゃまた帰りね〜♪」そう言って、絵奈とは別々の教室へ。私はかずくんと同じクラスへ入る。先生が来て、授業が始まる。勉強は嫌いだ…(今日の給食なんだっけかなぁ〜)なんて授業に集中していないと見事に先生に当てられているのに気づかない。隣の席のかずくんが小さな声で「おーい。紗羅。おーい」って言っているのに気づく。(ん?何?教科書忘れたのかこいつは本当にサッカーしかできない奴だからなぁ。しょうがないなぁ)机をくっつけて教科書を見せてやろうとするとかずくんはオドオドする。「ちげーよ(汗)紗羅(汗)」また小さな声でかずくんが言った後には怒った先生が私の目の前にいた…「山本、お前何やってんだ?あくびしたり、外をボケーっと見てたり。同じ山本のお前の姉とは大違いだな。」先生は不機嫌そうに言う。(すいませんね〜絵奈だったら、ちゃんと授業聞いてるんでしょうね〜私は残念な方の妹なので(笑))なんて怒られているはずなのに思っていると、朝、絵奈から怒られたからか、かずくんが私を庇った。「その、先生。絵奈は絵奈。紗羅は紗羅ですよ。」かずくんの言葉に先生もちょっと悪いと思ったのか、ブツブツ言いながら戻っていき、私がボケーっとしていたことは流されて終わった。(おぉーかずくんが助けてくれた(笑)もしかして、絵奈に朝怒られたから?もしかして、かずくんも昔から絵奈のことが…いや、考えるのはやめよう。私達3人は幼馴染。変に関係がおかしくなるのも嫌だし。)なんてふと思った。そして、この先、私達3人の関係は壊れていくことにこの時は気づかなかった…
The shirt is |9.15 |dollars|.
The shirt is |233 |dollars|.
lang|-|split
I have |10|, |€
日本のメディアでは|「|匿名掲示板|」|であると紹介されることが多いが|、|2003年1月7日から全書き込みについて|IP|アドレスの記録・保存を始めており|、|厳密には匿名掲示板ではなくなっていると|CNET Japan|は報じている
日本語|(|にほんご|、|にっぽんご|)|は|、|日本国内や|、|かつての日本領だった国|、|そして国外移民や移住者を含む日本人同士の間で使用されている言語|。|日本は法令によって公用語を規定していないが|、|法令その他の公用文は全て日本語で記述され|、|各種法令において日本語を用いることが規定され|、|学校教育においては「国語」の教科として学習を行うなど|、|事実上日本国内において唯一の公用語となっている|。
日语是日本通用语及事实上的官方语言|。|没有精确的日语使用人口的统计|,|如果计算日本人口以及居住在日本以外的日本人|、|日侨和日裔|,|日语使用者应超过一亿三千万人|。
8 changes: 2 additions & 6 deletions tests/split_acc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

from split_lang.config import DEFAULT_LANG
from split_lang.model import LangSectionType
from split_lang.split.splitter import (
SubString,
LangSplitter,
)
from split_lang.split.splitter import LangSplitter, SubString
from split_lang.split.utils import PUNCTUATION
from tests.test_config import TEST_DATA_FOLDER

Expand Down Expand Up @@ -60,8 +57,7 @@ def get_corrected_split_result(


def simple_test(splitter: LangSplitter, debug: bool = False):

text_file_name = "correct_split_merge_punc.txt"
text_file_name = "correct_split.txt"
correct_split = get_corrected_split_result(
splitter=splitter, text_file_path=f"{TEST_DATA_FOLDER}/{text_file_name}"
)
Expand Down

0 comments on commit 638d41f

Please sign in to comment.