전처리 과정은 기본적인 과정이다.
전처리에 따라 후처리 부분의 성능 차이가 커지므로 매우 중요한 작업이다.
전처리 과정은 형태소 분석기로
# 필요한 모듈 임포트
from konlpy.tag import Komoran
# 전처리 클래스
class Preprocess:
    def __init__(self, userdic=None): # 생성자
        # 형태소 분석기 초기화
        self.komoran = Komoran(userdic=userdic)
        
        # 제외할 품사
        # 관계언 제거, 기호 제거
        # 어미 제거
        # 접미사 제거
        self.exclusion_tags = [
            'JKS', 'JKC', 'JKG', 'JKO', 'JKB', 'JKV', 'JKQ',
            'JX', 'JC',
            'SF', 'SP', 'SS', 'SE', 'SO',
            'EP', 'EF', 'EC', 'ETN', 'ETM',
            'XSN', 'XSV', 'XSA'
        ]
    # 형태소 분석기 POS 태거
    def pos(self, sentence):
        return self.komoran.pos(sentence)
        
    # 불용어 제거 후 필요한 품사 정보만 가져오기
    def get_keywords(self, pos, without_tag=False):
        f = lambda x: x in self.exclusion_tags
        word_list = []
        for p in pos:
            if f(p[1]) is False:
                word_list.append(p if without_tag is False else p[0])
        return word_listself.komoran = Komoran(userdic=userdic)<br>클래스가 생성될 때 형태소 분석기 인스턴스 객체 생성
형태소 분석기 - komoran
userdic 인자에서 사용자 정의 사전 파일의 경로
self.exclusion_tags = [
            'JKS', 'JKC', 'JKG', 'JKO', 'JKB', 'JKV', 'JKQ',
            'JX', 'JC',
            'SF', 'SP', 'SS', 'SE', 'SO',
            'EP', 'EF', 'EC', 'ETN', 'ETM',
            'XSN', 'XSV', 'XSA'
        ]komoran 형태소 분석기 품사 정보
불용어로 정의할 품사를 리스트로 정의
불용어는 핵심 키워드에서 제외
def pos(self, sentence):
    return self.komoran.pos(sentence)komoran 형태소 분석기의 POS 태거를 호출하는 매서드
객체를 직접 호출하지 않기 위하여 매서드를 통해 접근
형태소 분석기 종류를 바꾸게 될 경우 매서드만 변경하면 되므로 유지보수에도 좋다.
def get_keywords(self, pos, without_tag=False):
    f = lambda x: x in self.exclusion_tags
    word_list = []
    for p in pos:
        if f(p[1]) is False:
            word_list.append(p if without_tag is False else p[0])
    return word_list불용어 제거 후 핵심 키워드만 추출
생성자에서 정의한 self.exclusion_tags 해당하지 않은 키워드만 저장
당신의 시간이 헛되지 않는 글이 되겠습니다.
I'll write something that won't waste your time.