Package: zh_segment
zh_segmentis an Apache2 licensed module for English word
segmentation, written in pure-Python, and based on a trillion-word corpus.Based on code from the chapter “Natural Language Corpus Data” by Peter
Norvig from the book “Beautiful Data” (Segaran and Hammerbacher, 2009).Data files are derived from theGoogle Web Trillion Word Corpus, as
described by Thorsten Brants and Alex Franz, anddistributedby the
Linguistic Data Consortium. This module contains only a subset of that
data. The unigram data includes only the most common 333,000 words. Similarly,
bigram data includes only the most common 250,000 phrases. Every word and
phrase is lowercased with punctuation removed.FeaturesPure-PythonFully documented100% Test CoverageIncludes unigram and bigram dataCommand line interface for batch processingEasy to hack (e.g. different scoring, new data, different language)Developed on Python 2.7Tested on CPython 2.6, 2.7, 3.2, 3.3, 3.4 and PyPy 2.5+, PyPy3 2.4+QuickstartInstalling zh_segment is simple withpip:$ pip install zh_segmentYou can access documentation in the interpreter with Python’s built-in help
function:>>> import zh_segment
>>> help(zh_segment)TutorialIn your own Python programs, you’ll mostly want to usesegmentto divide a
phrase into a list of its parts:>>> from zh_segment import segment
>>> segment('1077501; 1296599; 5000; 5000; 4975; 36 months; 10.64%; 162.87; B; B2;;10+ years;RENT')
['1077501', '1296599', '5000', '5000', '4975', '36', 'months', '10.64%', '162.87', 'B', 'B', '2', '10+', 'years', 'RENT']zh_segment also provides a command-line interface for batch processing. This
interface accepts two arguments: in-file and out-file. Lines from in-file are
iteratively segmented, joined by a space, and written to out-file. Input and
output default to stdin and stdout respectively.$ echo thisisatest | python -m zh_segment
this is a testThe maximum segmented word length is 24 characters. Neither the unigram nor
bigram data contain words exceeding that length. The corpus also excludes
punctuation and all letters have been lowercased. Before segmenting text,cleanis called to transform the input to a canonical form:>>> from zh_segment import clean
>>> clean('She said, "Python rocks!"')
'shesaidpythonrocks'
>>> segment('She said, "Python rocks!"')
['she', 'said', 'python', 'rocks']Sometimes its interesting to explore the unigram and bigram counts
themselves. These are stored in Python dictionaries mapping word to count.>>> import zh_segment as ws
>>> ws.load()
>>> ws.UNIGRAMS['the']
23135851162.0
>>> ws.UNIGRAMS['gray']
21424658.0
>>> ws.UNIGRAMS['grey']
18276942.0Above we see that the spellinggrayis more common than the spellinggrey.Bigrams are joined by a space:>>> import heapq
>>> from pprint import pprint
>>> from operator import itemgetter
>>> pprint(heapq.nlargest(10, ws.BIGRAMS.items(), itemgetter(1)))
[('of the', 2766332391.0),
 ('in the', 1628795324.0),
 ('to the', 1139248999.0),
 ('on the', 800328815.0),
 ('for the', 692874802.0),
 ('and the', 629726893.0),
 ('to be', 505148997.0),
 ('is a', 476718990.0),
 ('with the', 461331348.0),
 ('from the', 428303219.0)]Some bigrams begin with
. This is to indicate the start of a bigram:>>> ws.BIGRAMS['
 where']
15419048.0
>>> ws.BIGRAMS['
 what']
11779290.0The unigrams and bigrams data is stored in thezh_segment_datadirectory in
theunigrams.txtandbigrams.txtfiles respectively.Reference and Indiceszh_segment Documentationzh_segment at PyPIzh_segment at Githubzh_segment Issue Trackerzh_segment LicenseCopyright 2017 Z&HLicensed under the Apache License, Version 2.0 (the “License”);
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.