Datasets:

Modalities:
Text
Libraries:
Datasets
License:
namu / process.py
eddie14's picture
init
94a1be9
raw
history blame
2.32 kB
import re
from collections import defaultdict
def remove_double_linked_text(text):
PATTERN = "\[\[[^\[\]\|]+\|([^\]]+)\]\]"
result = re.search(PATTERN,text)
while result is not None:
s,e = result.span()
text = text[:s]+result.group(1)+text[e:]
result = re.search(PATTERN, text)
return text
def remove_linked_text(text):
PATTERN = "\[\[([^\[\]]+)\]\]"
result = re.search(PATTERN,text)
while result is not None:
s,e = result.span()
text = text[:s]+result.group(1)+text[e:]
result = re.search(PATTERN, text)
return text
def remove_attribute_in_table(text):
PATTERN = "{{{[^}]+ ([^\}]+)}}}"
result = re.search(PATTERN,text)
while result is not None:
s,e = result.span()
text = text[:s]+result.group(1)+text[e:]
result = re.search(PATTERN, text)
text = re.sub("<bgcolor=#[^>]+>", "", text)
text = re.sub("<-[0-9]>", "", text)
text = re.sub("\|\|<table[^\n]+\n", "", text)
text = re.sub("<tablewidth\=[^>]+>", "", text)
text = re.sub("<width\=[^>]+>", "", text)
text = re.sub("(?<=코멘트\-)\|\|(?=\n)", "", text)
return text
def replace_link(text):
text = re.sub("\[youtube\([^\]]+\)\]", "[YOUTUBE LINK]", text)
return text
def process_text(text: str):
text = text.strip()
text = re.sub("\[\[파일:[^\]]+\]\]", "", text)
text = remove_double_linked_text(text)
text = remove_linked_text(text)
text = re.sub("'''", "", text)
text = replace_link(text)
text = remove_attribute_in_table(text)
return text
def get_structured_data(text: str, pattern="\n\=\= ([^\=\n]+) \=\=\n", default_value=None) -> dict:
outputs = defaultdict(list)
matched = re.search(pattern, text)
is_first = True
while matched is not None:
b,s = matched.span()
if is_first:
outputs['item'].append("meta")
outputs["content"].append(text[:b])
is_first = False
outputs["item"].append(matched.group(1))
text = text[s:]
matched = re.search(pattern, text)
e = matched.start() if matched is not None else None
outputs["content"].append(text[:e].strip())
if not outputs and default_value is not None:
return default_value
return dict(outputs)