Package: segtok
NB: segtok v2, code-namedsyntok, is available and fixes some tricky issues with segtok, in particular splitting sentence with terminals not followed by spaces.Like this :-).Sentence segmentation and word tokenizationThe segtok package provides two modules,segtok.segmenterandsegtok.tokenizer.
The segmenter provides functionality for splitting (Indo-European) text into sentences.
The tokenizer provides functionality for splitting (Indo-European) sentences into words and symbols (collectively calledtokens).
Both modules can also be used from the command-line.
While other Indo-European languages could work, it has only been designed with languages such as Spanish, English, and German in mind.
For a more informed introduction to this tool, please read the article on myblog.InstallTo use this package, you minimally should have the latest version of Python 2.7 or any 3.5+ branch installed.
The package is expected to work with both Python 2.7 and 3.5+, tested against those latest Python branches, as well as Python 3.5.
The easiest way to getsegtokinstalled is usingpipor any other package manager that works with PyPI:pip3 install segtokImportant: If you are on a Linux machine and have problems installing theregexdependency ofsegtok, make sure you have thepython-devand/orpython3-devpackages installed to get the necessary headers to compile the package.Then try the command line tools on some plain-text files (e.g., this README) to see ifsegtokmeets your needs:segmenter README.rst | tokenizerTest SuiteThe testing environment works withpytest,toxandpyenv.
You first need to installpyenv(on OSX with Homebrew:brew install pyenv), andtoxwithpytest(pip3 install tox pytest).
Configuringpyenvdepends on the Python versions you have installed.
Here, we assume you have the latest 2.7 and 3 versions installed and only need to provide an environment for testingsegtokagainst the 3.8 branch:pyenv install 3.8.2
pyenv global system 3.8.2The second command is essential and indicates that your preferred Python binary is the system version and then the 3.8.2 branch.
If you forget the second command, you will see errors likeERROR: InvocationError: Failed to get version_info for python3.8: pyenv: python3.8: command not foundwhen runningtox.
If you only have one Python version installed (say, 2.7), to fully run the tests, you must also install and globally configure the other version (e.g., the latest 3.x) withpyenv, too.Finally, to run all ofsegtok’s unit-test suite, just runtox:toxUsageFor details, please refer to the respective documentation; This README only provides an overview of the provided functionality.A command-lineAfter installing the package, two command-line tools will be available,segmenterandtokenizer.
Each can take UTF-8 encoded plain-text and transforms it into newline-separated sentences or tokens, respectively.
You can use other encoding in Python3 simply by reconfiguring your environment encoding or in any version of Python by forcing a particular encoding with the--encodingparameters.
The tokenizer assumes that each line contains (at most) one single sentence, which is the output format of the segmenter.
To learn more about each tool, please invoke them with their help option (-hor--help).Bsegtok.segmenterThis module provides severalsplit_...functions to segment texts into lists of sentences.
In addition,to_unix_linebreaksnormalizeslinebreaks (including the Unicode linebreak) to newline control characters (\\n).
The functionrewrite_line_separatorscan be used to move (rewrite) the newline separators in the input text so that they are placed at the sentence segmentation locations.Csegtok.tokenizerThis module provides several..._tokenizerfunctions to tokenize input sentences into words and symbols.
To get the full functionality, use theweb_tokenizer, which will split everything “semantically correctly” except for URLs and e-mail addresses.
In addition, it provides convenience functionality for English texts:
Two compiled patterns (IS_...) can be used to detect if a word token contains a possessive-s marker (“Frank’s”) or is an apostrophe-based contraction (“didn’t”).
Tokens that match these patterns can then be split using thesplit_possessive_markersandsplit_contractionsfunctions, respectively.LegalLicense:MITCopyright (c) 2014-2021, Florian Leitner. All rights reserved.Contributors (kudos):Mikhail Korobov (@kmike; port to Python2.7 and Travis CI integration)Georg Kucsko (@gkucsko; splitting sentences at terminals followed by noise)Karthikeyan Singaravelan (@tirkarthi; removing deprecation warnings, #23)Primož Godec (@PrimozGodec; fixed LICENSE file in setup.py)History1.5.11setup.py: renamed data_files with the LICENSE.txt file reference to license_files1.5.10removed deprecation warning (#23) as well as support for Python 3.3 from tox1.5.9added the license as a LICENSE.txt file to this repository1.5.7enhancement: split sentences even if the terminal is followed by invalid characters (contributed by @gkucsko)1.5.6fixed a bug that would lead to joining lines in single-line mode (#11, reported by @yucongo)1.5.5support for middle name initials (“Lester P. Pearson”)1.5.4also support for European-style number-dates with numeric months (24. 12. 2016)1.5.3added support for European-style number-dates and for months (24. Dez. 2016)1.5.2fixed a tokenizer bug when parsing URLs ending with root paths (/), prevented sentence splitting after U.K., U.S. and E.U. if followed by upper-case (“U.S. Air Force”), added missing Unicode hyphens and apostrophes, and added test suite setup instructions1.5.1removedcount_continuations.pydiscussion from README (was only confusing); the segmenter now can preserve tab-separated text IDs before the text itself when reading from STDIN and then inserts a (tab-separated) sentence ID column for each sentence printed to STDOUT: seesegmenteroption--with-ids1.5.0continuation words have been statistically evaluated and some poor choices removed (leading to more [precise] sentence splitting; see issue #9 by @Klim314 on GitHub)1.4.0theword_tokenizerno longer splits on colons between digits (time, references, …)1.3.1fixed multiple dangling commas and colons (reported by Jim Geovedi)1.3.0added Python2.7 support and Travis CI test integration (BIG thanks to Mikhail!)1.2.2made segtok.tokenizer.match protected (renamed to “_match”) and fixed UNIX linebreak normalization1.2.1the length of sentences inside brackets is now parametrized1.2.0wroteblog“documentation” and added chemical formula sub/super-script functionality1.1.2fixed Unicode list of valid sentence terminals (was missing U+2048)1.1.1fixed PyPI setup (missing MANIFEST.in for README.rst and “packages” in setup.py)1.1.0added possessive-s marker and apostrophe contraction splitting of tokens1.0.0initial release