concordance.doctest 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. .. Copyright (C) 2001-2016 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ==================================
  4. Concordance Example
  5. ==================================
  6. A concordance view shows us every occurrence of a given
  7. word, together with some context. Here we look up the word monstrous
  8. in Moby Dick by entering text1 followed by a period, then the term
  9. concordance, and then placing "monstrous" in parentheses:
  10. >>> from nltk.corpus import gutenberg
  11. >>> from nltk.text import Text
  12. >>> corpus = gutenberg.words('melville-moby_dick.txt')
  13. >>> text = Text(corpus)
  14. >>> text.concordance("monstrous") # doctest:+NORMALIZE_WHITESPACE
  15. Displaying 11 of 11 matches:
  16. ong the former , one was of a most monstrous size . ... This came towards us ,
  17. ON OF THE PSALMS . " Touching that monstrous bulk of the whale or ork we have r
  18. ll over with a heathenish array of monstrous clubs and spears . Some were thick
  19. d as you gazed , and wondered what monstrous cannibal and savage could ever hav
  20. that has survived the flood ; most monstrous and most mountainous ! That Himmal
  21. they might scout at Moby Dick as a monstrous fable , or still worse and more de
  22. th of Radney .'" CHAPTER 55 Of the Monstrous Pictures of Whales . I shall ere l
  23. ing Scenes . In connexion with the monstrous pictures of whales , I am strongly
  24. ere to enter upon those still more monstrous stories of them which are to be fo
  25. ght have been rummaged out of this monstrous cabinet there is no telling . But
  26. of Whale - Bones ; for Whales of a monstrous size are oftentimes cast up dead u
  27. >>> text.concordance("monstrous") # doctest:+ELLIPSIS, +NORMALIZE_WHITESPACE
  28. Displaying 11 of 11 matches:
  29. ong the former , one was of a most monstrous size . ... This came towards us ,
  30. ON OF THE PSALMS . " Touching that monstrous bulk of the whale or ork we have r
  31. ll over with a heathenish array of monstrous clubs and spears . Some were thick
  32. ...
  33. =================================
  34. Concordance List
  35. =================================
  36. Often we need to store the results of concordance for further usage.
  37. To do so, call the concordance function with the stdout argument set
  38. to false:
  39. >>> from nltk.corpus import gutenberg
  40. >>> from nltk.text import Text
  41. >>> corpus = gutenberg.words('melville-moby_dick.txt')
  42. >>> text = Text(corpus)
  43. >>> con_list = text.concordance_list("monstrous")
  44. >>> con_list[2].line
  45. 'll over with a heathenish array of monstrous clubs and spears . Some were thick'
  46. >>> len(con_list)
  47. 11
  48. =================================
  49. Patching Issue #2088
  50. =================================
  51. Patching https://github.com/nltk/nltk/issues/2088
  52. The left slice of the left context should be clip to 0 if the `i-context` < 0.
  53. >>> from nltk import Text, word_tokenize
  54. >>> jane_eyre = 'Chapter 1\nTHERE was no possibility of taking a walk that day. We had been wandering, indeed, in the leafless shrubbery an hour in the morning; but since dinner (Mrs. Reed, when there was no company, dined early) the cold winter wind had brought with it clouds so sombre, and a rain so penetrating, that further outdoor exercise was now out of the question.'
  55. >>> text = Text(word_tokenize(jane_eyre))
  56. >>> text.concordance_list('taking')[0].left
  57. ['Chapter', '1', 'THERE', 'was', 'no', 'possibility', 'of']