Skip to content

Commit 35e3550

Browse files
committed
Improve loading efficiency
1 parent 988fb70 commit 35e3550

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

supar/utils/transform.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def cache(transform, sentences):
6363
fb = os.path.join(ftemp, os.path.basename(fbin))
6464
global flattened_fields
6565
flattened_fields = self.flattened_fields
66-
binarize(sentences, fs)
66+
binarize(progress_bar(sentences), fs)
6767
sentences = debinarize(fs, meta=True)
6868
try:
6969
yield ((sentences[s:s+chunksize], ft, fs, f"{fb}.{i}")
@@ -409,7 +409,7 @@ def load(
409409
lines = (i for s in data for i in StringIO(self.toconll(s) + '\n'))
410410

411411
index, sentence = 0, []
412-
for line in progress_bar(lines):
412+
for line in lines:
413413
line = line.strip()
414414
if len(line) == 0:
415415
sentence = CoNLLSentence(self, sentence, index)
@@ -696,7 +696,7 @@ def load(
696696
data = [data] if isinstance(data[0], str) else data
697697

698698
index = 0
699-
for s in progress_bar(data):
699+
for s in data:
700700
try:
701701
tree = nltk.Tree.fromstring(s) if isinstance(s, str) else self.totree(s, self.root)
702702
sentence = TreeSentence(self, tree, index)
@@ -878,10 +878,11 @@ class TreeSentence(Sentence):
878878
def __init__(self, transform: Tree, tree: nltk.Tree, index: Optional[int] = None) -> TreeSentence:
879879
super().__init__(transform, index)
880880

881-
words, tags = zip(*tree.pos())
882-
chart = [[None]*(len(words)+1) for _ in range(len(words)+1)]
883-
for i, j, label in Tree.factorize(Tree.binarize(tree)[0]):
884-
chart[i][j] = label
881+
words, tags, chart = *zip(*tree.pos()), None
882+
if transform.training:
883+
chart = [[None]*(len(words)+1) for _ in range(len(words)+1)]
884+
for i, j, label in Tree.factorize(Tree.binarize(tree)[0]):
885+
chart[i][j] = label
885886
self.values = [words, tags, tree, chart]
886887

887888
def __repr__(self):

0 commit comments

Comments
 (0)