XML 내용 중 Chart XML 내용이 없으면 0점 처리로 넘어가도록 로직 수정
This commit is contained in:
80
score5.py
80
score5.py
@@ -52,15 +52,14 @@ class XMLScorer:
|
||||
return None
|
||||
|
||||
def chart_query_xml(self, tree, xpath, namespaces):
|
||||
try:
|
||||
result = tree.xpath(xpath, namespaces=namespaces)
|
||||
if type(result) is list and len(result) == 0:
|
||||
return None
|
||||
|
||||
return result
|
||||
except ET.XPathEvalError as e:
|
||||
result = tree.xpath(xpath, namespaces=namespaces)
|
||||
if type(result) is list and len(result) == 0:
|
||||
return None
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# 유사한 텍스트 찾기
|
||||
def find_similar_text(self, root, target_text, threshold=0.5):
|
||||
"""
|
||||
@@ -109,7 +108,7 @@ class XMLScorer:
|
||||
}
|
||||
|
||||
if chart_xml is None:
|
||||
chart_tree = ET.fromstring('')
|
||||
chart_tree = ET.fromstring('<xml></xml>')
|
||||
else:
|
||||
chart_tree = ET.fromstring(chart_xml)
|
||||
|
||||
@@ -235,47 +234,42 @@ class XMLScorer:
|
||||
}
|
||||
|
||||
def binary_to_chartxml(self, xml_path):
|
||||
try:
|
||||
print(f'binary_to_chartxml {xml_path}')
|
||||
tree = ET.parse(xml_path)
|
||||
root = tree.getroot()
|
||||
|
||||
binary_data = root.xpath('//BINDATA[@Id=//BINITEM[@Format="OLE"]/@BinData]/text()')
|
||||
if not binary_data:
|
||||
raise ValueError("No binary data found in the XML.")
|
||||
binary_data = binary_data[0].encode('utf-8')
|
||||
print(f'binary_to_chartxml {xml_path}')
|
||||
tree = ET.parse(xml_path)
|
||||
root = tree.getroot()
|
||||
|
||||
# <BINDATA ...> 태그와 그 내부 내용을 삭제합니다.
|
||||
encoded_data = re.sub(b'<BINDATA.*?>', b'', binary_data)
|
||||
encoded_data = encoded_data.replace(b'</BINDATA>', b'')
|
||||
encoded_data = encoded_data.replace(b'\r\n', b'')
|
||||
binary_data = root.xpath('//BINDATA[@Id=//BINITEM[@Format="OLE"]/@BinData]/text()')
|
||||
if not binary_data:
|
||||
return None
|
||||
binary_data = binary_data[0].encode('utf-8')
|
||||
|
||||
# base64 디코딩을 수행합니다.
|
||||
decoded_data = base64.b64decode(encoded_data+b'==')
|
||||
# <BINDATA ...> 태그와 그 내부 내용을 삭제합니다.
|
||||
encoded_data = re.sub(b'<BINDATA.*?>', b'', binary_data)
|
||||
encoded_data = encoded_data.replace(b'</BINDATA>', b'')
|
||||
encoded_data = encoded_data.replace(b'\r\n', b'')
|
||||
|
||||
# 디코딩된 데이터 내용 중 xml 형식만 추출할 때 <c:chartSpace>, </c:chartSpace> 사이의 데이터만 추출.
|
||||
start = decoded_data.find(b'<?xml')
|
||||
print(start)
|
||||
end = decoded_data.find(b'</c:chartSpace>')
|
||||
print(end)
|
||||
xml_data = decoded_data[start:end+len(b'</c:chartSpace>')]
|
||||
# base64 디코딩을 수행합니다.
|
||||
decoded_data = base64.b64decode(encoded_data+b'==')
|
||||
|
||||
# 디코딩된 데이터를 파일로 저장합니다.
|
||||
base_filename = os.path.splitext(xml_path)[0]
|
||||
new_filename = f'{base_filename}.xml'
|
||||
with open(new_filename, 'wb') as file:
|
||||
file.write(xml_data)
|
||||
# 디코딩된 데이터 내용 중 xml 형식만 추출할 때 <c:chartSpace>, </c:chartSpace> 사이의 데이터만 추출.
|
||||
start = decoded_data.find(b'<?xml')
|
||||
print(start)
|
||||
end = decoded_data.find(b'</c:chartSpace>')
|
||||
print(end)
|
||||
xml_data = decoded_data[start:end+len(b'</c:chartSpace>')]
|
||||
|
||||
return xml_data
|
||||
if -1 in [start, end]:
|
||||
return None
|
||||
|
||||
# 디코딩된 데이터를 파일로 저장합니다.
|
||||
base_filename = os.path.splitext(xml_path)[0]
|
||||
new_filename = f'{base_filename}.xml'
|
||||
with open(new_filename, 'wb') as file:
|
||||
file.write(xml_data)
|
||||
|
||||
return xml_data
|
||||
|
||||
except ET.ParseError as e:
|
||||
print(f"XML 파싱 오류: {str(e)}")
|
||||
except IndexError as e:
|
||||
print(f"IndexError: {str(e)}")
|
||||
except ValueError as e:
|
||||
print(f"ValueError: {str(e)}")
|
||||
except Exception as e:
|
||||
print(f"Unexpected error: {str(e)}")
|
||||
|
||||
# XML 파일 채점
|
||||
def score_directory(self, xml_directory):
|
||||
@@ -365,10 +359,10 @@ class XMLScorer:
|
||||
|
||||
|
||||
def main():
|
||||
scoring_criteria_path = r'C:\Users\dra\project\HWP-Scoring\scoring_criteria.json'
|
||||
scoring_criteria_path = r'./scoring_criteria.json'
|
||||
|
||||
# xml(hml)파일 디렉토리 경로
|
||||
xml_directory = r'C:\Users\dra\project\HWP-Scoring\output'
|
||||
xml_directory = r'./output'
|
||||
|
||||
# 채점 클래스 초기화
|
||||
scorer = XMLScorer(scoring_criteria_path)
|
||||
|
||||
Reference in New Issue
Block a user