util.doctest 1011 B

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