From eecc6ca756e6a0a8a5a3e1949ae615c0ed505f1d Mon Sep 17 00:00:00 2001 From: waterdrw Date: Fri, 17 Jan 2025 18:25:16 +0900 Subject: [PATCH] Implement detailed comparison of input text and original text using difflib for spell checking --- spellCheck.py | 67 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/spellCheck.py b/spellCheck.py index adb31bd..255fa20 100644 --- a/spellCheck.py +++ b/spellCheck.py @@ -1,28 +1,63 @@ from hanspell import spell_checker from lxml import etree as ET +import difflib + # sent = ['서울시 용일동 보건소에서 주최하는 ','즐거운 컬러푸드 영양교실', '은 제8회째 진행하는 행사입니다. 조화로운 식생활과 건강한 삶을 유지하도록 음식 재료의 빛깔만큼이나 다양한 효능이 그 속에 담겨져 있는 컬러푸드에 대한 올바른 영양지식을 제공하고 지역주민들이 직접 참여하여 각각의 맛과 특징을 살펴보고 골고루 먹기의 중요성을 컬러푸드 체험을 통하여 배울 수 있습니다. 이 밖에 아이들을 위한 영양 식단과 고혈압, 당뇨, 비만예방 식단 및 컬러푸드를 이용한 영양 간식 만들기 등 다양한 프로그램이 준비되어 있습니다. 아이들에게는 좋은 경험이 될 수 있는 이번 행사에 많은 참여 부탁드립니다.'] # spelled_check=spell_checker.check(sent) - -xml_path = r"/Users/waterdrw/Works/KAIT/HWP-Scoring/output/워드(한글)-010065-양야베스.hml" +xml_path_origin = r"/Users/waterdrw/Works/KAIT/HWP-Scoring/output/정답.hml" +xml_path = r"/Users/waterdrw/Works/KAIT/HWP-Scoring/output/워드(한글)-010036-구준호.hml" tree = ET.parse(xml_path) root = tree.getroot() +tree_origin = ET.parse(xml_path_origin) +root_origin = tree_origin.getroot() + # xpath로 바이너리 부분추출 input_text = root.xpath('//CHAR/text()') -# input_text = input_text.encode('utf-8') +input_text_origin = root_origin.xpath('//CHAR/text()') -# print(input_text) +# 각 요소에서 공백 제거 +input_text = [text.replace(' ', '') for text in input_text] +input_text_origin = [text.replace(' ', '') for text in input_text_origin] -spelled_check = spell_checker.check(input_text) -# Checked(result=True, original='DIAT', checked='DIAT', errors=0, words=OrderedDict([('DIAT', 0)]), time=0.5238590240478516) -# 위와 같은 형식으로 반환 -# 이때 words=OrderedDict([]) [] 안에 두번째 인덱스에 있는 값이 0이 아닌 경우에만 출력 -err_list = [] -for i in spelled_check: - for word, value in i.words.items(): - # words=OrderedDict([('과일야채', 0), ('항산화', 2), ('지수', 2), ('비교', 0)]) - # 와 같은 형식으로 반환, value에 있는 값이 0이 아닌 경우에만 출력 - if value != 0: - err_list.append(word) - print(word, value) +# 리스트를 하나의 문자열로 변경 +input_text_str = ''.join(input_text) +input_text_origin_str = ''.join(input_text_origin) + +print("input_text as string:") +print(input_text_str) +print("\ninput_text_origin as string:") +print(input_text_origin_str) + + +# 문자열의 차이를 비교 +diff = difflib.ndiff(input_text_origin_str, input_text_str) +diff_list = list(diff) + +# 차이점을 정리하여 result_diff에 저장 +result_diff = [] +skip_next = False + +for i, line in enumerate(diff_list): + if skip_next: + skip_next = False + continue + # diff_list의 line 시작이 '-'이면서 다음 line이 '+'이면 두 line을 붙여서 맞춤법이 틀린 단어로 판단 + if line.startswith('- '): + if i + 1 < len(diff_list) and diff_list[i + 1].startswith('+ '): + line = line.replace('- ', '-') + next = diff_list[i + 1].replace('+ ', '') + result_diff.append(line+'=>'+next) + skip_next = True + else: + line = line.replace('- ', '-') + result_diff.append(line) + elif line.startswith('+ '): + line = line.replace('+ ', '+') + result_diff.append(line) + +# result_diff 출력 +print("\nResult Differences:") +for diff in result_diff: + print(diff) \ No newline at end of file