Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions nlputils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ def get_wiki(path,lang):
shutil.rmtree(path/'text')


def split_wiki(path,lang):
dest = path/'docs'
def split_wiki(path,lang,frac=1):
if frac > 1 or frac <= 0:
print(f"frac must be > 0 and <= 1, {frac} was given!")
return

if frac == 1:
dest = path/'docs'
else:
dest = path/f'docs_{frac}'
name = f'{lang}wiki'
if dest.exists():
print(f"{dest} already exists; not splitting")
Expand All @@ -37,15 +44,26 @@ def split_wiki(path,lang):
title_re = re.compile(rf'<doc id="\d+" url="https://{lang}.wikipedia.org/wiki\?curid=\d+" title="([^"]+)">')
lines = (path/name).open()
f=None

n_written = 0
if frac < 1:
text = (path/name).read_text()
n_files = len(list(filter(lambda title: len(title) <= 150, title_re.findall(text))))
del text
limit = n_files*frac
else:
limit = n_files

for i,l in enumerate(lines):
if i%100000 == 0: print(i)
if l.startswith('<doc id="'):
if n_written > limit:
break
title = title_re.findall(l)[0].replace('/','_')
if len(title)>150: continue
if f: f.close()
f = (dest/f'{title}.txt').open('w')
n_written+=1
else: f.write(l)
f.close()
return dest