forked from facebookresearch/fairseq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move string line encoding logic from tokenizer to Dictionary (unified…
… diff). (facebookresearch#541) Summary: Pull Request resolved: facebookresearch#541 Just a combo of a stacked pair D14057943 & D14176011, Made this as a separete diff cause there seems to be some issue with porting a stacked change into github repo Differential Revision: D14251048 fbshipit-source-id: 0a47f534a69d6ab2ebe035fba40fd51748cccfb8
- Loading branch information
1 parent
bc91927
commit f296824
Showing
13 changed files
with
204 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright (c) 2017-present, Facebook, Inc. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the license found in the LICENSE file in | ||
# the root directory of this source tree. An additional grant of patent rights | ||
# can be found in the PATENTS file in the same directory. | ||
|
||
from collections import Counter | ||
import os | ||
|
||
from fairseq.tokenizer import tokenize_line | ||
|
||
|
||
def safe_readline(f): | ||
pos = f.tell() | ||
while True: | ||
try: | ||
return f.readline() | ||
except UnicodeDecodeError: | ||
pos -= 1 | ||
f.seek(pos) # search where this character begins | ||
|
||
|
||
class Binarizer: | ||
|
||
@staticmethod | ||
def binarize(filename, dict, consumer, tokenize=tokenize_line, append_eos=True, reverse_order=False, | ||
offset=0, end=-1): | ||
nseq, ntok = 0, 0 | ||
replaced = Counter() | ||
|
||
def replaced_consumer(word, idx): | ||
if idx == dict.unk_index and word != dict.unk_word: | ||
replaced.update([word]) | ||
|
||
with open(filename, 'r', encoding='utf-8') as f: | ||
f.seek(offset) | ||
# next(f) breaks f.tell(), hence readline() must be used | ||
line = safe_readline(f) | ||
while line: | ||
if end > 0 and f.tell() > end: | ||
break | ||
ids = dict.encode_line( | ||
line=line, | ||
line_tokenizer=tokenize, | ||
add_if_not_exist=False, | ||
consumer=replaced_consumer, | ||
append_eos=append_eos, | ||
reverse_order=reverse_order, | ||
) | ||
nseq += 1 | ||
ntok += len(ids) | ||
consumer(ids) | ||
line = f.readline() | ||
return {'nseq': nseq, 'nunk': sum(replaced.values()), 'ntok': ntok, 'replaced': replaced} | ||
|
||
@staticmethod | ||
def find_offsets(filename, num_chunks): | ||
with open(filename, 'r', encoding='utf-8') as f: | ||
size = os.fstat(f.fileno()).st_size | ||
chunk_size = size // num_chunks | ||
offsets = [0 for _ in range(num_chunks + 1)] | ||
for i in range(1, num_chunks): | ||
f.seek(chunk_size * i) | ||
safe_readline(f) | ||
offsets[i] = f.tell() | ||
return offsets |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.