-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffaloparse.py
executable file
·51 lines (40 loc) · 1.33 KB
/
buffaloparse.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
import nltk
import sys
from nltk import CFG
def parse(string):
# Note that this grammar does not accept "Buffalo buffalo" as a
# verb phrase (meaning "to buffalo in a way unique to Buffalo"),
# as illustrated here: https://cse.buffalo.edu/~rapaport/buffalobuffalo.html
# Accepting this would make the final buffaloscript much easier to
# write in a way that guarantees any program is grammatical, but
# feels awkward in real English (Pinker agrees)
grammar_string = """
S -> V | VP
NP -> NP NP V | A N | N
VP -> NP V NP | NP V
V -> 'buffalo'
N -> 'buffalo'
A -> 'Buffalo'
"""
grammar = CFG.fromstring(grammar_string)
parser = nltk.ChartParser(grammar)
parsed = parser.parse(string.split())
return parsed
def parses(string):
if len(list(parse(string))) > 0:
return True
return False
if __name__ == "__main__":
if len(sys.argv) == 2:
if sys.argv[1][-4:] != '.buf':
filepath = str(sys.argv[1]) + '.buf'
else:
filepath = str(sys.argv[1])
with open(filepath, 'r') as f:
string = f.read()
elif len(sys.argv) == 1:
string = sys.stdin.read()
else:
print(f'usage: {sys.argv[0]} <src>\nor cat <src.buf> | {sys.argv[0]}')
sys.exit(1)
sys.stdout.write(str(parse(string)))