Browse Source

* 给vocabulary添加遍历方法

tags/v0.4.10
FengZiYjun 5 years ago
parent
commit
6a498bbdf2
2 changed files with 12 additions and 0 deletions
  1. +3
    -0
      fastNLP/core/vocabulary.py
  2. +9
    -0
      test/core/test_vocabulary.py

+ 3
- 0
fastNLP/core/vocabulary.py View File

@@ -199,3 +199,6 @@ class Vocabulary(object):


def __repr__(self): def __repr__(self):
return "Vocabulary({}...)".format(list(self.word_count.keys())[:5]) return "Vocabulary({}...)".format(list(self.word_count.keys())[:5])

def __iter__(self):
return iter(list(self.word_count.keys()))

+ 9
- 0
test/core/test_vocabulary.py View File

@@ -60,6 +60,15 @@ class TestIndexing(unittest.TestCase):
vocab.update(text) vocab.update(text)
self.assertEqual(text, [vocab.to_word(idx) for idx in [vocab[w] for w in text]]) self.assertEqual(text, [vocab.to_word(idx) for idx in [vocab[w] for w in text]])


def test_iteration(self):
vocab = Vocabulary()
text = ["FastNLP", "works", "well", "in", "most", "cases", "and", "scales", "well", "in",
"works", "well", "in", "most", "cases", "scales", "well"]
vocab.update(text)
text = set(text)
for word in vocab:
self.assertTrue(word in text)



class TestOther(unittest.TestCase): class TestOther(unittest.TestCase):
def test_additional_update(self): def test_additional_update(self):


Loading…
Cancel
Save