util.doctest 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. =================
  4. Utility functions
  5. =================
  6. >>> from __future__ import print_function
  7. >>> from nltk.util import *
  8. >>> from nltk.tree import Tree
  9. >>> print_string("This is a long string, therefore it should break", 25)
  10. This is a long string,
  11. therefore it should break
  12. >>> re_show("[a-z]+", "sdf123")
  13. {sdf}123
  14. >>> tree = Tree(5,
  15. ... [Tree(4, [Tree(2, [1, 3])]),
  16. ... Tree(8, [Tree(6, [7]), 9])])
  17. >>> for x in breadth_first(tree):
  18. ... if isinstance(x, int): print(x)
  19. ... else: print(x.label())
  20. 5
  21. 4
  22. 8
  23. 2
  24. 6
  25. 9
  26. 1
  27. 3
  28. 7
  29. >>> for x in breadth_first(tree, maxdepth=2):
  30. ... if isinstance(x, int): print(x)
  31. ... else: print(x.label())
  32. 5
  33. 4
  34. 8
  35. 2
  36. 6
  37. 9
  38. >>> invert_dict({1: 2})
  39. defaultdict(<... 'list'>, {2: 1})
  40. >>> invert_dict({1: [3, 4, 5]})
  41. defaultdict(<... 'list'>, {3: [1], 4: [1], 5: [1]})