PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/nltk/tokenize/punkt.py

https://github.com/BrucePHill/nltk
Python | 1601 lines | 1560 code | 7 blank | 34 comment | 5 complexity | 39e036d04646b3c0191e20c09b067e9b MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. # Natural Language Toolkit: Punkt sentence tokenizer
  2. #
  3. # Copyright (C) 2001-2013 NLTK Project
  4. # Algorithm: Kiss & Strunk (2006)
  5. # Author: Willy <willy@csse.unimelb.edu.au> (original Python port)
  6. # Steven Bird <stevenbird1@gmail.com> (additions)
  7. # Edward Loper <edloper@gradient.cis.upenn.edu> (rewrite)
  8. # Joel Nothman <jnothman@student.usyd.edu.au> (almost rewrite)
  9. # URL: <http://www.nltk.org/>
  10. # For license information, see LICENSE.TXT
  11. r"""
  12. Punkt Sentence Tokenizer
  13. This tokenizer divides a text into a list of sentences,
  14. by using an unsupervised algorithm to build a model for abbreviation
  15. words, collocations, and words that start sentences. It must be
  16. trained on a large collection of plaintext in the taret language
  17. before it can be used.
  18. The NLTK data package includes a pre-trained Punkt tokenizer for
  19. English.
  20. >>> import nltk.data
  21. >>> text = '''
  22. ... Punkt knows that the periods in Mr. Smith and Johann S. Bach
  23. ... do not mark sentence boundaries. And sometimes sentences
  24. ... can start with non-capitalized words. i is a good variable
  25. ... name.
  26. ... '''
  27. >>> sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
  28. >>> print('\n-----\n'.join(sent_detector.tokenize(text.strip())))
  29. Punkt knows that the periods in Mr. Smith and Johann S. Bach
  30. do not mark sentence boundaries.
  31. -----
  32. And sometimes sentences
  33. can start with non-capitalized words.
  34. -----
  35. i is a good variable
  36. name.
  37. (Note that whitespace from the original text, including newlines, is
  38. retained in the output.)
  39. Punctuation following sentences can be included with the realign_boundaries
  40. flag:
  41. >>> text = '''
  42. ... (How does it deal with this parenthesis?) "It should be part of the
  43. ... previous sentence."
  44. ... '''
  45. >>> print('\n-----\n'.join(
  46. ... sent_detector.tokenize(text.strip(), realign_boundaries=True)))
  47. (How does it deal with this parenthesis?)
  48. -----
  49. "It should be part of the
  50. previous sentence."
  51. However, Punkt is designed to learn parameters (a list of abbreviations, etc.)
  52. unsupervised from a corpus similar to the target domain. The pre-packaged models
  53. may therefore be unsuitable: use ``PunktSentenceTokenizer(text)`` to learn
  54. parameters from the given text.
  55. :class:`.PunktTrainer` learns parameters such as a list of abbreviations
  56. (without supervision) from portions of text. Using a ``PunktTrainer`` directly
  57. allows for incremental training and modification of the hyper-parameters used
  58. to decide what is considered an abbreviation, etc.
  59. :class:`.PunktWordTokenizer` uses a regular expression to divide a text into tokens,
  60. leaving all periods attached to words, but separating off other punctuation:
  61. >>> from nltk.tokenize.punkt import PunktWordTokenizer
  62. >>> s = "Good muffins cost $3.88\nin New York. Please buy me\ntwo of them.\n\nThanks."
  63. >>> PunktWordTokenizer().tokenize(s)
  64. ['Good', 'muffins', 'cost', '$3.88', 'in', 'New', 'York.', 'Please',
  65. 'buy', 'me', 'two', 'of', 'them.', 'Thanks.']
  66. The algorithm for this tokenizer is described in::
  67. Kiss, Tibor and Strunk, Jan (2006): Unsupervised Multilingual Sentence
  68. Boundary Detection. Computational Linguistics 32: 485-525.
  69. """
  70. from __future__ import print_function, unicode_literals
  71. # TODO: Make orthographic heuristic less susceptible to overtraining
  72. # TODO: Frequent sentence starters optionally exclude always-capitalised words
  73. # FIXME: Problem with ending string with e.g. '!!!' -> '!! !'
  74. import re
  75. import math
  76. from collections import defaultdict
  77. from nltk.compat import unicode_repr, python_2_unicode_compatible, string_types
  78. from nltk.probability import FreqDist
  79. from nltk.tokenize.api import TokenizerI
  80. ######################################################################
  81. #{ Orthographic Context Constants
  82. ######################################################################
  83. # The following constants are used to describe the orthographic
  84. # contexts in which a word can occur. BEG=beginning, MID=middle,
  85. # UNK=unknown, UC=uppercase, LC=lowercase, NC=no case.
  86. _ORTHO_BEG_UC = 1 << 1
  87. """Orthographic context: beginning of a sentence with upper case."""
  88. _ORTHO_MID_UC = 1 << 2
  89. """Orthographic context: middle of a sentence with upper case."""
  90. _ORTHO_UNK_UC = 1 << 3
  91. """Orthographic context: unknown position in a sentence with upper case."""
  92. _ORTHO_BEG_LC = 1 << 4
  93. """Orthographic context: beginning of a sentence with lower case."""
  94. _ORTHO_MID_LC = 1 << 5
  95. """Orthographic context: middle of a sentence with lower case."""
  96. _ORTHO_UNK_LC = 1 << 6
  97. """Orthographic context: unknown position in a sentence with lower case."""
  98. _ORTHO_UC = _ORTHO_BEG_UC + _ORTHO_MID_UC + _ORTHO_UNK_UC
  99. """Orthographic context: occurs with upper case."""
  100. _ORTHO_LC = _ORTHO_BEG_LC + _ORTHO_MID_LC + _ORTHO_UNK_LC
  101. """Orthographic context: occurs with lower case."""
  102. _ORTHO_MAP = {
  103. ('initial', 'upper'): _ORTHO_BEG_UC,
  104. ('internal', 'upper'): _ORTHO_MID_UC,
  105. ('unknown', 'upper'): _ORTHO_UNK_UC,
  106. ('initial', 'lower'): _ORTHO_BEG_LC,
  107. ('internal', 'lower'): _ORTHO_MID_LC,
  108. ('unknown', 'lower'): _ORTHO_UNK_LC,
  109. }
  110. """A map from context position and first-letter case to the
  111. appropriate orthographic context flag."""
  112. #} (end orthographic context constants)
  113. ######################################################################
  114. ######################################################################
  115. #{ Decision reasons for debugging
  116. ######################################################################
  117. REASON_DEFAULT_DECISION = 'default decision'
  118. REASON_KNOWN_COLLOCATION = 'known collocation (both words)'
  119. REASON_ABBR_WITH_ORTHOGRAPHIC_HEURISTIC = 'abbreviation + orthographic heuristic'
  120. REASON_ABBR_WITH_SENTENCE_STARTER = 'abbreviation + frequent sentence starter'
  121. REASON_INITIAL_WITH_ORTHOGRAPHIC_HEURISTIC = 'initial + orthographic heuristic'
  122. REASON_NUMBER_WITH_ORTHOGRAPHIC_HEURISTIC = 'initial + orthographic heuristic'
  123. REASON_INITIAL_WITH_SPECIAL_ORTHOGRAPHIC_HEURISTIC = 'initial + special orthographic heuristic'
  124. #} (end decision reasons for debugging)
  125. ######################################################################
  126. ######################################################################
  127. #{ Language-dependent variables
  128. ######################################################################
  129. class PunktLanguageVars(object):
  130. """
  131. Stores variables, mostly regular expressions, which may be
  132. language-dependent for correct application of the algorithm.
  133. An extension of this class may modify its properties to suit
  134. a language other than English; an instance can then be passed
  135. as an argument to PunktSentenceTokenizer and PunktTrainer
  136. constructors.
  137. """
  138. __slots__ = ('_re_period_context', '_re_word_tokenizer')
  139. def __getstate__(self):
  140. # All modifications to the class are performed by inheritance.
  141. # Non-default parameters to be pickled must be defined in the inherited
  142. # class.
  143. return 1
  144. def __setstate__(self, state):
  145. return 1
  146. sent_end_chars = ('.', '?', '!')
  147. """Characters which are candidates for sentence boundaries"""
  148. @property
  149. def _re_sent_end_chars(self):
  150. return '[%s]' % re.escape(''.join(self.sent_end_chars))
  151. internal_punctuation = ',:;' # might want to extend this..
  152. """sentence internal punctuation, which indicates an abbreviation if
  153. preceded by a period-final token."""
  154. re_boundary_realignment = re.compile(r'["\')\]}]+?(?:\s+|(?=--)|$)',
  155. re.MULTILINE)
  156. """Used to realign punctuation that should be included in a sentence
  157. although it follows the period (or ?, !)."""
  158. _re_word_start = r"[^\(\"\`{\[:;&\#\*@\)}\]\-,]"
  159. """Excludes some characters from starting word tokens"""
  160. _re_non_word_chars = r"(?:[?!)\";}\]\*:@\'\({\[])"
  161. """Characters that cannot appear within words"""
  162. _re_multi_char_punct = r"(?:\-{2,}|\.{2,}|(?:\.\s){2,}\.)"
  163. """Hyphen and ellipsis are multi-character punctuation"""
  164. _word_tokenize_fmt = r'''(
  165. %(MultiChar)s
  166. |
  167. (?=%(WordStart)s)\S+? # Accept word characters until end is found
  168. (?= # Sequences marking a word's end
  169. \s| # White-space
  170. $| # End-of-string
  171. %(NonWord)s|%(MultiChar)s| # Punctuation
  172. ,(?=$|\s|%(NonWord)s|%(MultiChar)s) # Comma if at end of word
  173. )
  174. |
  175. \S
  176. )'''
  177. """Format of a regular expression to split punctuation from words,
  178. excluding period."""
  179. def _word_tokenizer_re(self):
  180. """Compiles and returns a regular expression for word tokenization"""
  181. try:
  182. return self._re_word_tokenizer
  183. except AttributeError:
  184. self._re_word_tokenizer = re.compile(
  185. self._word_tokenize_fmt %
  186. {
  187. 'NonWord': self._re_non_word_chars,
  188. 'MultiChar': self._re_multi_char_punct,
  189. 'WordStart': self._re_word_start,
  190. },
  191. re.UNICODE | re.VERBOSE
  192. )
  193. return self._re_word_tokenizer
  194. def word_tokenize(self, s):
  195. """Tokenize a string to split off punctuation other than periods"""
  196. return self._word_tokenizer_re().findall(s)
  197. _period_context_fmt = r"""
  198. \S* # some word material
  199. %(SentEndChars)s # a potential sentence ending
  200. (?=(?P<after_tok>
  201. %(NonWord)s # either other punctuation
  202. |
  203. \s+(?P<next_tok>\S+) # or whitespace and some other token
  204. ))"""
  205. """Format of a regular expression to find contexts including possible
  206. sentence boundaries. Matches token which the possible sentence boundary
  207. ends, and matches the following token within a lookahead expression."""
  208. def period_context_re(self):
  209. """Compiles and returns a regular expression to find contexts
  210. including possible sentence boundaries."""
  211. try:
  212. return self._re_period_context
  213. except:
  214. self._re_period_context = re.compile(
  215. self._period_context_fmt %
  216. {
  217. 'NonWord': self._re_non_word_chars,
  218. 'SentEndChars': self._re_sent_end_chars,
  219. },
  220. re.UNICODE | re.VERBOSE)
  221. return self._re_period_context
  222. _re_non_punct = re.compile(r'[^\W\d]', re.UNICODE)
  223. """Matches token types that are not merely punctuation. (Types for
  224. numeric tokens are changed to ##number## and hence contain alpha.)"""
  225. #}
  226. ######################################################################
  227. ######################################################################
  228. #{ Punkt Word Tokenizer
  229. ######################################################################
  230. class PunktWordTokenizer(TokenizerI):
  231. # Retained for backward compatibility
  232. def __init__(self, lang_vars=PunktLanguageVars()):
  233. self._lang_vars = lang_vars
  234. def tokenize(self, text):
  235. return self._lang_vars.word_tokenize(text)
  236. #}
  237. ######################################################################
  238. #////////////////////////////////////////////////////////////
  239. #{ Helper Functions
  240. #////////////////////////////////////////////////////////////
  241. def _pair_iter(it):
  242. """
  243. Yields pairs of tokens from the given iterator such that each input
  244. token will appear as the first element in a yielded tuple. The last
  245. pair will have None as its second element.
  246. """
  247. it = iter(it)
  248. prev = next(it)
  249. for el in it:
  250. yield (prev, el)
  251. prev = el
  252. yield (prev, None)
  253. ######################################################################
  254. #{ Punkt Parameters
  255. ######################################################################
  256. class PunktParameters(object):
  257. """Stores data used to perform sentence boundary detection with Punkt."""
  258. def __init__(self):
  259. self.abbrev_types = set()
  260. """A set of word types for known abbreviations."""
  261. self.collocations = set()
  262. """A set of word type tuples for known common collocations
  263. where the first word ends in a period. E.g., ('S.', 'Bach')
  264. is a common collocation in a text that discusses 'Johann
  265. S. Bach'. These count as negative evidence for sentence
  266. boundaries."""
  267. self.sent_starters = set()
  268. """A set of word types for words that often appear at the
  269. beginning of sentences."""
  270. self.ortho_context = defaultdict(int)
  271. """A dictionary mapping word types to the set of orthographic
  272. contexts that word type appears in. Contexts are represented
  273. by adding orthographic context flags: ..."""
  274. def clear_abbrevs(self):
  275. self.abbrev_types = set()
  276. def clear_collocations(self):
  277. self.collocations = set()
  278. def clear_sent_starters(self):
  279. self.sent_starters = set()
  280. def clear_ortho_context(self):
  281. self.ortho_context = defaultdict(int)
  282. def add_ortho_context(self, typ, flag):
  283. self.ortho_context[typ] |= flag
  284. def _debug_ortho_context(self, typ):
  285. c = self.ortho_context[typ]
  286. if c & _ORTHO_BEG_UC:
  287. yield 'BEG-UC'
  288. if c & _ORTHO_MID_UC:
  289. yield 'MID-UC'
  290. if c & _ORTHO_UNK_UC:
  291. yield 'UNK-UC'
  292. if c & _ORTHO_BEG_LC:
  293. yield 'BEG-LC'
  294. if c & _ORTHO_MID_LC:
  295. yield 'MID-LC'
  296. if c & _ORTHO_UNK_LC:
  297. yield 'UNK-LC'
  298. ######################################################################
  299. #{ PunktToken
  300. ######################################################################
  301. @python_2_unicode_compatible
  302. class PunktToken(object):
  303. """Stores a token of text with annotations produced during
  304. sentence boundary detection."""
  305. _properties = [
  306. 'parastart', 'linestart',
  307. 'sentbreak', 'abbr', 'ellipsis'
  308. ]
  309. __slots__ = ['tok', 'type', 'period_final'] + _properties
  310. def __init__(self, tok, **params):
  311. self.tok = tok
  312. self.type = self._get_type(tok)
  313. self.period_final = tok.endswith('.')
  314. for p in self._properties:
  315. setattr(self, p, None)
  316. for k in params:
  317. setattr(self, k, params[k])
  318. #////////////////////////////////////////////////////////////
  319. #{ Regular expressions for properties
  320. #////////////////////////////////////////////////////////////
  321. # Note: [A-Za-z] is approximated by [^\W\d] in the general case.
  322. _RE_ELLIPSIS = re.compile(r'\.\.+$')
  323. _RE_NUMERIC = re.compile(r'^-?[\.,]?\d[\d,\.-]*\.?$')
  324. _RE_INITIAL = re.compile(r'[^\W\d]\.$', re.UNICODE)
  325. _RE_ALPHA = re.compile(r'[^\W\d]+$', re.UNICODE)
  326. #////////////////////////////////////////////////////////////
  327. #{ Derived properties
  328. #////////////////////////////////////////////////////////////
  329. def _get_type(self, tok):
  330. """Returns a case-normalized representation of the token."""
  331. return self._RE_NUMERIC.sub('##number##', tok.lower())
  332. @property
  333. def type_no_period(self):
  334. """
  335. The type with its final period removed if it has one.
  336. """
  337. if len(self.type) > 1 and self.type[-1] == '.':
  338. return self.type[:-1]
  339. return self.type
  340. @property
  341. def type_no_sentperiod(self):
  342. """
  343. The type with its final period removed if it is marked as a
  344. sentence break.
  345. """
  346. if self.sentbreak:
  347. return self.type_no_period
  348. return self.type
  349. @property
  350. def first_upper(self):
  351. """True if the token's first character is uppercase."""
  352. return self.tok[0].isupper()
  353. @property
  354. def first_lower(self):
  355. """True if the token's first character is lowercase."""
  356. return self.tok[0].islower()
  357. @property
  358. def first_case(self):
  359. if self.first_lower:
  360. return 'lower'
  361. elif self.first_upper:
  362. return 'upper'
  363. return 'none'
  364. @property
  365. def is_ellipsis(self):
  366. """True if the token text is that of an ellipsis."""
  367. return self._RE_ELLIPSIS.match(self.tok)
  368. @property
  369. def is_number(self):
  370. """True if the token text is that of a number."""
  371. return self.type.startswith('##number##')
  372. @property
  373. def is_initial(self):
  374. """True if the token text is that of an initial."""
  375. return self._RE_INITIAL.match(self.tok)
  376. @property
  377. def is_alpha(self):
  378. """True if the token text is all alphabetic."""
  379. return self._RE_ALPHA.match(self.tok)
  380. @property
  381. def is_non_punct(self):
  382. """True if the token is either a number or is alphabetic."""
  383. return _re_non_punct.search(self.type)
  384. #////////////////////////////////////////////////////////////
  385. #{ String representation
  386. #////////////////////////////////////////////////////////////
  387. def __repr__(self):
  388. """
  389. A string representation of the token that can reproduce it
  390. with eval(), which lists all the token's non-default
  391. annotations.
  392. """
  393. typestr = (' type=%s,' % unicode_repr(self.type)
  394. if self.type != self.tok else '')
  395. propvals = ', '.join(
  396. '%s=%s' % (p, unicode_repr(getattr(self, p)))
  397. for p in self._properties
  398. if getattr(self, p)
  399. )
  400. return '%s(%s,%s %s)' % (self.__class__.__name__,
  401. unicode_repr(self.tok), typestr, propvals)
  402. def __str__(self):
  403. """
  404. A string representation akin to that used by Kiss and Strunk.
  405. """
  406. res = self.tok
  407. if self.abbr:
  408. res += '<A>'
  409. if self.ellipsis:
  410. res += '<E>'
  411. if self.sentbreak:
  412. res += '<S>'
  413. return res
  414. ######################################################################
  415. #{ Punkt base class
  416. ######################################################################
  417. class PunktBaseClass(object):
  418. """
  419. Includes common components of PunktTrainer and PunktSentenceTokenizer.
  420. """
  421. def __init__(self, lang_vars=PunktLanguageVars(), token_cls=PunktToken,
  422. params=PunktParameters()):
  423. self._params = params
  424. self._lang_vars = lang_vars
  425. self._Token = token_cls
  426. """The collection of parameters that determines the behavior
  427. of the punkt tokenizer."""
  428. #////////////////////////////////////////////////////////////
  429. #{ Word tokenization
  430. #////////////////////////////////////////////////////////////
  431. def _tokenize_words(self, plaintext):
  432. """
  433. Divide the given text into tokens, using the punkt word
  434. segmentation regular expression, and generate the resulting list
  435. of tokens augmented as three-tuples with two boolean values for whether
  436. the given token occurs at the start of a paragraph or a new line,
  437. respectively.
  438. """
  439. parastart = False
  440. for line in plaintext.split('\n'):
  441. if line.strip():
  442. line_toks = iter(self._lang_vars.word_tokenize(line))
  443. yield self._Token(next(line_toks),
  444. parastart=parastart, linestart=True)
  445. parastart = False
  446. for t in line_toks:
  447. yield self._Token(t)
  448. else:
  449. parastart = True
  450. #////////////////////////////////////////////////////////////
  451. #{ Annotation Procedures
  452. #////////////////////////////////////////////////////////////
  453. def _annotate_first_pass(self, tokens):
  454. """
  455. Perform the first pass of annotation, which makes decisions
  456. based purely based on the word type of each word:
  457. - '?', '!', and '.' are marked as sentence breaks.
  458. - sequences of two or more periods are marked as ellipsis.
  459. - any word ending in '.' that's a known abbreviation is
  460. marked as an abbreviation.
  461. - any other word ending in '.' is marked as a sentence break.
  462. Return these annotations as a tuple of three sets:
  463. - sentbreak_toks: The indices of all sentence breaks.
  464. - abbrev_toks: The indices of all abbreviations.
  465. - ellipsis_toks: The indices of all ellipsis marks.
  466. """
  467. for aug_tok in tokens:
  468. self._first_pass_annotation(aug_tok)
  469. yield aug_tok
  470. def _first_pass_annotation(self, aug_tok):
  471. """
  472. Performs type-based annotation on a single token.
  473. """
  474. tok = aug_tok.tok
  475. if tok in self._lang_vars.sent_end_chars:
  476. aug_tok.sentbreak = True
  477. elif aug_tok.is_ellipsis:
  478. aug_tok.ellipsis = True
  479. elif aug_tok.period_final and not tok.endswith('..'):
  480. if (tok[:-1].lower() in self._params.abbrev_types or
  481. tok[:-1].lower().split('-')[-1] in self._params.abbrev_types):
  482. aug_tok.abbr = True
  483. else:
  484. aug_tok.sentbreak = True
  485. return
  486. ######################################################################
  487. #{ Punkt Trainer
  488. ######################################################################
  489. class PunktTrainer(PunktBaseClass):
  490. """Learns parameters used in Punkt sentence boundary detection."""
  491. def __init__(self, train_text=None, verbose=False,
  492. lang_vars=PunktLanguageVars(), token_cls=PunktToken):
  493. PunktBaseClass.__init__(self, lang_vars=lang_vars,
  494. token_cls=token_cls)
  495. self._type_fdist = FreqDist()
  496. """A frequency distribution giving the frequency of each
  497. case-normalized token type in the training data."""
  498. self._num_period_toks = 0
  499. """The number of words ending in period in the training data."""
  500. self._collocation_fdist = FreqDist()
  501. """A frequency distribution giving the frequency of all
  502. bigrams in the training data where the first word ends in a
  503. period. Bigrams are encoded as tuples of word types.
  504. Especially common collocations are extracted from this
  505. frequency distribution, and stored in
  506. ``_params``.``collocations <PunktParameters.collocations>``."""
  507. self._sent_starter_fdist = FreqDist()
  508. """A frequency distribution giving the frequency of all words
  509. that occur at the training data at the beginning of a sentence
  510. (after the first pass of annotation). Especially common
  511. sentence starters are extracted from this frequency
  512. distribution, and stored in ``_params.sent_starters``.
  513. """
  514. self._sentbreak_count = 0
  515. """The total number of sentence breaks identified in training, used for
  516. calculating the frequent sentence starter heuristic."""
  517. self._finalized = True
  518. """A flag as to whether the training has been finalized by finding
  519. collocations and sentence starters, or whether finalize_training()
  520. still needs to be called."""
  521. if train_text:
  522. self.train(train_text, verbose, finalize=True)
  523. def get_params(self):
  524. """
  525. Calculates and returns parameters for sentence boundary detection as
  526. derived from training."""
  527. if not self._finalized:
  528. self.finalize_training()
  529. return self._params
  530. #////////////////////////////////////////////////////////////
  531. #{ Customization Variables
  532. #////////////////////////////////////////////////////////////
  533. ABBREV = 0.3
  534. """cut-off value whether a 'token' is an abbreviation"""
  535. IGNORE_ABBREV_PENALTY = False
  536. """allows the disabling of the abbreviation penalty heuristic, which
  537. exponentially disadvantages words that are found at times without a
  538. final period."""
  539. ABBREV_BACKOFF = 5
  540. """upper cut-off for Mikheev's(2002) abbreviation detection algorithm"""
  541. COLLOCATION = 7.88
  542. """minimal log-likelihood value that two tokens need to be considered
  543. as a collocation"""
  544. SENT_STARTER = 30
  545. """minimal log-likelihood value that a token requires to be considered
  546. as a frequent sentence starter"""
  547. INCLUDE_ALL_COLLOCS = False
  548. """this includes as potential collocations all word pairs where the first
  549. word ends in a period. It may be useful in corpora where there is a lot
  550. of variation that makes abbreviations like Mr difficult to identify."""
  551. INCLUDE_ABBREV_COLLOCS = False
  552. """this includes as potential collocations all word pairs where the first
  553. word is an abbreviation. Such collocations override the orthographic
  554. heuristic, but not the sentence starter heuristic. This is overridden by
  555. INCLUDE_ALL_COLLOCS, and if both are false, only collocations with initials
  556. and ordinals are considered."""
  557. """"""
  558. MIN_COLLOC_FREQ = 1
  559. """this sets a minimum bound on the number of times a bigram needs to
  560. appear before it can be considered a collocation, in addition to log
  561. likelihood statistics. This is useful when INCLUDE_ALL_COLLOCS is True."""
  562. #////////////////////////////////////////////////////////////
  563. #{ Training..
  564. #////////////////////////////////////////////////////////////
  565. def train(self, text, verbose=False, finalize=True):
  566. """
  567. Collects training data from a given text. If finalize is True, it
  568. will determine all the parameters for sentence boundary detection. If
  569. not, this will be delayed until get_params() or finalize_training() is
  570. called. If verbose is True, abbreviations found will be listed.
  571. """
  572. # Break the text into tokens; record which token indices correspond to
  573. # line starts and paragraph starts; and determine their types.
  574. self._train_tokens(self._tokenize_words(text), verbose)
  575. if finalize:
  576. self.finalize_training(verbose)
  577. def train_tokens(self, tokens, verbose=False, finalize=True):
  578. """
  579. Collects training data from a given list of tokens.
  580. """
  581. self._train_tokens((self._Token(t) for t in tokens), verbose)
  582. if finalize:
  583. self.finalize_training(verbose)
  584. def _train_tokens(self, tokens, verbose):
  585. self._finalized = False
  586. # Ensure tokens are a list
  587. tokens = list(tokens)
  588. # Find the frequency of each case-normalized type. (Don't
  589. # strip off final periods.) Also keep track of the number of
  590. # tokens that end in periods.
  591. for aug_tok in tokens:
  592. self._type_fdist.inc(aug_tok.type)
  593. if aug_tok.period_final:
  594. self._num_period_toks += 1
  595. # Look for new abbreviations, and for types that no longer are
  596. unique_types = self._unique_types(tokens)
  597. for abbr, score, is_add in self._reclassify_abbrev_types(unique_types):
  598. if score >= self.ABBREV:
  599. if is_add:
  600. self._params.abbrev_types.add(abbr)
  601. if verbose:
  602. print((' Abbreviation: [%6.4f] %s' %
  603. (score, abbr)))
  604. else:
  605. if not is_add:
  606. self._params.abbrev_types.remove(abbr)
  607. if verbose:
  608. print((' Removed abbreviation: [%6.4f] %s' %
  609. (score, abbr)))
  610. # Make a preliminary pass through the document, marking likely
  611. # sentence breaks, abbreviations, and ellipsis tokens.
  612. tokens = list(self._annotate_first_pass(tokens))
  613. # Check what contexts each word type can appear in, given the
  614. # case of its first letter.
  615. self._get_orthography_data(tokens)
  616. # We need total number of sentence breaks to find sentence starters
  617. self._sentbreak_count += self._get_sentbreak_count(tokens)
  618. # The remaining heuristics relate to pairs of tokens where the first
  619. # ends in a period.
  620. for aug_tok1, aug_tok2 in _pair_iter(tokens):
  621. if not aug_tok1.period_final or not aug_tok2:
  622. continue
  623. # Is the first token a rare abbreviation?
  624. if self._is_rare_abbrev_type(aug_tok1, aug_tok2):
  625. self._params.abbrev_types.add(aug_tok1.type_no_period)
  626. if verbose:
  627. print((' Rare Abbrev: %s' % aug_tok1.type))
  628. # Does second token have a high likelihood of starting a sentence?
  629. if self._is_potential_sent_starter(aug_tok2, aug_tok1):
  630. self._sent_starter_fdist.inc(aug_tok2.type)
  631. # Is this bigram a potential collocation?
  632. if self._is_potential_collocation(aug_tok1, aug_tok2):
  633. self._collocation_fdist.inc(
  634. (aug_tok1.type_no_period, aug_tok2.type_no_sentperiod))
  635. def _unique_types(self, tokens):
  636. return set(aug_tok.type for aug_tok in tokens)
  637. def finalize_training(self, verbose=False):
  638. """
  639. Uses data that has been gathered in training to determine likely
  640. collocations and sentence starters.
  641. """
  642. self._params.clear_sent_starters()
  643. for typ, ll in self._find_sent_starters():
  644. self._params.sent_starters.add(typ)
  645. if verbose:
  646. print((' Sent Starter: [%6.4f] %r' % (ll, typ)))
  647. self._params.clear_collocations()
  648. for (typ1, typ2), ll in self._find_collocations():
  649. self._params.collocations.add( (typ1,typ2) )
  650. if verbose:
  651. print((' Collocation: [%6.4f] %r+%r' %
  652. (ll, typ1, typ2)))
  653. self._finalized = True
  654. #////////////////////////////////////////////////////////////
  655. #{ Overhead reduction
  656. #////////////////////////////////////////////////////////////
  657. def freq_threshold(self, ortho_thresh=2, type_thresh=2, colloc_thres=2,
  658. sentstart_thresh=2):
  659. """
  660. Allows memory use to be reduced after much training by removing data
  661. about rare tokens that are unlikely to have a statistical effect with
  662. further training. Entries occurring above the given thresholds will be
  663. retained.
  664. """
  665. if ortho_thresh > 1:
  666. old_oc = self._params.ortho_context
  667. self._params.clear_ortho_context()
  668. for tok in self._type_fdist:
  669. count = self._type_fdist[tok]
  670. if count >= ortho_thresh:
  671. self._params.ortho_context[tok] = old_oc[tok]
  672. self._type_fdist = self._freq_threshold(self._type_fdist, type_thresh)
  673. self._collocation_fdist = self._freq_threshold(
  674. self._collocation_fdist, colloc_thres)
  675. self._sent_starter_fdist = self._freq_threshold(
  676. self._sent_starter_fdist, sentstart_thresh)
  677. def _freq_threshold(self, fdist, threshold):
  678. """
  679. Returns a FreqDist containing only data with counts below a given
  680. threshold, as well as a mapping (None -> count_removed).
  681. """
  682. # We assume that there is more data below the threshold than above it
  683. # and so create a new FreqDist rather than working in place.
  684. res = FreqDist()
  685. num_removed = 0
  686. for tok in fdist:
  687. count = fdist[tok]
  688. if count < threshold:
  689. num_removed += 1
  690. else:
  691. res.inc(tok, count)
  692. res.inc(None, num_removed)
  693. return res
  694. #////////////////////////////////////////////////////////////
  695. #{ Orthographic data
  696. #////////////////////////////////////////////////////////////
  697. def _get_orthography_data(self, tokens):
  698. """
  699. Collect information about whether each token type occurs
  700. with different case patterns (i) overall, (ii) at
  701. sentence-initial positions, and (iii) at sentence-internal
  702. positions.
  703. """
  704. # 'initial' or 'internal' or 'unknown'
  705. context = 'internal'
  706. tokens = list(tokens)
  707. for aug_tok in tokens:
  708. # If we encounter a paragraph break, then it's a good sign
  709. # that it's a sentence break. But err on the side of
  710. # caution (by not positing a sentence break) if we just
  711. # saw an abbreviation.
  712. if aug_tok.parastart and context != 'unknown':
  713. context = 'initial'
  714. # If we're at the beginning of a line, then err on the
  715. # side of calling our context 'initial'.
  716. if aug_tok.linestart and context == 'internal':
  717. context = 'unknown'
  718. # Find the case-normalized type of the token. If it's a
  719. # sentence-final token, strip off the period.
  720. typ = aug_tok.type_no_sentperiod
  721. # Update the orthographic context table.
  722. flag = _ORTHO_MAP.get((context, aug_tok.first_case), 0)
  723. if flag:
  724. self._params.add_ortho_context(typ, flag)
  725. # Decide whether the next word is at a sentence boundary.
  726. if aug_tok.sentbreak:
  727. if not (aug_tok.is_number or aug_tok.is_initial):
  728. context = 'initial'
  729. else:
  730. context = 'unknown'
  731. elif aug_tok.ellipsis or aug_tok.abbr:
  732. context = 'unknown'
  733. else:
  734. context = 'internal'
  735. #////////////////////////////////////////////////////////////
  736. #{ Abbreviations
  737. #////////////////////////////////////////////////////////////
  738. def _reclassify_abbrev_types(self, types):
  739. """
  740. (Re)classifies each given token if
  741. - it is period-final and not a known abbreviation; or
  742. - it is not period-final and is otherwise a known abbreviation
  743. by checking whether its previous classification still holds according
  744. to the heuristics of section 3.
  745. Yields triples (abbr, score, is_add) where abbr is the type in question,
  746. score is its log-likelihood with penalties applied, and is_add specifies
  747. whether the present type is a candidate for inclusion or exclusion as an
  748. abbreviation, such that:
  749. - (is_add and score >= 0.3) suggests a new abbreviation; and
  750. - (not is_add and score < 0.3) suggests excluding an abbreviation.
  751. """
  752. # (While one could recalculate abbreviations from all .-final tokens at
  753. # every iteration, in cases requiring efficiency, the number of tokens
  754. # in the present training document will be much less.)
  755. for typ in types:
  756. # Check some basic conditions, to rule out words that are
  757. # clearly not abbrev_types.
  758. if not _re_non_punct.search(typ) or typ == '##number##':
  759. continue
  760. if typ.endswith('.'):
  761. if typ in self._params.abbrev_types:
  762. continue
  763. typ = typ[:-1]
  764. is_add = True
  765. else:
  766. if typ not in self._params.abbrev_types:
  767. continue
  768. is_add = False
  769. # Count how many periods & nonperiods are in the
  770. # candidate.
  771. num_periods = typ.count('.') + 1
  772. num_nonperiods = len(typ) - num_periods + 1
  773. # Let <a> be the candidate without the period, and <b>
  774. # be the period. Find a log likelihood ratio that
  775. # indicates whether <ab> occurs as a single unit (high
  776. # value of ll), or as two independent units <a> and
  777. # <b> (low value of ll).
  778. count_with_period = self._type_fdist[typ + '.']
  779. count_without_period = self._type_fdist[typ]
  780. ll = self._dunning_log_likelihood(
  781. count_with_period + count_without_period,
  782. self._num_period_toks, count_with_period,
  783. self._type_fdist.N())
  784. # Apply three scaling factors to 'tweak' the basic log
  785. # likelihood ratio:
  786. # F_length: long word -> less likely to be an abbrev
  787. # F_periods: more periods -> more likely to be an abbrev
  788. # F_penalty: penalize occurrences w/o a period
  789. f_length = math.exp(-num_nonperiods)
  790. f_periods = num_periods
  791. f_penalty = (int(self.IGNORE_ABBREV_PENALTY)
  792. or math.pow(num_nonperiods, -count_without_period))
  793. score = ll * f_length * f_periods * f_penalty
  794. yield typ, score, is_add
  795. def find_abbrev_types(self):
  796. """
  797. Recalculates abbreviations given type frequencies, despite no prior
  798. determination of abbreviations.
  799. This fails to include abbreviations otherwise found as "rare".
  800. """
  801. self._params.clear_abbrevs()
  802. tokens = (typ for typ in self._type_fdist if typ and typ.endswith('.'))
  803. for abbr, score, is_add in self._reclassify_abbrev_types(tokens):
  804. if score >= self.ABBREV:
  805. self._params.abbrev_types.add(abbr)
  806. # This function combines the work done by the original code's
  807. # functions `count_orthography_context`, `get_orthography_count`,
  808. # and `get_rare_abbreviations`.
  809. def _is_rare_abbrev_type(self, cur_tok, next_tok):
  810. """
  811. A word type is counted as a rare abbreviation if...
  812. - it's not already marked as an abbreviation
  813. - it occurs fewer than ABBREV_BACKOFF times
  814. - either it is followed by a sentence-internal punctuation
  815. mark, *or* it is followed by a lower-case word that
  816. sometimes appears with upper case, but never occurs with
  817. lower case at the beginning of sentences.
  818. """
  819. if cur_tok.abbr or not cur_tok.sentbreak:
  820. return False
  821. # Find the case-normalized type of the token. If it's
  822. # a sentence-final token, strip off the period.
  823. typ = cur_tok.type_no_sentperiod
  824. # Proceed only if the type hasn't been categorized as an
  825. # abbreviation already, and is sufficiently rare...
  826. count = self._type_fdist[typ] + self._type_fdist[typ[:-1]]
  827. if (typ in self._params.abbrev_types or count >= self.ABBREV_BACKOFF):
  828. return False
  829. # Record this token as an abbreviation if the next
  830. # token is a sentence-internal punctuation mark.
  831. # [XX] :1 or check the whole thing??
  832. if next_tok.tok[:1] in self._lang_vars.internal_punctuation:
  833. return True
  834. # Record this type as an abbreviation if the next
  835. # token... (i) starts with a lower case letter,
  836. # (ii) sometimes occurs with an uppercase letter,
  837. # and (iii) never occus with an uppercase letter
  838. # sentence-internally.
  839. # [xx] should the check for (ii) be modified??
  840. elif next_tok.first_lower:
  841. typ2 = next_tok.type_no_sentperiod
  842. typ2ortho_context = self._params.ortho_context[typ2]
  843. if ( (typ2ortho_context & _ORTHO_BEG_UC) and
  844. not (typ2ortho_context & _ORTHO_MID_UC) ):
  845. return True
  846. #////////////////////////////////////////////////////////////
  847. #{ Log Likelihoods
  848. #////////////////////////////////////////////////////////////
  849. # helper for _reclassify_abbrev_types:
  850. @staticmethod
  851. def _dunning_log_likelihood(count_a, count_b, count_ab, N):
  852. """
  853. A function that calculates the modified Dunning log-likelihood
  854. ratio scores for abbreviation candidates. The details of how
  855. this works is available in the paper.
  856. """
  857. p1 = float(count_b) / N
  858. p2 = 0.99
  859. null_hypo = (float(count_ab) * math.log(p1) +
  860. (count_a - count_ab) * math.log(1.0 - p1))
  861. alt_hypo = (float(count_ab) * math.log(p2) +
  862. (count_a - count_ab) * math.log(1.0 - p2))
  863. likelihood = null_hypo - alt_hypo
  864. return (-2.0 * likelihood)
  865. @staticmethod
  866. def _col_log_likelihood(count_a, count_b, count_ab, N):
  867. """
  868. A function that will just compute log-likelihood estimate, in
  869. the original paper it's described in algorithm 6 and 7.
  870. This *should* be the original Dunning log-likelihood values,
  871. unlike the previous log_l function where it used modified
  872. Dunning log-likelihood values
  873. """
  874. import math
  875. p = 1.0 * count_b / N
  876. p1 = 1.0 * count_ab / count_a
  877. p2 = 1.0 * (count_b - count_ab) / (N - count_a)
  878. summand1 = (count_ab * math.log(p) +
  879. (count_a - count_ab) * math.log(1.0 - p))
  880. summand2 = ((count_b - count_ab) * math.log(p) +
  881. (N - count_a - count_b + count_ab) * math.log(1.0 - p))
  882. if count_a == count_ab:
  883. summand3 = 0
  884. else:
  885. summand3 = (count_ab * math.log(p1) +
  886. (count_a - count_ab) * math.log(1.0 - p1))
  887. if count_b == count_ab:
  888. summand4 = 0
  889. else:
  890. summand4 = ((count_b - count_ab) * math.log(p2) +
  891. (N - count_a - count_b + count_ab) * math.log(1.0 - p2))
  892. likelihood = summand1 + summand2 - summand3 - summand4
  893. return (-2.0 * likelihood)
  894. #////////////////////////////////////////////////////////////
  895. #{ Collocation Finder
  896. #////////////////////////////////////////////////////////////
  897. def _is_potential_collocation(self, aug_tok1, aug_tok2):
  898. """
  899. Returns True if the pair of tokens may form a collocation given
  900. log-likelihood statistics.
  901. """
  902. return ((self.INCLUDE_ALL_COLLOCS or
  903. (self.INCLUDE_ABBREV_COLLOCS and aug_tok1.abbr) or
  904. (aug_tok1.sentbreak and
  905. (aug_tok1.is_number or aug_tok1.is_initial)))
  906. and aug_tok1.is_non_punct
  907. and aug_tok2.is_non_punct)
  908. def _find_collocations(self):
  909. """
  910. Generates likely collocations and their log-likelihood.
  911. """
  912. for types in self._collocation_fdist:
  913. try:
  914. typ1, typ2 = types
  915. except TypeError:
  916. # types may be None after calling freq_threshold()
  917. continue
  918. if typ2 in self._params.sent_starters:
  919. continue
  920. col_count = self._collocation_fdist[types]
  921. typ1_count = self._type_fdist[typ1]+self._type_fdist[typ1+'.']
  922. typ2_count = self._type_fdist[typ2]+self._type_fdist[typ2+'.']
  923. if (typ1_count > 1 and typ2_count > 1
  924. and self.MIN_COLLOC_FREQ <
  925. col_count <= min(typ1_count, typ2_count)):
  926. ll = self._col_log_likelihood(typ1_count, typ2_count,
  927. col_count, self._type_fdist.N())
  928. # Filter out the not-so-collocative
  929. if (ll >= self.COLLOCATION and
  930. (float(self._type_fdist.N())/typ1_count >
  931. float(typ2_count)/col_count)):
  932. yield (typ1, typ2), ll
  933. #////////////////////////////////////////////////////////////
  934. #{ Sentence-Starter Finder
  935. #////////////////////////////////////////////////////////////
  936. def _is_potential_sent_starter(self, cur_tok, prev_tok):
  937. """
  938. Returns True given a token and the token that preceds it if it
  939. seems clear that the token is beginning a sentence.
  940. """
  941. # If a token (i) is preceded by a sentece break that is
  942. # not a potential ordinal number or initial, and (ii) is
  943. # alphabetic, then it is a a sentence-starter.
  944. return ( prev_tok.sentbreak and
  945. not (prev_tok.is_number or prev_tok.is_initial) and
  946. cur_tok.is_alpha )
  947. def _find_sent_starters(self):
  948. """
  949. Uses collocation heuristics for each candidate token to
  950. determine if it frequently starts sentences.
  951. """
  952. for typ in self._sent_starter_fdist:
  953. if not typ:
  954. continue
  955. typ_at_break_count = self._sent_starter_fdist[typ]
  956. typ_count = self._type_fdist[typ]+self._type_fdist[typ+'.']
  957. if typ_count < typ_at_break_count:
  958. # needed after freq_threshold
  959. continue
  960. ll = self._col_log_likelihood(self._sentbreak_count, typ_count,
  961. typ_at_break_count,
  962. self._type_fdist.N())
  963. if (ll >= self.SENT_STARTER and
  964. float(self._type_fdist.N())/self._sentbreak_count >
  965. float(typ_count)/typ_at_break_count):
  966. yield typ, ll
  967. def _get_sentbreak_count(self, tokens):
  968. """
  969. Returns the number of sentence breaks marked in a given set of
  970. augmented tokens.
  971. """
  972. return sum(1 for aug_tok in tokens if aug_tok.sentbreak)
  973. ######################################################################
  974. #{ Punkt Sentence Tokenizer
  975. ######################################################################
  976. class PunktSentenceTokenizer(PunktBaseClass,TokenizerI):
  977. """
  978. A sentence tokenizer which uses an unsupervised algorithm to build
  979. a model for abbreviation words, collocations, and words that start
  980. sentences; and then uses that model to find sentence boundaries.
  981. This approach has been shown to work well for many European
  982. languages.
  983. """
  984. def __init__(self, train_text=None, verbose=False,
  985. lang_vars=PunktLanguageVars(), token_cls=PunktToken):
  986. """
  987. train_text can either be the sole training text for this sentence
  988. boundary detector, or can be a PunktParameters object.
  989. """
  990. PunktBaseClass.__init__(self, lang_vars=lang_vars,
  991. token_cls=token_cls)
  992. if train_text:
  993. self._params = self.train(train_text, verbose)
  994. def train(self, train_text, verbose=False):
  995. """
  996. Derives parameters from a given training text, or uses the parameters
  997. given. Repeated calls to this method destroy previous parameters. For
  998. incremental training, instantiate a separate PunktTrainer instance.
  999. """
  1000. if not isinstance(train_text, string_types):
  1001. return train_text
  1002. return PunktTrainer(train_text, lang_vars=self._lang_vars,
  1003. token_cls=self._Token).get_params()
  1004. #////////////////////////////////////////////////////////////
  1005. #{ Tokenization
  1006. #////////////////////////////////////////////////////////////
  1007. def tokenize(self, text, realign_boundaries=False):
  1008. """
  1009. Given a text, returns a list of the sentences in that text.
  1010. """
  1011. return list(self.sentences_from_text(text, realign_boundaries))
  1012. def debug_decisions(self, text):
  1013. """
  1014. Classifies candidate periods as sentence breaks, yielding a dict for
  1015. each that may be used to understand why the decision was made.
  1016. See format_debug_decision() to help make this output readable.
  1017. """
  1018. for match in self._lang_vars.period_context_re().finditer(text):
  1019. decision_text = match.group() + match.group('after_tok')
  1020. tokens = self._tokenize_words(decision_text)
  1021. tokens = list(self._annotate_first_pass(tokens))
  1022. while not tokens[0].period_final:
  1023. tokens.pop(0)
  1024. yield dict(period_index=match.end() - 1,
  1025. text=decision_text,
  1026. type1=tokens[0].type,
  1027. type2=tokens[1].type,
  1028. type1_in_abbrs=bool(tokens[0].abbr),
  1029. type1_is_initial=bool(tokens[0].is_initial),
  1030. type2_is_sent_starter=tokens[1].type_no_sentperiod in self._params.sent_starters,
  1031. type2_ortho_heuristic=self._ortho_heuristic(tokens[1]),
  1032. type2_ortho_contexts=set(self._params._debug_ortho_context(tokens[1].type_no_sentperiod)),
  1033. collocation=(tokens[0].type_no_sentperiod, tokens[1].type_no_sentperiod) in self._params.collocations,
  1034. reason=self._second_pass_annotation(tokens[0], tokens[1]) or REASON_DEFAULT_DECISION,
  1035. break_decision=tokens[0].sentbreak,
  1036. )
  1037. def span_tokenize(self, text):
  1038. """
  1039. Given a text, returns a list of the (start, end) spans of sentences
  1040. in the text.
  1041. """
  1042. return [(sl.start, sl.stop) for sl in self._slices_from_text(text)]
  1043. def sentences_from_text(self, text, realign_boundaries=False):
  1044. """
  1045. Given a text, generates the sentences in that text by only
  1046. testing candidate sentence breaks. If realign_boundaries is
  1047. True, includes in the sentence closing punctuation that
  1048. follows the period.
  1049. """
  1050. sents = [text[sl] for sl in self._slices_from_text(text)]
  1051. if realign_boundaries:
  1052. sents = self._realign_boundaries(sents)
  1053. return sents
  1054. def _slices_from_text(self, text):
  1055. last_break = 0
  1056. for match in self._lang_vars.period_context_re().finditer(text):
  1057. context = match.group() + match.group('after_tok')
  1058. if self.text_contains_sentbreak(context):
  1059. yield slice(last_break, match.end())
  1060. if match.group('next_tok'):
  1061. # next sentence starts after whitespace
  1062. last_break = match.start('next_tok')
  1063. else:
  1064. # next sentence starts at following punctuation
  1065. last_break = match.end()
  1066. yield slice(last_break, len(text))
  1067. def _realign_boundaries(self, sents):
  1068. """
  1069. Attempts to realign punctuation that falls after the

Large files files are truncated, but you can click here to view the full file