-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
56 lines (49 loc) · 1.47 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# parser.py
# Erica Portnoy
# Uses the Stanford parser to parse single sentences
#
# To avoid repeated long load times, can only handle
# single sentences.
#
# Usage: spawn_stanford_parser() to load
# sentence_parse(text) - text need not end in a newline
# exit_parser() to close
import os, sys, subprocess
def feed_to_parser(text):
hacky_text = text+'\nSorry.\n'
p.stdin.write(hacky_text.encode('UTF-8'))
# please excuse my hardcoding.
def spawn_stanford_parser():
sys.stdout.flush()
global p
p = subprocess.Popen('exec bash stanford-parser-full-2014-01-04/lexparser.sh -sentence newline -', shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
feed_to_parser("")
def exit_parser():
p.stdin.close()
sys.stdout.flush()
p.kill()
# will only parse the first sentence it receives
def sentence_parse(text):
p.stdout.flush()
feed_to_parser(text)
p.stdin.flush()
p.stdout.flush()
line = p.stdout.readline()
while line != "\n":
line = p.stdout.readline()
p.stdout.flush()
out = ""
line = p.stdout.readline()
while line != "\n":
out += line
line = p.stdout.readline()
if "Sorry" in out:
feed_to_parser("")
# we lose this one. it happens.
return out
def test():
spawn_stanford_parser()
print sentence_parse("This is a sample sentence. This is another.")
print sentence_parse("This is a third sentence.")
# Should print the parses of sample and third.
exit_parser()