Skip to content

Commit 588f4d4

Browse files
committed
Add support for server use
1 parent d3aceef commit 588f4d4

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ out-file. Input and output default to stdin and stdout respectively. ::
7171
$ echo thisisatest | python -m wordsegment
7272
this is a test
7373

74+
If you want to run `WordSegment`_ as a kind of server process then use Python's
75+
``-u`` option for unbuffered output. You can also set ``PYTHONUNBUFFERED=1`` in
76+
the environment. ::
77+
78+
>>> import subprocess as sp
79+
>>> wordsegment = sp.Popen(
80+
['python', '-um', 'wordsegment'],
81+
stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.STDOUT)
82+
>>> wordsegment.stdin.write('thisisatest\n')
83+
>>> wordsegment.stdout.readline()
84+
'this is a test\n'
85+
>>> wordsegment.stdin.write('workswithotherlanguages\n')
86+
>>> wordsegment.stdout.readline()
87+
'works with other languages\n'
88+
>>> wordsegment.stdin.close()
89+
>>> wordsegment.wait() # Process exit code.
90+
0
91+
7492
The maximum segmented word length is 24 characters. Neither the unigram nor
7593
bigram data contain words exceeding that length. The corpus also excludes
7694
punctuation and all letters have been lowercased. Before segmenting text,

wordsegment/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,10 @@ def main(arguments=()):
199199
default=sys.stdout)
200200

201201
streams = parser.parse_args(arguments)
202+
load()
202203

203-
for line in streams.infile:
204-
streams.outfile.write(' '.join(segment(line)))
204+
for line in iter(streams.infile.readline, ''):
205+
streams.outfile.write(' '.join(segment(line.strip())))
205206
streams.outfile.write(os.linesep)
206207

207208

wordsegment/__main__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sys
2+
from . import main
3+
4+
main(sys.argv[1:])

0 commit comments

Comments
 (0)