|
from os.path import join, dirname |
|
from underthesea.file_utils import DATASETS_FOLDER |
|
import random |
|
|
|
random.seed(10) |
|
text_file = join(DATASETS_FOLDER, "VNESES", "VNESEScorpus.txt") |
|
with open(text_file) as f: |
|
lines = f.read().splitlines() |
|
NUM_LONG_TOKENS = 50 |
|
NUM_SHORT_TOKENS = 20 |
|
NUM_MAX_TOKENS = 200 |
|
|
|
|
|
def longline_conditions(line): |
|
if len(line) < NUM_LONG_TOKENS or len(line) > NUM_MAX_TOKENS: |
|
return False |
|
if not (line[0].isupper() and line[-1] == "."): |
|
return False |
|
return True |
|
|
|
|
|
long_lines = [line for line in lines if longline_conditions(line)] |
|
print("Short lines", len(long_lines)) |
|
|
|
|
|
def shortline_conditions(line): |
|
if len(line) < NUM_SHORT_TOKENS: |
|
return False |
|
if len(line) > NUM_LONG_TOKENS: |
|
return False |
|
if not line[0].isupper(): |
|
return False |
|
return True |
|
|
|
|
|
short_lines = [line for line in lines if shortline_conditions(line)] |
|
print("Short lines", len(short_lines)) |
|
|
|
print("Long lines", len(long_lines)) |
|
print("Short lines", len(short_lines)) |
|
|
|
pwd = dirname(__file__) |
|
tmp = join(pwd, "data") |
|
corpus_file = join(tmp, "UTS_Text_v1.txt") |
|
|
|
with open(corpus_file, "w") as f: |
|
lines = long_lines + short_lines |
|
lines = lines[:1000] |
|
content = "\n".join(lines) |
|
f.write(content) |
|
|
|
with open(join(tmp, "large", "train.txt"), "w") as f: |
|
lines = long_lines + short_lines |
|
lines = lines[:100000] |
|
content = "\n".join(lines) |
|
f.write(content) |
|
|