-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
153 lines (136 loc) · 5.02 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from flask import Flask, request
from konlpy.tag import Okt
import requests
from flask_cors import CORS
import openai
import os
openai.api_key = os.getenv('OPENAI_API_KEY')
app = Flask(__name__)
CORS(app, resources={r"*": {"origins": "*"}})
okt = Okt()
def findWords(word):
res = requests.post(os.getenv('BASE_URL') + "/searchWordItems", json={
"params": {
"dicType": "syn",
"pageNum": 0,
"pageSize": 10,
"searchWord": word,
"wordGrade": 99
}})
res = res.json()
res = res["results"]["wordInfoItems"]
res = [
{
"wordId": wordInfoItem["WORD_NO"],
"name": wordInfoItem["WORD_NAME"],
"meaning": wordInfoItem["DEFINITION"],
"part": wordInfoItem["PART_SPEECH"]
}
for wordInfoItem in res if word == wordInfoItem["WORD_NAME"]
]
return res
def findSimilarWords(wordInfo):
res = requests.post(os.getenv('BASE_URL') + "/selectedWordItem", json={
"params": {
"itemId": wordInfo['wordId'],
"itemLevel": 99,
"showType": 1,
"userId": 1,
"viewDepth": 1,
"viewType": "List"
}})
res = res.json()
res = res["synFirstItems"]
res = [
{
"name": wordInfoItem["WORD_NAME"],
"level": int(wordInfoItem["WORD_LEVEL"])
}
for wordInfoItem in res
]
return res
def findCorrectWord(text, wordInfos):
res = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "원래의 문장과 그 문장에서 사용된 단어를 제공받고, 그 단어의 모든 사전적 의미 중 그 문장에서 사용된 의미를 선택해야 한다. 다음과 같은 출력형식을 가져야 한다: index"
},
{
"role": "user",
"content": "나는 이 입장을 고수하겠다., 고수하다, [차지한 물건이나 형세 따위를 굳게 지키다., 근심과 걱정으로 괴로워하다., 머리를 조아리어 존경의 뜻을 나타내다.]"
},
{
"role": "assistant",
"content": "0"
},
{
"role": "user",
"content": f"{text}, {wordInfos[0]['name']}, {[wordInfo['meaning'] for wordInfo in wordInfos]}"
},
],
temperature=0,
max_tokens=1024,
top_p=0,
frequency_penalty=0,
presence_penalty=0
)
return res['choices'][0]['message']['content']
def findNearNum(exList, values):
res = min(exList, key=lambda x:abs(x['level']-values))
return res
@app.route('/translate', methods=['GET'])
def translate():
text = request.args.get('text')
education = request.args.get('education')
field = request.args.get('field')
res = okt.pos(text, stem=True)
#불용어 처리
res = [word for word in res
if word[1] != 'Josa' and len(word[0]) > 1]
#명사 + 하다 => 동사 결합
res_copy = res[:]
for i in range(1, len(res_copy)):
print(res[i-1][0], res[i-1][1])
if (res[i-1][1] == "Noun" and res[i][0] == "하다"):
res[i-1] = [res[i-1][0]+"하다", "Verb"]
res[i] = ""
res = [word for word in res if word]
#단어 처리
wordInfos = []
for item in res:
words = findWords(item[0])
if words:
wordInfos.append(words[int(findCorrectWord(text, words))])
if len(words) == 0:
return { "translateText": text, "translateWords": None }
#유의어 검색
similarWord = []
for wordInfo in wordInfos:
similarWords = findSimilarWords(wordInfo)
if len(similarWords) == 0:
del wordInfos[wordInfos.index(wordInfo)]
continue
similarWord.append(findNearNum(similarWords, float(field)*float(education)/10))
#단어와 유의어 묶기
translateWords = []
for index, wordInfo in enumerate(wordInfos):
translateWords.append(
{
"plainWord": wordInfo['name'],
"translateWord": similarWord[index]['name'],
"translateWordLevel": similarWord[index]['level'],
"meaning": wordInfo['meaning'],
"part": wordInfo['part']
}
)
translateText = text
for translateWord in translateWords:
if translateWord['part'] == "동사":
translateText = translateText.replace(translateWord['plainWord'][:-1], translateWord['translateWord'][:-1])
else:
translateText = translateText.replace(translateWord['plainWord'], translateWord['translateWord'])
return {"translateText": translateText, "translateWords": translateWords}
if __name__ == "__main__":
app.run()