File tree Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Expand file tree Collapse file tree 3 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,24 @@ out-file. Input and output default to stdin and stdout respectively. ::
71
71
$ echo thisisatest | python -m wordsegment
72
72
this is a test
73
73
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
+
74
92
The maximum segmented word length is 24 characters. Neither the unigram nor
75
93
bigram data contain words exceeding that length. The corpus also excludes
76
94
punctuation and all letters have been lowercased. Before segmenting text,
Original file line number Diff line number Diff line change @@ -199,9 +199,10 @@ def main(arguments=()):
199
199
default = sys .stdout )
200
200
201
201
streams = parser .parse_args (arguments )
202
+ load ()
202
203
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 () )))
205
206
streams .outfile .write (os .linesep )
206
207
207
208
Original file line number Diff line number Diff line change
1
+ import sys
2
+ from . import main
3
+
4
+ main (sys .argv [1 :])
You can’t perform that action at this time.
0 commit comments