Package: sentencex
Sentence segmenterA sentence segmentation library with wide language support optimized for speed and utility.ApproachIf it's a period, it ends a sentence.If the preceding token is in the hand-compiled list of abbreviations, then it doesn't end a sentence.However, it is not 'period' for many languages. So we will use a list of known punctuations that can cause a sentence break in as many languages as possible.We also collect a list of known, popular abbreviations in as many languages as possible.Sometimes, it is very hard to get the segmentation correct. In such cases this library is opinionated and prefer not segmenting than wrong segmentation.  If two sentences are accidentally together, that is ok. It is better than sentence being split in middle.
Avoid over engineering to get everything linguistically 100% accurate.This approach would be suitable for applications like text to speech, machine translation.Consider this example:We make a good team, you and I. Did you see Albert I. Jones yesterday?The accurate splitting of this sentence is["We make a good team, you and I." ,"Did you see Albert I. Jones yesterday?"]However, to achieve this level precision, complex rules need to be added and it could create side effects. Instead, if we just don't segment betweenI. Did, it is ok for most of downstream applications.The sentence segmentation in this library isnon-destructive. This means, if the sentences are combined together, you can reconstruct the original text. Line breaks, punctuations and whitespaces are preserved in the output.UsageInstall the library usingpipinstallsentencexThen, any text can be segmented as follows.fromsentenceximportsegmenttext="""The James Webb Space Telescope (JWST) is a space telescope specifically designed to conduct infrared astronomy. The U.S. National Aeronautics and Space Administration (NASA) led Webb's design and development")"""print(list(segment("en",text)))The first argument is language code, second argument is text to segment. Thesegmentmethod returns an iterator on identified sentences.Language supportThe aim is to support all languages where there is a wikipedia. Instead of falling back on English for languages not defined in the library, a fallback chain is used. The closest language which is defined in the library will be used. Fallbacks for ~244 languages are defined.PerformanceMeasured on Golden Rule Set(GRS) for English. Lists are exempted (1. sentence 2. another sentence).The following libraries are used for benchmarking:mwtokenizer fromhttps://gitlab.wikimedia.org/repos/research/wiki-nlp-toolsblingfire fromhttps://github.com/microsoft/BlingFirenltk fromhttps://pypi.org/project/nltk/pysbd fromhttps://github.com/nipunsadvilkar/pySBD/spacy fromhttps://github.com/stanfordnlp/stanzasyntok fromhttps://github.com/fnl/syntokTokenizer LibraryEnglish Golden Rule Set scoreSpeed(Avg over 100 runs) in secondssentencex_segment74.360.93mwtokenizer_tokenize30.771.54blingfire_tokenize89.740.27nltk_tokenize66.671.86pysbd_tokenize97.4410.57spacy_tokenize61.542.45spacy_dep_tokenize74.36138.93stanza_tokenize87.18107.51syntok_tokenize79.494.72Thankshttps://github.com/diasks2/pragmatic_segmenterfor test cases. The English golden rule set is also sourced from it.LicenseMIT license. SeeLicense.txt