PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/papershortener/nltk/tokenize/punkt.py

http://narorumo.googlecode.com/
Python | 1449 lines | 1349 code | 32 blank | 68 comment | 3 complexity | 8816934812b89aaf62bcf3e847963eb3 MD5 | raw file

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

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