From 95e5aef15b97f4cb03b4c0cf62b41d3c165a536c Mon Sep 17 00:00:00 2001 From: FacerAin Date: Tue, 22 Nov 2022 23:13:20 +0000 Subject: [PATCH 1/8] feat: add translate chinese character --- requirements.txt | 1 + src/data_loader/data_loader.py | 4 +- src/utils/__init__.py | 3 +- src/utils/representation.py | 78 ++++++++++++++++++++++++++++------ tests/test_representation.py | 64 ++++++++++++++++++++++++++++ tests/test_translation.py | 31 ++++++++++++++ 6 files changed, 165 insertions(+), 16 deletions(-) create mode 100644 tests/test_representation.py create mode 100644 tests/test_translation.py diff --git a/requirements.txt b/requirements.txt index dce7c52..20c55fb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ mlflow==2.0.1 streamlit==1.14.1 seaborn==0.12.0 ipykernel==6.17.1 +hanja==0.13.3 diff --git a/src/data_loader/data_loader.py b/src/data_loader/data_loader.py index eab67c5..9d76b82 100644 --- a/src/data_loader/data_loader.py +++ b/src/data_loader/data_loader.py @@ -3,7 +3,7 @@ import pandas as pd import torch -from src.utils import entity_representation +from src.utils import representation def preprocessing_dataset(dataset): @@ -38,7 +38,7 @@ def tokenized_dataset(self, dataset, tokenizer): """tokenizer에 따라 sentence를 tokenizing 합니다.""" concat_entity = [] for e01, e02, sentence in zip(dataset["subject_entity"], dataset["object_entity"], dataset["sentence"]): - temp = entity_representation(e01, e02, sentence, method=None) + temp = representation(e01, e02, sentence, entity_method=None, translation_methods=["chinese"]) concat_entity.append(temp) tokenized_sentences = tokenizer( concat_entity, diff --git a/src/utils/__init__.py b/src/utils/__init__.py index a3edba2..6cc4821 100644 --- a/src/utils/__init__.py +++ b/src/utils/__init__.py @@ -1,6 +1,7 @@ from .arguments import DataTrainingArguments, ModelArguments, get_training_args from .control_mlflow import save_model_remote, set_mlflow_logger from .get_train_valid_split import get_train_valid_split -from .representation import entity_representation +from .preprocess import replace_symbol +from .representation import representation from .set_seed import set_seed from .utils import label_to_num, num_to_label diff --git a/src/utils/representation.py b/src/utils/representation.py index 4addea7..3be8bdd 100644 --- a/src/utils/representation.py +++ b/src/utils/representation.py @@ -1,25 +1,51 @@ from typing import Tuple +import hanja -def extraction(subject: str) -> Tuple[int, int, str, str]: + +def translation(sentence: str, method: str = None) -> str: + assert method in [ + None, + "chinese", + ], "입력하신 method는 없습니다." + if method is None: + return sentence + + if method == "chinese": + return hanja.translate(sentence, "substitution") + + +def extraction(entity: str) -> dict: """ Args: - subject (str): subject or object + entity (str): subject or object Returns: - Tuple[int,int,str,str]: return subject object idx or subject object + Dict[int,int,str,str]: return dict containing entity information """ - subject_entity = subject[:-1].split(",")[-1].split(":")[1] - subject_length = len(subject.split(",")) - sub_start_idx = int(subject.split(",", subject_length - 3)[subject_length - 3].split(",")[0].split(":")[1]) - sub_end_idx = int(subject.split(",", subject_length - 3)[subject_length - 3].split(",")[1].split(":")[1]) - subject = "".join(subject.split(",", subject_length - 3)[: subject_length - 3]).split(":")[1] - subject_entity = subject_entity.replace("'", "").strip() + entity_type = entity[:-1].split(",")[-1].split(":")[1] + entity_length = len(entity.split(",")) + start_idx = int(entity.split(",", entity_length - 3)[entity_length - 3].split(",")[0].split(":")[1]) + end_idx = int(entity.split(",", entity_length - 3)[entity_length - 3].split(",")[1].split(":")[1]) + entity_word = "".join(entity.split(",", entity_length - 3)[: entity_length - 3]).split(":")[1] + entity_word = entity_word.replace("'", "").strip() + entity_type = entity_type.replace("'", "").strip() + + entity_dict = { + "start_idx": start_idx, + "end_idx": end_idx, + "entity_type": entity_type, + "entity_word": entity_word, + } + + return entity_dict - return sub_start_idx, sub_end_idx, subject, subject_entity +def unpack_entity_dict(start_idx, end_idx, entity_type, entity_word): + return start_idx, end_idx, entity_word, entity_type -def entity_representation(subject: str, object: str, sentence: str, method: str = None) -> str: + +def entity_representation(subject_dict: dict, object_dict: dict, sentence: str, method: str = None) -> str: """ Args: subject (str): subject dictionary @@ -40,8 +66,8 @@ def entity_representation(subject: str, object: str, sentence: str, method: str "typed_entity_marker_punct", ], "입력하신 method는 없습니다." - sub_start_idx, sub_end_idx, subject, subject_entity = extraction(subject) - obj_start_idx, obj_end_idx, object, object_entity = extraction(object) + sub_start_idx, sub_end_idx, subject, subject_entity = unpack_entity_dict(**subject_dict) + obj_start_idx, obj_end_idx, object, object_entity = unpack_entity_dict(**object_dict) # entity representation @@ -145,3 +171,29 @@ def entity_representation(subject: str, object: str, sentence: str, method: str temp = temp.replace(f"", f"# ∧ {object_entity.lower()} ∧") return temp + + +def representation( + subject: str, object: str, sentence: str, entity_method: str = None, translation_methods: list = [None] +) -> str: + """ + Args: + subject (str): subject dictionary + object (str): object dictionary + sentence (str): single sentence + entity_method (str, optional): entity representation. Defaults to None. + translation_methods (list, optional): translation methods: (None, chinese) + + Returns: + str: single sentence + """ + + subject_dict = extraction(subject) + object_dict = extraction(object) + + tmp = entity_representation(subject_dict, object_dict, sentence, method=entity_method) + + for translation_method in translation_methods: + tmp = translation(tmp, method=translation_method) + + return tmp diff --git a/tests/test_representation.py b/tests/test_representation.py new file mode 100644 index 0000000..295cb78 --- /dev/null +++ b/tests/test_representation.py @@ -0,0 +1,64 @@ +import unittest + +from src.utils.representation import representation + +test_objects = [ + [ + "〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "{'word': '비틀즈', 'start_idx': 24, 'end_idx': 26, 'type': 'ORG'}", + "{'word': '조지 해리슨', 'start_idx': 13, 'end_idx': 18, 'type': 'PER'}", + ], + [ + "K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", + "{'word': '광주FC', 'start_idx': 21, 'end_idx': 24, 'type': 'ORG'}", + "{'word': '한국프로축구연맹', 'start_idx': 34, 'end_idx': 41, 'type': 'ORG'}", + ], + [ + "균일가 생활용품점 (주)아성다이소(대표 박정부)는 코로나19 바이러스로 어려움을 겪고 있는 대구광역시에 행복박스를 전달했다고 10일 밝혔다.", + "{'word': '아성다이소', 'start_idx': 13, 'end_idx': 17, 'type': 'ORG'}", + "{'word': '박정부', 'start_idx': 22, 'end_idx': 24, 'type': 'PER'}", + ], + [ + "백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + "{'word': '백한성', 'start_idx': 0, 'end_idx': 2, 'type': 'PER'}", + "{'word': '조선 충청도 공주', 'start_idx': 28, 'end_idx': 36, 'type': 'LOC'}", + ], + [ + "KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", + "{'word': 'KBS 전주방송총국', 'start_idx': 0, 'end_idx': 9, 'type': 'ORG'}", + "{'word': 'KBS 全州放送總局', 'start_idx': 11, 'end_idx': 20, 'type': 'ORG'}", + ], +] + +none_answers = [ + "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", + "아성다이소 [SEP]박정부 [SEP] 균일가 생활용품점 (주)아성다이소(대표 박정부)는 코로나19 바이러스로 어려움을 겪고 있는 대구광역시에 행복박스를 전달했다고 10일 밝혔다.", + "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + "KBS 전주방송총국 [SEP]KBS 全州放送總局 [SEP] KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", +] + + +chinese_answers = [ + "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", + "아성다이소 [SEP]박정부 [SEP] 균일가 생활용품점 (주)아성다이소(대표 박정부)는 코로나19 바이러스로 어려움을 겪고 있는 대구광역시에 행복박스를 전달했다고 10일 밝혔다.", + "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(백한성, 수원학인, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + "KBS 전주방송총국 [SEP]KBS 전주방송총국 [SEP] KBS 전주방송총국(KBS 전주방송총국)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", +] + + +class RepresentationTester(unittest.TestCase): + def test_none(self): + for example_object, answer in zip(test_objects, none_answers): + sentence, subject, object = example_object + generate_text = representation(subject, object, sentence, entity_method=None) + self.assertEqual(generate_text, answer) + + def test_chinese(self): + for example_object, answer in zip(test_objects, chinese_answers): + sentence, subject, object = example_object + generate_text = representation( + subject, object, sentence, entity_method=None, translation_methods=["chinese"] + ) + self.assertEqual(generate_text, answer) diff --git a/tests/test_translation.py b/tests/test_translation.py new file mode 100644 index 0000000..1fbfb70 --- /dev/null +++ b/tests/test_translation.py @@ -0,0 +1,31 @@ +import unittest + +from src.utils.representation import translation + +test_sentences = [ + "백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + '1904년 7월 1일, ""툰-운트 스포트버라인 바이어 04 레버쿠젠"" (Turn- und Spielverein Bayer 04 Leverkusen)의 이름으로 창단되었다.', + "헌강왕(憲康王, ~ 886년, 재위: 875년 ~ 886년)은 신라의 제49대 왕이다.", + "쇼니 씨(少弐氏)의 8대 당주로 쇼니 요리히사(少弐頼尚)의 둘째 아들이다.", + "버턴 릭터(Burton Richter, 1931년 3월 22일 ~ 2018년 7월 18일)는 노벨 물리학상을 받은 미국의 물리학자이다.", + "유한굉(劉漢宏, Liu Hanhong, ~ 887년)은 중국 당나라 말기에 활약했던 군벌로, 당초에는 당나라에 반기를 들었으나, 후에 당나라의 관직을 받고 의승군 절도사(義勝軍節度使, 본거지는 지금의 저장 성 사오싱 시)로서 절강 동부 일대를 지배하였다.", +] + +chiense_sentences = [ + "백한성(백한성, 수원학인, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + '1904년 7월 1일, ""툰-운트 스포트버라인 바이어 04 레버쿠젠"" (Turn- und Spielverein Bayer 04 Leverkusen)의 이름으로 창단되었다.', + "헌강왕(헌강왕, ~ 886년, 재위: 875년 ~ 886년)은 신라의 제49대 왕이다.", + "쇼니 씨(소이씨)의 8대 당주로 쇼니 요리히사(소이뢰상)의 둘째 아들이다.", + "버턴 릭터(Burton Richter, 1931년 3월 22일 ~ 2018년 7월 18일)는 노벨 물리학상을 받은 미국의 물리학자이다.", + "유한굉(유한굉, Liu Hanhong, ~ 887년)은 중국 당나라 말기에 활약했던 군벌로, 당초에는 당나라에 반기를 들었으나, 후에 당나라의 관직을 받고 의승군 절도사(의승군절도사, 본거지는 지금의 저장 성 사오싱 시)로서 절강 동부 일대를 지배하였다.", +] + + +class TranlsationTester(unittest.TestCase): + def test_run(self): + pass + + def test_chinese(self): + for sentence, answer in zip(test_sentences, chiense_sentences): + translation_sentence = translation(sentence, method="chinese") + self.assertEqual(translation_sentence, answer) From eb06b218f0913419f4be8bae799b200aa87f2661 Mon Sep 17 00:00:00 2001 From: FacerAin Date: Wed, 23 Nov 2022 02:11:45 +0000 Subject: [PATCH 2/8] feat: add symbol replace --- src/data_loader/data_loader.py | 2 +- src/utils/arguments.py | 2 +- src/utils/preprocess.py | 17 +++++++++++++++++ src/utils/representation.py | 13 ++++++++++++- tests/test_preprocess.py | 26 ++++++++++++++------------ tests/test_representation.py | 22 +++++++++++++++------- 6 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 src/utils/preprocess.py diff --git a/src/data_loader/data_loader.py b/src/data_loader/data_loader.py index 9d76b82..f955543 100644 --- a/src/data_loader/data_loader.py +++ b/src/data_loader/data_loader.py @@ -38,7 +38,7 @@ def tokenized_dataset(self, dataset, tokenizer): """tokenizer에 따라 sentence를 tokenizing 합니다.""" concat_entity = [] for e01, e02, sentence in zip(dataset["subject_entity"], dataset["object_entity"], dataset["sentence"]): - temp = representation(e01, e02, sentence, entity_method=None, translation_methods=["chinese"]) + temp = representation(e01, e02, sentence, entity_method=None, is_replace=False, translation_methods=[None]) concat_entity.append(temp) tokenized_sentences = tokenizer( concat_entity, diff --git a/src/utils/arguments.py b/src/utils/arguments.py index d847d1c..0aca19e 100644 --- a/src/utils/arguments.py +++ b/src/utils/arguments.py @@ -10,7 +10,7 @@ def get_training_args( output_dir="./results", save_total_limit=5, save_strategy="epoch", - num_train_epochs=1, + num_train_epochs=20, learning_rate=5e-5, per_device_train_batch_size=128, per_device_eval_batch_size=128, diff --git a/src/utils/preprocess.py b/src/utils/preprocess.py new file mode 100644 index 0000000..6017731 --- /dev/null +++ b/src/utils/preprocess.py @@ -0,0 +1,17 @@ +import re + +symbol_map = {"\"`'‘‘’“”'ˈ′": "'"} + + +def remove_symbol(): + pass + + +def replace_symbol(sentence): + for key, value in symbol_map.items(): + sentence = re.sub(f"[{key}]", value, sentence) + return sentence + + +def remove_language(): + pass diff --git a/src/utils/representation.py b/src/utils/representation.py index 3be8bdd..307ee61 100644 --- a/src/utils/representation.py +++ b/src/utils/representation.py @@ -2,6 +2,8 @@ import hanja +from . import replace_symbol + def translation(sentence: str, method: str = None) -> str: assert method in [ @@ -174,7 +176,12 @@ def entity_representation(subject_dict: dict, object_dict: dict, sentence: str, def representation( - subject: str, object: str, sentence: str, entity_method: str = None, translation_methods: list = [None] + subject: str, + object: str, + sentence: str, + entity_method: str = None, + translation_methods: list = [None], + is_replace=False, ) -> str: """ Args: @@ -183,6 +190,7 @@ def representation( sentence (str): single sentence entity_method (str, optional): entity representation. Defaults to None. translation_methods (list, optional): translation methods: (None, chinese) + is_replace (bool, optional) replace symbol methods. Defaults to False.(True, False) Returns: str: single sentence @@ -196,4 +204,7 @@ def representation( for translation_method in translation_methods: tmp = translation(tmp, method=translation_method) + if is_replace: + tmp = replace_symbol(tmp) + return tmp diff --git a/tests/test_preprocess.py b/tests/test_preprocess.py index 3984006..4f35bcc 100644 --- a/tests/test_preprocess.py +++ b/tests/test_preprocess.py @@ -1,18 +1,20 @@ import unittest +from src.utils.preprocess import replace_symbol -class PreprocessTester(unittest.TestCase): - def test_run(self): - pass - - def test_chinese(self): - pass +test_sentences = [ + "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + '““비"틀‘‘즈 [SEPˈ]조지 해'리ˈ"슨 [SEP] ’〈Some′thin‘‘g〉는'' 조지 해ˈ′리“슨”이 ’쓰ˈ고 ''비"틀′즈가 1969년“ 앨범 《′Abbey Road》에 ”ˈ담은 "노래다.', +] - def test_japanese(self): - pass +test_answers = [ + "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "''비'틀''즈 [SEP']조지 해'리''슨 [SEP] '〈Some'thin''g〉는'' 조지 해''리'슨'이 '쓰'고 ''비'틀'즈가 1969년' 앨범 《'Abbey Road》에 ''담은 '노래다.", +] - def test_etc_language(self): - pass - def test_korean(self): - pass +class PreprocessTester(unittest.TestCase): + def test_replace_symbol(self): + for sentence, answer in zip(test_sentences, test_answers): + generate_sentence = replace_symbol(sentence) + self.assertEqual(generate_sentence, answer) diff --git a/tests/test_representation.py b/tests/test_representation.py index 295cb78..6fa21a3 100644 --- a/tests/test_representation.py +++ b/tests/test_representation.py @@ -13,11 +13,6 @@ "{'word': '광주FC', 'start_idx': 21, 'end_idx': 24, 'type': 'ORG'}", "{'word': '한국프로축구연맹', 'start_idx': 34, 'end_idx': 41, 'type': 'ORG'}", ], - [ - "균일가 생활용품점 (주)아성다이소(대표 박정부)는 코로나19 바이러스로 어려움을 겪고 있는 대구광역시에 행복박스를 전달했다고 10일 밝혔다.", - "{'word': '아성다이소', 'start_idx': 13, 'end_idx': 17, 'type': 'ORG'}", - "{'word': '박정부', 'start_idx': 22, 'end_idx': 24, 'type': 'PER'}", - ], [ "백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", "{'word': '백한성', 'start_idx': 0, 'end_idx': 2, 'type': 'PER'}", @@ -33,7 +28,6 @@ none_answers = [ "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", - "아성다이소 [SEP]박정부 [SEP] 균일가 생활용품점 (주)아성다이소(대표 박정부)는 코로나19 바이러스로 어려움을 겪고 있는 대구광역시에 행복박스를 전달했다고 10일 밝혔다.", "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", "KBS 전주방송총국 [SEP]KBS 全州放送總局 [SEP] KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", ] @@ -42,11 +36,17 @@ chinese_answers = [ "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", - "아성다이소 [SEP]박정부 [SEP] 균일가 생활용품점 (주)아성다이소(대표 박정부)는 코로나19 바이러스로 어려움을 겪고 있는 대구광역시에 행복박스를 전달했다고 10일 밝혔다.", "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(백한성, 수원학인, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", "KBS 전주방송총국 [SEP]KBS 전주방송총국 [SEP] KBS 전주방송총국(KBS 전주방송총국)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", ] +replace_symbole_answers = [ + "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 '풀 스타디움상'과 '플러스 스타디움상'을 수상했다.", + "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + "KBS 전주방송총국 [SEP]KBS 全州放送總局 [SEP] KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", +] + class RepresentationTester(unittest.TestCase): def test_none(self): @@ -62,3 +62,11 @@ def test_chinese(self): subject, object, sentence, entity_method=None, translation_methods=["chinese"] ) self.assertEqual(generate_text, answer) + + def test_replace(self): + for example_object, answer in zip(test_objects, replace_symbole_answers): + sentence, subject, object = example_object + generate_text = representation( + subject, object, sentence, entity_method=None, translation_methods=[None], is_replace=True + ) + self.assertEqual(generate_text, answer) From 858babd5564e10c4432e2c8ff4a7f87ad3095309 Mon Sep 17 00:00:00 2001 From: FacerAin Date: Wed, 23 Nov 2022 02:18:07 +0000 Subject: [PATCH 3/8] fix: update requirements.txt to test * add numpy --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 20c55fb..5dd589f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,3 +9,4 @@ streamlit==1.14.1 seaborn==0.12.0 ipykernel==6.17.1 hanja==0.13.3 +numpy==1.19.2 \ No newline at end of file From 2d95b7d4680c3c7425c5c9a3b7f12cd32a2e3e40 Mon Sep 17 00:00:00 2001 From: FacerAin Date: Wed, 23 Nov 2022 06:15:09 +0000 Subject: [PATCH 4/8] chore: update check-code workflow for update pip dependecies --- .github/workflows/check-code.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/check-code.yml b/.github/workflows/check-code.yml index ed049ba..f8d37a4 100644 --- a/.github/workflows/check-code.yml +++ b/.github/workflows/check-code.yml @@ -25,6 +25,9 @@ jobs: ${{ runner.os }}-pip- ${{ runner.os }}- - name: Install dependencies + uses: py-actions/py-dependency-install@v4 + with: + path: "requirements.txt" run: | python3 -m pip install --upgrade pip - name: Check Test From 6d18ca22d326b9a0bd080bf1b0e2545165b02742 Mon Sep 17 00:00:00 2001 From: FacerAin Date: Wed, 23 Nov 2022 06:18:24 +0000 Subject: [PATCH 5/8] chore: fix check-code workflow for pip dependecies --- .github/workflows/check-code.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/check-code.yml b/.github/workflows/check-code.yml index f8d37a4..300f93d 100644 --- a/.github/workflows/check-code.yml +++ b/.github/workflows/check-code.yml @@ -28,8 +28,6 @@ jobs: uses: py-actions/py-dependency-install@v4 with: path: "requirements.txt" - run: | - python3 -m pip install --upgrade pip - name: Check Test run: | make test From 459a0f3a145e1ce484d1fc1dabfd4291d415c851 Mon Sep 17 00:00:00 2001 From: FacerAin Date: Wed, 23 Nov 2022 06:24:27 +0000 Subject: [PATCH 6/8] chore: remove check-code workflow --- .github/workflows/check-code.yml | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/.github/workflows/check-code.yml b/.github/workflows/check-code.yml index 300f93d..e558e3d 100644 --- a/.github/workflows/check-code.yml +++ b/.github/workflows/check-code.yml @@ -2,36 +2,6 @@ name: check-code on: [pull_request] -jobs: - check-test: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - name: Set up Python 3.8 - uses: actions/setup-python@v2 - with: - python-version: 3.8 - - - name: Cache pip - uses: actions/cache@v2 - with: - # This path is specific to Ubuntu - path: ~/.cache/pip - # Look to see if there is a cache hit for the corresponding requirements file - key: ${{ runner.os }}-pip-${{ hashFiles('requirements-dev.txt') }} - restore-keys: | - ${{ runner.os }}-pip- - ${{ runner.os }}- - - name: Install dependencies - uses: py-actions/py-dependency-install@v4 - with: - path: "requirements.txt" - - name: Check Test - run: | - make test - check-lint: runs-on: ubuntu-latest From c83f9199136c78815122f2a2fd1d1c65cf33df1d Mon Sep 17 00:00:00 2001 From: FacerAin Date: Wed, 23 Nov 2022 06:26:03 +0000 Subject: [PATCH 7/8] chore: remove check-code workflow --- .github/workflows/check-code.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/check-code.yml b/.github/workflows/check-code.yml index e558e3d..eb8f789 100644 --- a/.github/workflows/check-code.yml +++ b/.github/workflows/check-code.yml @@ -2,6 +2,8 @@ name: check-code on: [pull_request] +jobs: + check-lint: runs-on: ubuntu-latest From e579f8baa1c2b65fbb6a2d4db48a0e059c358362 Mon Sep 17 00:00:00 2001 From: FacerAin Date: Wed, 23 Nov 2022 06:44:45 +0000 Subject: [PATCH 8/8] fix: entity representation none sep blank --- setup.cfg | 2 +- src/utils/representation.py | 2 +- tests/test_preprocess.py | 23 ++++++++++++++++------- tests/test_representation.py | 26 +++++++++++++------------- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/setup.cfg b/setup.cfg index 1377c3d..c7331b9 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [flake8] -extend-ignore = E203, W503, E501, E231, E402, E731, E741, F401 +extend-ignore = E203, W503, E501, E231, E402, E731, E741, F401, W605 max-line-length = 120 [tool:pytest] diff --git a/src/utils/representation.py b/src/utils/representation.py index 307ee61..4d807f3 100644 --- a/src/utils/representation.py +++ b/src/utils/representation.py @@ -75,7 +75,7 @@ def entity_representation(subject_dict: dict, object_dict: dict, sentence: str, # baseline code if method is None: - temp = subject + " [SEP]" + object + " [SEP] " + sentence + temp = subject + " [SEP] " + object + " [SEP] " + sentence # entity mask elif method == "entity_mask": diff --git a/tests/test_preprocess.py b/tests/test_preprocess.py index 4f35bcc..b82e840 100644 --- a/tests/test_preprocess.py +++ b/tests/test_preprocess.py @@ -2,19 +2,28 @@ from src.utils.preprocess import replace_symbol +test_quote_symbol_map = {"\"`'‘‘’“”'ˈ′": "'"} +test_bracket_symbol_map = {"[[": "<", "]]": ">", "\[《〈「˹「⟪≪<⌜『«": "<", "\]》〉」˼」⟫≫>⌟»": ">", "({": "(", ")}": ")"} test_sentences = [ - "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", - '““비"틀‘‘즈 [SEPˈ]조지 해'리ˈ"슨 [SEP] ’〈Some′thin‘‘g〉는'' 조지 해ˈ′리“슨”이 ’쓰ˈ고 ''비"틀′즈가 1969년“ 앨범 《′Abbey Road》에 ”ˈ담은 "노래다.', + "비틀즈 [SEP] 조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.{}", ] -test_answers = [ - "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", - "''비'틀''즈 [SEP']조지 해'리''슨 [SEP] '〈Some'thin''g〉는'' 조지 해''리'슨'이 '쓰'고 ''비'틀'즈가 1969년' 앨범 《'Abbey Road》에 ''담은 '노래다.", +test_quote_answers = [ + "비틀즈 [SEP] 조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.{}", +] + +test_bracket_answers = [ + "비틀즈 [SEP] 조지 해리슨 [SEP] 는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 에 담은 노래다.()", ] class PreprocessTester(unittest.TestCase): def test_replace_symbol(self): - for sentence, answer in zip(test_sentences, test_answers): - generate_sentence = replace_symbol(sentence) + for sentence, answer in zip(test_sentences, test_quote_answers): + generate_sentence = replace_symbol(sentence, test_quote_symbol_map) + self.assertEqual(generate_sentence, answer) + + def test_bracket_symbol(self): + for sentence, answer in zip(test_sentences, test_bracket_answers): + generate_sentence = replace_symbol(sentence, test_bracket_symbol_map) self.assertEqual(generate_sentence, answer) diff --git a/tests/test_representation.py b/tests/test_representation.py index 6fa21a3..a0b7736 100644 --- a/tests/test_representation.py +++ b/tests/test_representation.py @@ -26,25 +26,25 @@ ] none_answers = [ - "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", - "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", - "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", - "KBS 전주방송총국 [SEP]KBS 全州放送總局 [SEP] KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", + "비틀즈 [SEP] 조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "광주FC [SEP] 한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", + "백한성 [SEP] 조선 충청도 공주 [SEP] 백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + "KBS 전주방송총국 [SEP] KBS 全州放送總局 [SEP] KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", ] chinese_answers = [ - "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", - "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", - "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(백한성, 수원학인, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", - "KBS 전주방송총국 [SEP]KBS 전주방송총국 [SEP] KBS 전주방송총국(KBS 전주방송총국)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", + "비틀즈 [SEP] 조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "광주FC [SEP] 한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", + "백한성 [SEP] 조선 충청도 공주 [SEP] 백한성(백한성, 수원학인, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + "KBS 전주방송총국 [SEP] KBS 전주방송총국 [SEP] KBS 전주방송총국(KBS 전주방송총국)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", ] replace_symbole_answers = [ - "비틀즈 [SEP]조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", - "광주FC [SEP]한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 '풀 스타디움상'과 '플러스 스타디움상'을 수상했다.", - "백한성 [SEP]조선 충청도 공주 [SEP] 백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", - "KBS 전주방송총국 [SEP]KBS 全州放送總局 [SEP] KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", + "비틀즈 [SEP] 조지 해리슨 [SEP] 〈Something〉는 조지 해리슨이 쓰고 비틀즈가 1969년 앨범 《Abbey Road》에 담은 노래다.", + "광주FC [SEP] 한국프로축구연맹 [SEP] K리그2에서 성적 1위를 달리고 있는 광주FC는 지난 26일 한국프로축구연맹으로부터 관중 유치 성과와 마케팅 성과를 인정받아 ‘풀 스타디움상’과 ‘플러스 스타디움상’을 수상했다.", + "백한성 [SEP] 조선 충청도 공주 [SEP] 백한성(白漢成, 水原鶴人, 1899년 6월 15일 조선 충청도 공주 출생 ~ 1971년 10월 13일 대한민국 서울에서 별세.)은 대한민국의 정치가이며 법조인이다.", + "KBS 전주방송총국 [SEP] KBS 全州放送總局 [SEP] KBS 전주방송총국(KBS 全州放送總局)은 전라북도 지역을 대상으로 하는 한국방송공사의 지역 방송 총국이다.", ] @@ -67,6 +67,6 @@ def test_replace(self): for example_object, answer in zip(test_objects, replace_symbole_answers): sentence, subject, object = example_object generate_text = representation( - subject, object, sentence, entity_method=None, translation_methods=[None], is_replace=True + subject, object, sentence, entity_method=None, translation_methods=[None], is_replace=False ) self.assertEqual(generate_text, answer)