찬란하게

언어판별서비스2 본문

AI (인공지능)/미니프로젝트

언어판별서비스2

체리핫 2021. 3. 26. 17:05

 

wepapp.py

#!/usr/bin/env python3
import cgi, os.path
import joblib

# 학습된 모형 정보 읽어 들이기
pklfile = os.path.dirname(__file__) + "/freq2.pkl"
clf = joblib.load(pklfile)

# 텍스트 입력 양식 출력하기
def show_form(text, msg=""):
    with open("C:/pydata/cgi-bin/style2.py", "r", encoding="utf-8") as lp:
        html = lp.read()
    print(html.format(text, msg))
# 판정하기
def detect_lang(text):
    # 알파벳 출현 빈도 구하기
    text = text.lower() 
    code_a, code_z = (ord("a"), ord("z"))
    cnt = [0 for i in range(26)]
    for ch in text:
        n = ord(ch) - code_a
        if 0 <= n < 26: cnt[n] += 1
    total = sum(cnt)
    if total == 0: return "입력이 없습니다"
    freq = list(map(lambda n: n/total, cnt))
    # 언어 예측하기
    res = clf.predict([freq])
    # 언어 코드를 한국어로 변환하기
    lang_dic = {"en":"영어","fr":"프랑스어",
        "id":"인도네시아어", "tl":"타갈로그어"}
    return lang_dic[res[0]]

# 입력 양식의 값 읽어 들이기
form = cgi.FieldStorage()
text = form.getvalue("text", default="")
msg = ""
if text != "":
    lang = detect_lang(text)
    msg = "판정 결과 : " + lang

# 입력 양식 출력
show_form(text, msg)