punkt.py 61 KB

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