-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnerExtraction.py
More file actions
45 lines (33 loc) · 1.18 KB
/
nerExtraction.py
File metadata and controls
45 lines (33 loc) · 1.18 KB
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
import sys
import io
import re
from kiwipiepy import Kiwi
from flask import Flask, request, jsonify
#출력 인코딩을 UTF-8로 설정
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
from gliner import GLiNER
app = Flask(__name__)
model = GLiNER.from_pretrained('urchade/gliner_multi')
@app.route('/nerExtraction', methods=['POST'])
def ner_extraction():
data = request.json
input_text = data['text']
labels = data['labels']
entities = model.predict_entities(input_text, labels)
entities_ref = {entity["label"]:[noun_extractor(entity["text"])] for entity in entities}
#정규 표현식으로 '주X일' 또는 'X일' 패턴 찾기
pattern = r"(주?\d+일)"
work_periods = re.findall(pattern, input_text)
#결과에 추가
entities_ref['WorkPeriod'] = work_periods
return jsonify(entities_ref)
def noun_extractor(text):
results = []
kiwi = Kiwi()
result = kiwi.analyze(text)
for token, pos, _, _ in result[0][0]:
if len(token) != 1 and (pos.startswith('N') or pos.startswith('SL')):
results.append(token)
return results
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)