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

/nltk/tokenize/punkt.py

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

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