-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
namuwikitext #4
Comments
wikitext 형식
|
나무위키 데이터베이스의 라이센스입니다. 무상/영구적 사용이 가능합니다. wikitext 형식으로 재배포 가능합니다.
|
|
|
(WIP) 전처리 코드: 각 항목을 개별 문서로 분리import json
import os
import numpy as np
from glob import glob
from tqdm import tqdm
def split_namuwiki(namuwiki_dump_path, data_root):
with open(namuwiki_dump_path, 'rb') as f:
namuwiki = json.load(f)
print(len(namuwiki)) # 752884
def get_path(index):
suffix = '{:07}'.format(index)[-3:]
path = f'{data_root}/{suffix}/{index}.json'
return path
def check_dir(path):
dirname = os.path.abspath(os.path.dirname(path))
if not os.path.exists(dirname):
os.makedirs(dirname)
iterator = tqdm(namuwiki, desc='split namuwiki')
for index, item in enumerate(iterator):
path = get_path(index)
check_dir(path)
with open(path, 'w', encoding='utf-8') as f:
json.dump(item, f, ensure_ascii=False, indent=2, sort_keys=True)
def to_wikitext(data_root, wikitext_path, num_samples, random_seed):
paths = sorted(glob(f'{data_root}/*/*.json'))
n = len(paths)
np.random.seed(random_seed)
indices = np.random.permutation(n)[:num_samples]
paths = [paths[i] for i in indices]
raise NotImplementedError('text parser 가 필요합니다.')
namuwiki_dump_path = 'namuwiki200302.json'
data_root = 'path/to/namuwiki/data/' |
class NamuwikitextData(KorpusData):
titles : List[str]
def __init__(self, texts, titles):
if len(texts) != len(titles):
raise ValueError('`texts` and `titles` must be same length')
self.texts = texts # including '\n' in a text
self.titles = titles |
전처리 코드: JSON 에서 wikitext 형식으로 데이터 가져오기import re
def split(s, begin_marker, end_marker):
b = s.index(begin_marker)
e = s.index(end_marker, b)
prefix = s[:b]
suffix = s[e + len(end_marker):]
sub = s[b + len(begin_marker) : e]
return prefix, sub, suffix
def detach_links(s):
while '[[' in s and ']]' in s:
prefix, sub, suffix = split(s, '[[', ']]')
if sub[:2] == '파일':
sub = ''
if '|' in sub:
sub = sub.split('|', 1)[-1]
s = f'{prefix}{sub}{suffix}'
return s
def detach_markers(s, begin_marker, end_marker):
while (begin_marker in s) and (end_marker in s):
prefix, sub, suffix = split(s, begin_marker, end_marker)
s = f'{prefix}{sub}{suffix}'
return s
def space_markers(s, begin_marker, end_marker):
s = s.replace(begin_marker, f' {begin_marker} ')
s = s.replace(end_marker, f' {end_marker} ')
return s
def space_header(s):
s = s.strip()
if not s:
return s
if s[0] == '=' and s[-1] == '=':
s = f"\n{s.replace('=', ' =')}\n"
return s
def normalize(s):
s = detach_links(s)
s = detach_markers(s, '{{{', '}}}')
s = detach_markers(s, "'''", "'''")
s = detach_markers(s, '[*', ']')
s = detach_markers(s, '~~', '~~')
s = space_markers(s, '(', ')')
s = space_header(s)
return s
def get_wikitext_from(json_data):
title = json_data['title']
text = json_data['text']
if '==' not in text:
return ''
text = text[text.index('=='):]
text = '\n'.join(normalize(line) for line in text.split('\n'))
wikitext = f' = {title} =\n\n{text}'
return wikitext (sample)
(wikitext)
|
현재 작업중인 namuwikitext 의 샘플입니다. @ratsgo @hungry-wook |
몇 가지 수정된 namuwikitext 샘플입니다. 제거하다 남았던 markup language 부분들을 제거했습니다. @ratsgo @hungry-wook |
아래의 repository 에 namuwikitext corpus 를 오픈했습니다. https://github.com/lovit/namuwikitext Korpora 에서 사용할 수 있도록 개발이 완료되면 Korpora 연동 이슈를 마무리할 예정입니다. |
@ratsgo Github release 에 파일을 올릴 수가 있습니다. 이를 통하여 namuwikitext repo 에서는 aws 에서 github release download 로 링크를 수정하였습니다. Korpora 는 내일 작업하겠습니다. 관련 커밋: https://github.com/lovit/namuwikitext/blob/master/namuwikitext/utils.py#L24 |
The text was updated successfully, but these errors were encountered: