File size: 8,257 Bytes
cf5028a |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
import os
import glob
import json
import xml.etree.ElementTree as ET
from concurrent.futures import ProcessPoolExecutor
import time
# ==========================================
# 1. ์ค์
# ==========================================
INPUT_DIR = r"D:\KCI\data_kci_format"
OUTPUT_DIR = r"D:\KCI\processed"
OUTPUT_FILE = os.path.join(OUTPUT_DIR, "kci_articles.jsonl")
# ๋ค์์คํ์ด์ค (KCI, DC ๋ชจ๋ ์ง์)
NS = {
'oai': 'http://www.openarchives.org/OAI/2.0/',
'kci': 'http://www.kci.go.kr/kciportal/OAI/',
'oai_dc': 'http://www.openarchives.org/OAI/2.0/oai_dc/',
'dc': 'http://purl.org/dc/elements/1.1/'
}
# ==========================================
# 2. ํ์ฑ ๋ก์ง (ํ์ด๋ธ๋ฆฌ๋ ์ง์)
# ==========================================
def parse_single_xml(filepath):
"""
XML ํ์ผ ํ๋๋ฅผ ์ด์ด์ ๋ด๋ถ์ ๋
ผ๋ฌธ(record)๋ค์ ๋ฆฌ์คํธ๋ก ๋ณํํฉ๋๋ค.
oai_kci(์์ธ) ํฌ๋งท๊ณผ oai_dc(๊ธฐ๋ณธ) ํฌ๋งท์ ๋ชจ๋ ์ฒ๋ฆฌํฉ๋๋ค.
"""
extracted_data = []
try:
tree = ET.parse(filepath)
root = tree.getroot()
# OAI-PMH ๊ตฌ์กฐ ๋ด record ๋ฐ๋ณต
records = root.findall('.//oai:record', NS)
for record in records:
# ์ญ์ ๋ ๋ ์ฝ๋๋ ๊ฑด๋๋
header = record.find('oai:header', NS)
if header is not None and header.get('status') == 'deleted':
continue
metadata = record.find('oai:metadata', NS)
if metadata is None:
continue
# A. oai_kci ํฌ๋งท ์๋ (์์ธ ์ ๋ณด)
kci_meta = metadata.find('kci:oai_kci', NS)
if kci_meta is not None:
extracted_data.append(_parse_kci_format(kci_meta, filepath))
continue
# B. oai_dc ํฌ๋งท ์๋ (๊ธฐ๋ณธ ์ ๋ณด) - ํ์ผ ์์ ๋๋น
dc_meta = metadata.find('oai_dc:dc', NS)
if dc_meta is not None:
extracted_data.append(_parse_dc_format(dc_meta, filepath))
continue
except Exception as e:
# ํ์ผ์ด ๊นจ์ง ๊ฒฝ์ฐ ๋ก๊ทธ๋ง ๋จ๊ธฐ๊ณ ๋ฌด์
# print(f"Error parsing {filepath}: {e}")
return []
return extracted_data
def _parse_kci_format(kci_meta, filepath):
"""KCI ์ ์ฉ ์์ธ ํฌ๋งท ํ์ฑ"""
# 1. ์ ๋ ์ ๋ณด
j_info = kci_meta.find('kci:journalInfo', NS)
journal_data = {}
if j_info is not None:
journal_data = {
'journal_name': _get_text(j_info, 'kci:journal-name'),
'publisher': _get_text(j_info, 'kci:publisher-name'),
'pub_year': _get_text(j_info, 'kci:pub-year'),
'pub_month': _get_text(j_info, 'kci:pub-mon'),
'volume': _get_text(j_info, 'kci:volume'),
'issue': _get_text(j_info, 'kci:issue'),
}
# 2. ๋
ผ๋ฌธ ์ ๋ณด
a_info = kci_meta.find('kci:articleInfo', NS)
if a_info is None:
return {}
# ์ ๋ชฉ
title_group = a_info.find('kci:title-group', NS)
title_ko, title_en = "", ""
if title_group is not None:
for t in title_group.findall('kci:article-title', NS):
lang = t.get('lang')
if lang == 'original': title_ko = t.text
elif lang == 'english': title_en = t.text
# ์ ์
authors = []
author_group = a_info.find('kci:author-group', NS)
if author_group is not None:
for auth in author_group.findall('kci:author', NS):
authors.append(auth.text.strip() if auth.text else "")
# ์ด๋ก
abstract_ko, abstract_en = "", ""
ab_group = a_info.find('kci:abstract-group', NS)
if ab_group is not None:
for ab in ab_group.findall('kci:abstract', NS):
lang = ab.get('lang')
if lang == 'original': abstract_ko = ab.text
elif lang == 'english': abstract_en = ab.text
return {
'id': a_info.get('article-id'),
'format': 'oai_kci', # ํฌ๋งท ๊ตฌ๋ถ์ฉ
'title_ko': title_ko,
'title_en': title_en,
'journal': journal_data,
'authors': authors,
'abstract_ko': abstract_ko,
'abstract_en': abstract_en,
'keywords': _get_text(a_info, 'kci:keyword-group'),
'citation_count': _get_text(a_info, 'kci:citation-count'),
'doi': _get_text(a_info, 'kci:doi'),
'url': _get_text(a_info, 'kci:url'),
'file_source': os.path.basename(filepath)
}
def _parse_dc_format(dc_meta, filepath):
"""DC ๊ธฐ๋ณธ ํฌ๋งท ํ์ฑ (๊ตฌ์กฐ๊ฐ ๋จ์ํจ)"""
# ์ ๋ชฉ (์ธ์ด ๊ตฌ๋ถ ์์ฑ์ด ์์ ์๋ ์์ ์๋ ์์)
titles = dc_meta.findall('dc:title', NS)
title_ko = titles[0].text if titles else ""
title_en = titles[1].text if len(titles) > 1 else "" # ๋๋ต์ ์ถ์
# ์ ์ (๋จ์ ํ
์คํธ ๋์ด)
authors = [c.text for c in dc_meta.findall('dc:creator', NS) if c.text]
# ์ด๋ก
descriptions = dc_meta.findall('dc:description', NS)
abstracts = [d.text for d in descriptions if d.text]
abstract_ko = abstracts[0] if abstracts else ""
# ์๋ณ์ (DOI, URL ๋ฑ์ด ์์ฌ ์์)
identifiers = dc_meta.findall('dc:identifier', NS)
doi = ""
url = ""
art_id = ""
for ide in identifiers:
text = ide.text or ""
type_attr = ide.get('type')
if type_attr == 'doi' or 'doi.org' in text:
doi = text
elif type_attr == 'artId' or 'ART' in text:
art_id = text
elif 'kci.go.kr' in text and 'http' in text:
url = text
return {
'id': art_id,
'format': 'oai_dc', # ํฌ๋งท ๊ตฌ๋ถ์ฉ
'title_ko': title_ko,
'title_en': title_en,
'journal': {'journal_name': _get_text(dc_meta, 'dc:publisher')}, # DC๋ ์ ๋๋ช
์ด publisher์ ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์
'authors': authors,
'abstract_ko': abstract_ko,
'abstract_en': "", # DC์์๋ ๊ตฌ๋ถ์ด ๋ชจํธํจ
'keywords': _get_text(dc_meta, 'dc:subject'),
'citation_count': None, # DC์๋ ์์
'doi': doi,
'url': url,
'file_source': os.path.basename(filepath)
}
def _get_text(parent, tag):
if parent is None: return None
elem = parent.find(tag, NS)
return elem.text.strip() if elem is not None and elem.text else None
# ==========================================
# 3. ๋ฉ์ธ ์คํ ๋ก์ง
# ==========================================
def main():
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
xml_files = glob.glob(os.path.join(INPUT_DIR, "*.xml"))
total_files = len(xml_files)
print(f"๐ ์ด {total_files}๊ฐ์ XML ํ์ผ ์ฒ๋ฆฌ๋ฅผ ์์ํฉ๋๋ค. (ํ์ด๋ธ๋ฆฌ๋ ๋ชจ๋)")
print(f"๐ ์
๋ ฅ: {INPUT_DIR}")
print(f"๐พ ์ถ๋ ฅ: {OUTPUT_FILE}")
print("โณ ๋ณํ ์ค... (ํ์ผ์ด ๋ง์ ์๊ฐ์ด ์กฐ๊ธ ๊ฑธ๋ฆฝ๋๋ค)")
start_time = time.time()
with open(OUTPUT_FILE, 'w', encoding='utf-8') as outfile:
# ProcessPoolExecutor๋ก ๋ณ๋ ฌ ์ฒ๋ฆฌ
with ProcessPoolExecutor() as executor:
results = executor.map(parse_single_xml, xml_files)
count = 0
total_articles = 0
for result in results:
count += 1
if result:
total_articles += len(result)
for doc in result:
json.dump(doc, outfile, ensure_ascii=False)
outfile.write('\n')
if count % 2000 == 0:
print(f" [{count}/{total_files}] ์ฒ๋ฆฌ ์ค... ({total_articles} ๊ฑด ์ถ์ถ)")
print("="*50)
print(f"โ
๋ณํ ์๋ฃ!")
print(f"๐ ์ด ํ์ผ: {total_files}๊ฐ")
print(f"๐ ์ถ์ถ๋ ๋
ผ๋ฌธ: {total_articles}๊ฑด")
print(f"โฑ๏ธ ์์ ์๊ฐ: {time.time() - start_time:.1f}์ด")
print(f"๐ ์ ์ฅ ์์น: {OUTPUT_FILE}")
if __name__ == "__main__":
main() |