psyche commited on
Commit
fda4f05
1 Parent(s): 885c19c
Files changed (1) hide show
  1. kowiki.py +10 -8
kowiki.py CHANGED
@@ -60,6 +60,8 @@ def replace_link(text):
60
 
61
 
62
  def process_text(text: str):
 
 
63
  text = text.strip()
64
  text = re.sub("<[^>]+>", "", text)
65
  text = re.sub("\[\[파일:[^\]]+\]\]", "", text)
@@ -143,19 +145,19 @@ class KoWiki(datasets.GeneratorBasedBuilder):
143
  ]
144
 
145
  def _generate_examples(self, data_file):
146
- n = 0
147
- output = {}
148
  if data_file.startswith(("https://", "http://")):
149
  data_file = requests.get(data_file, stream=True).raw
150
 
 
151
  for event, node in ET.iterparse(data_file):
152
- if node.tag.endswith("title"):
 
153
  output["title"] = node.text
154
- elif node.tag.endswith("text"):
155
- text = node.text
156
- text = text if isinstance(text, str) else ""
157
- output["text"] = process_text(text)
158
- if node.tag.endswith("page"):
159
  output["id"] = f"{n:08d}"
160
  yield n, output
161
  output = {}
 
60
 
61
 
62
  def process_text(text: str):
63
+ if not isinstance(text, str):
64
+ return ""
65
  text = text.strip()
66
  text = re.sub("<[^>]+>", "", text)
67
  text = re.sub("\[\[파일:[^\]]+\]\]", "", text)
 
145
  ]
146
 
147
  def _generate_examples(self, data_file):
 
 
148
  if data_file.startswith(("https://", "http://")):
149
  data_file = requests.get(data_file, stream=True).raw
150
 
151
+ n, output = 0, {}
152
  for event, node in ET.iterparse(data_file):
153
+ tag_name = node.tag
154
+ if tag_name.endswith("title"):
155
  output["title"] = node.text
156
+
157
+ elif tag_name.endswith("text"):
158
+ output["text"] = process_text(node.text)
159
+
160
+ elif tag_name.endswith("page"):
161
  output["id"] = f"{n:08d}"
162
  yield n, output
163
  output = {}