File size: 2,320 Bytes
94a1be9 |
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 |
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) |