You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_field.py 994 B

1234567891011121314151617181920212223
  1. import unittest
  2. from fastNLP.core.field import CharTextField
  3. class TestField(unittest.TestCase):
  4. def test_case(self):
  5. text = "PhD applicants must submit a Research Plan and a resume " \
  6. "specify your class ranking written in English and a list of research" \
  7. " publications if any".split()
  8. max_word_len = max([len(w) for w in text])
  9. field = CharTextField(text, max_word_len, is_target=False)
  10. all_char = set()
  11. for word in text:
  12. all_char.update([ch for ch in word])
  13. char_vocab = {ch: idx + 1 for idx, ch in enumerate(all_char)}
  14. self.assertEqual(field.index(char_vocab),
  15. [[char_vocab[ch] for ch in word] + [0] * (max_word_len - len(word)) for word in text])
  16. self.assertEqual(field.get_length(), len(text))
  17. self.assertEqual(field.contents(), text)
  18. tensor = field.to_tensor(50)
  19. self.assertEqual(tuple(tensor.shape), (50, max_word_len))