Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessing Code #32

Merged
merged 27 commits into from
Dec 14, 2021
Merged

Preprocessing Code #32

merged 27 commits into from
Dec 14, 2021

Conversation

sangHa0411
Copy link
Contributor

전처리의 방향은 크게 2방향으로 설정을 하였습니다.

  1. 문자 제거

    • Text 및 Title에서 특정 문자들을 제외하기
    • 괄호 제거 (괄호 안에 있는 내용 포함해서 제거)
  2. 필터링

    • Title의 갯수가 너무 짧거나 너무 긴 데이터들은 학습하는데 방해가 될 수 있다고 생각해서 해당 데이터를 제거하기로 하였습니다.
  3. Unicode Block

    • 정규 표현식에서 특정 글자들을 제외하고서 전부 삭제하는 방향으로 진행했습니다.
    • 글자를 선택하는 것 보다 유니코드의 범위를 명시하는 것이 코드를 작성하는데 더 편해서 유니코드를 사용하였습니다.
    • 예시
      1.
      스크린샷 2021-12-11 오후 2 04 02
      2. 출처 URL : https://unicode-table.com/kr/blocks/cjk-unified-ideographs/
      3. code
      • 위에서 처럼 한자를 제외하고 모든 문자들을 제거하고 싶으면 re.sub('[\u4e00-\u9fff]', '', text) 라고 해주면 됩니다.
  4. 전체적인 코드

    1. preprocessor.py
   
class Preprocessor(metaclass=ABCMeta) :
    def __init__(self ) :
        # 일본어, 한국어, 한자, 기본 문자, 구두점, 문장 기호
        self.private_comp = re.compile('[\ue000-\uf8ff]')
        self.outrange_comp = re.compile('[^\u3040-\u30ff\
            \uac00-\ud7af\
            \u4e00-\u9fff\
            \u0000-\u007f\
            \u2000-\u206f\
            \u25a0-\u25ff]') 

    @abstractmethod
    def for_train(self, data) :
        pass

    @abstractmethod
    def for_test(self, data) :
        pass

    def strip(self, txt) :
        txt = re.sub('\s+' , ' ', txt) 
        return txt.strip()

    def check_data(self, data) :
        if 'text' not in data.keys() or 'title' not in data.keys() :
            raise KeyError('Wrong Data keys')

    def doc_preprocess(self, txt) :
        txt = self.private_comp.sub(' ', txt)
        txt = self.outrange_comp.sub(' ', txt)
        return txt

class DocsPreprocessor(Preprocessor) :
    def __init__(self) :
        super().__init__()
        self.bracket_comp = re.compile(r"\([^)]+\)")

    def for_train(self, data) :
        self.check_data(data)
        title = data['title']
        title = self.bracket_comp.sub(' ', title) # 괄호를 포함해서 괄호 안 내용까지 제거합니다.
        title = self.doc_preprocess(title) # Title를 전처리 합니다.
        title = self.strip(title)

        text = data['text']
        text = self.bracket_comp.sub(' ', text) # 괄호를 포함해서 괄호 안 내용까지 제거합니다.
        text = self.doc_preprocess(text) # Text를 전처리 합니다.
        text = self.strip(text)

        data['text'] = text 
        data['title'] = title
        return data

    def for_test(self, data) :
        self.check_data(data)
        text = data['text']
        text = self.bracket_comp.sub(' ', text)
        text = self.doc_preprocess(text)
        text = self.strip(text)
        data['text'] = text 
        return data

class Filter :
    def __init__(self, min_size, max_size) :
        self.min_size = min_size
        self.max_size = max_size

    def __call__(self, data) :
        self.check_data(data)
        if len(data['title']) < self.min_size or len(data['title']) > self.max_size:
            return False
        return True

    def check_data(self, data) :
        if 'text' not in data.keys() or 'title' not in data.keys() :
            raise KeyError('Wrong Data keys')

@sangHa0411 sangHa0411 requested a review from gistarrr December 11, 2021 05:17
@sangHa0411 sangHa0411 merged commit 1e32635 into dev Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant