[NLP] 챗봇 프로젝트 구조 : Chatbot Project Structure

KingU·2022년 6월 2일
0

NLP

목록 보기
11/14
post-thumbnail

챗봇 프로젝트 구조 : Chatbot Project Structure





chatbot

  • train_tools: 챗봇 학습툴 관련 파일
  • models: 챗봇 엔진에서 사용하는 모델 관련 파일
    • intent: 의도 분류 모델 관련 파일
    • ner: 개체 인식 모델 관련 파일
  • utils: 챗봇 개발에 필요한 유틸리티 라이브러리
  • config: 챗봇 개발에 필요한 설정
  • test: 챗봇 개발에 필요한 테스트 코드





데이터베이스 설계 및 테이블 생성


칼럼속성설명
idint primary key not null학습 데이터 id
intentvarchar(45)의도명, 의도가 없는 경우 null
nervarchar(45)개체명, 개체명이 없는 경우 null
querytext null질문 텍스트
answertext not null답변 텍스트
answer_imagevarchar(2048)답변에 들어갈 이미지 URL,
이미지 URL 을 사용하지 않을 경우 null



import pymysql

db = None
try:
    db = pymysql.connect(
        host="127.0.0.1",
        user="homestead",
        passwd="12test34",
        db="homestead",
        charset='utf8'
    )
    
    # 테이블 생성 sql 정의
    sql='''
        CREATE TABLE IF NOT EXISTS `chatbot_train_data`(
        `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
        `intent` VARCHAR(45) NULL, 
        `ner` VARCHAR(1024) NULL,
        `query` TEXT NULL,
        `answer` TEXT NOT NULL,
        `answer_image` VARCHAR(2048) NULL,
        PRIMARY KEY(`id`))
    ENGINE = InnoDB DEFAULT CHARSET=utf8
    '''
    
    # 테이블 생성
    with db.cursor() as cursor:
        cursor.execute(sql)
    
except Exception as e:
    print(e)
    
finally:
    if db is not None:
        db.close()





챗봇 학습 데이터 엑셀 파일 및 DB 연동


칼럼속성
의도(Intent)질문의 의도를 나타내는 텍스트, 의도가 없는 경우 NULL
개체명 인식(NER)질문에 필요한 개체명, 개체명이 없는 경우 NULL
질문(Query)질문 텍스트
답변(Answer)답변 택스트
답변 이미지답변에 들어갈 이미지 URL, 이미지 URL이 없는 경우 NULL



# 필요한 모듈 임포트
import pymysql
import openpyxl

# 학습 데이터 초기화
def all_clear_train_data(db):
    # 기존 학습 데이터 삭제
    sql='''
        delete from chatbot_train_data
    '''
    with db.cursor() as cursor:
        cursor.execute(sql)
        
    # auto increment 초기화
    sql='''
        ALTER TABLE chatbot_train_data AUTO_INCREMENT=1
    '''
    with db.cursor() as cursor:
        cursor.execute(sql)
        
 # db에 데이터 저장
def insert_data(db, xls_row):
    intent, ner, query, answer, answer_img_url = xls_row
    
    sql='''
        INSERT chatbot_train_data(intent, ner, query, answer, answer_img_url)
        values(
            '%s', '%s', '%s', '%s', '%s'
        )
    ''' % (intent.value, ner.value, query.value, answer.value, answer_img_url.value)
    
    # 엑셀에서 불러온 cell에 데이터가 없는 경우 null로 치환
    sql = sql.replace("'None'", "null")
    
    with db.cursor() as cursor:
        cursor.execute(sql)
        print('{} 저장'.format(query.value))
        db.commit()
    
# 학습 엑셀 파일 선언
train_file = './train_data.xlsx'

db = None
try:
    db = pymysql.connect(
        host="127.0.0.1",
        user="homestead",
        passwd="12test34",
        db="homestead",
        charset='utf8'
    )

    # 기존 학습 데이터 초기화
    all_clear_train_data(db)

    # 학습 엑셀 파일 불러오기
    wb = openpyxl.load_workbook(train_file)
    sheet = wb['Sheet1']
    for row in sheet.iter_rows(min_row=2):  # 헤더는 불러오지 않음
        # 데이터 저장
        insert_data(db, row)

    wb.close()

except Exception as e:
    print(e)

finally:
    if db is not None:
        db.close()






당신의 시간이 헛되지 않는 글이 되겠습니다.
I'll write something that won't waste your time.

profile
원하는 것을 창조하고 창조한 것을 의미있게 사용하자

0개의 댓글