Skip to content
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

I found some small problems #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,6 @@ ENV/
corpora
logdir
preprocessed
.idea
iwslt2016
log
4 changes: 2 additions & 2 deletions data_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def load_vocab(vocab_fpath):
Returns
two dictionaries.
'''
vocab = [line.split()[0] for line in open(vocab_fpath, 'r').read().splitlines()]
vocab = [line.split()[0] for line in open(vocab_fpath, 'r',encoding='UTF-8').read().splitlines()]
token2idx = {token: idx for idx, token in enumerate(vocab)}
idx2token = {idx: token for idx, token in enumerate(vocab)}
return token2idx, idx2token
Expand All @@ -38,7 +38,7 @@ def load_data(fpath1, fpath2, maxlen1, maxlen2):
sents2: list of target sents
'''
sents1, sents2 = [], []
with open(fpath1, 'r') as f1, open(fpath2, 'r') as f2:
with open(fpath1, 'r',encoding='UTF-8') as f1, open(fpath2, 'r',encoding='UTF-8') as f2:
for sent1, sent2 in zip(f1, f2):
if len(sent1.split()) + 1 > maxlen1: continue # 1: </s>
if len(sent2.split()) + 1 > maxlen2: continue # 1: </s>
Expand Down
30 changes: 15 additions & 15 deletions prepro.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ def prepro(hp):

logging.info("# Preprocessing")
# train
_prepro = lambda x: [line.strip() for line in open(x, 'r').read().split("\n") \
_prepro = lambda x: [line.strip() for line in open(x, 'r',encoding='UTF-8').read().split("\n") \
if not line.startswith("<")]
prepro_train1, prepro_train2 = _prepro(train1), _prepro(train2)
assert len(prepro_train1)==len(prepro_train2), "Check if train source and target files match."

# eval
_prepro = lambda x: [re.sub("<[^>]+>", "", line).strip() \
for line in open(x, 'r').read().split("\n") \
for line in open(x, 'r',encoding='UTF-8').read().split("\n") \
if line.startswith("<seg id")]
prepro_eval1, prepro_eval2 = _prepro(eval1), _prepro(eval2)
assert len(prepro_eval1) == len(prepro_eval2), "Check if eval source and target files match."
Expand All @@ -51,17 +51,17 @@ def prepro(hp):
assert len(prepro_test1) == len(prepro_test2), "Check if test source and target files match."

logging.info("Let's see how preprocessed data look like")
logging.info("prepro_train1:", prepro_train1[0])
logging.info("prepro_train2:", prepro_train2[0])
logging.info("prepro_eval1:", prepro_eval1[0])
logging.info("prepro_eval2:", prepro_eval2[0])
logging.info("prepro_test1:", prepro_test1[0])
logging.info("prepro_test2:", prepro_test2[0])
logging.info("prepro_train1:%s", prepro_train1[0])
logging.info("prepro_train2:%s", prepro_train2[0])
logging.info("prepro_eval1:%s", prepro_eval1[0])
logging.info("prepro_eval2:%s", prepro_eval2[0])
logging.info("prepro_test1:%s", prepro_test1[0])
logging.info("prepro_test2:%s", prepro_test2[0])

logging.info("# write preprocessed files to disk")
os.makedirs("iwslt2016/prepro", exist_ok=True)
def _write(sents, fname):
with open(fname, 'w') as fout:
with open(fname, 'w',encoding='UTF-8') as fout:
fout.write("\n".join(sents))

_write(prepro_train1, "iwslt2016/prepro/train.de")
Expand All @@ -86,7 +86,7 @@ def _write(sents, fname):

logging.info("# Segment")
def _segment_and_write(sents, fname):
with open(fname, "w") as fout:
with open(fname, "w",encoding='UTF-8') as fout:
for sent in sents:
pieces = sp.EncodeAsPieces(sent)
fout.write(" ".join(pieces) + "\n")
Expand All @@ -98,11 +98,11 @@ def _segment_and_write(sents, fname):
_segment_and_write(prepro_test1, "iwslt2016/segmented/test.de.bpe")

logging.info("Let's see how segmented data look like")
print("train1:", open("iwslt2016/segmented/train.de.bpe",'r').readline())
print("train2:", open("iwslt2016/segmented/train.en.bpe", 'r').readline())
print("eval1:", open("iwslt2016/segmented/eval.de.bpe", 'r').readline())
print("eval2:", open("iwslt2016/segmented/eval.en.bpe", 'r').readline())
print("test1:", open("iwslt2016/segmented/test.de.bpe", 'r').readline())
print("train1:", open("iwslt2016/segmented/train.de.bpe",'r',encoding='UTF-8').readline())
print("train2:", open("iwslt2016/segmented/train.en.bpe", 'r',encoding='UTF-8').readline())
print("eval1:", open("iwslt2016/segmented/eval.de.bpe", 'r',encoding='UTF-8').readline())
print("eval2:", open("iwslt2016/segmented/eval.en.bpe", 'r',encoding='UTF-8').readline())
print("test1:", open("iwslt2016/segmented/test.de.bpe", 'r',encoding='UTF-8').readline())

if __name__ == '__main__':
hparams = Hparams()
Expand Down