punkt.py 61 KB

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