-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
141 lines (133 loc) · 4.36 KB
/
index.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* Created by PhpStorm.
* User: clicker
* Date: 25/04/17
* Time: 18:42
*/
require __DIR__ . '/vendor/autoload.php';
$grammatical_relations = array(
'acl' => 'clausal modifier of noun',
'acl:relcl' => 'relative clause modifier',
'advcl' => 'adverbial clause modifier',
'advmod' => 'adverbial modifier',
'amod' => 'adjectival modifier',
'appos' => 'appositional modifier',
'aux' => 'auxiliary',
'auxpass' => 'passive auxiliary',
'case' => 'case marking',
'cc' => 'coordination',
'cc:preconj' => 'preconjunct',
'ccomp' => 'clausal complement',
'compound' => 'compound',
'compound:prt' => 'phrasal verb particle',
'conj' => 'conjunct',
'cop' => 'copula',
'csubj' => 'clausal subject',
'csubjpass' => 'clausal passive subject',
'dep' => 'dependent',
'det' => 'determiner',
'det:predet' => 'predeterminer',
'discourse' => 'discourse element',
'dislocated' => 'dislocated elements',
'dobj' => 'direct object',
'expl' => 'expletive',
'foreign' => 'foreign words',
'goeswith' => 'goes with',
'iobj' => 'indirect object',
'list' => 'list',
'mark' => 'marker',
'mwe' => 'multi-word expression',
'name' => 'name',
'neg' => 'negation modifier',
'nmod' => 'nominal modifier',
'nmod:npmod' => 'noun phrase as adverbial modifier',
'nmod:poss' => 'possessive nominal modifier',
'nmod:tmod' => 'temporal modifier',
'nsubj' => 'nominal subject',
'nsubjpass' => 'passive nominal subject',
'nummod' => 'numeric modifier',
'parataxis' => 'parataxis',
'punct' => 'punctuation',
'remnant' => 'remnant in ellipsis',
'reparandum' => 'overridden disfluency',
'root' => 'root',
'vocative' => 'vocative',
'xcomp' => 'open clausal complement'
);
$penn_treebank = array(
// POS
'CC' => 'conjunction, coordinating',
'CD' => 'cardinal number',
'DT' => 'determiner',
'EX' => 'existential there',
'FW' => 'foreign word',
'IN' => 'conjunction, subordinating or preposition',
'JJ' => 'adjective',
'JJR' => 'adjective, comparative',
'JJS' => 'adjective, superlative',
'LS' => 'list item marker',
'MD' => 'verb, modal auxillary',
'NN' => 'noun, singular or mass',
'NNS' => 'noun, plural',
'NNP' => 'noun, proper singular',
'NNPS' => 'noun, proper plural',
'PDT' => 'predeterminer',
'POS' => 'possessive ending',
'PRP' => 'pronoun, personal',
'PRP$' => 'pronoun, possessive',
'RB' => 'adverb',
'RBR' => 'adverb, comparative',
'RBS' => 'adverb, superlative',
'RP' => 'adverb, particle',
'SYM' => 'symbol',
'TO' => 'infinitival to',
'UH' => 'interjection',
'VB' => 'verb, base form',
'VBZ' => 'verb, 3rd person singular present',
'VBP' => 'verb, non-3rd person singular present',
'VBD' => 'verb, past tense',
'VBN' => 'verb, past participle',
'VBG' => 'verb, gerund or present participle',
'WDT' => 'wh-determiner',
'WP' => 'wh-pronoun, personal',
'WP$' => 'wh-pronoun, possessive',
'WRB' => 'wh-adverb',
'.' => 'punctuation mark, sentence closer',
',' => 'punctuation mark, comma',
':' => 'punctuation mark, colon',
'(' => 'contextual separator, left paren',
')' => 'contextual separator, right paren',
// phrases
'NP' => 'noun phrase',
'PP' => 'prepositional phrase',
'VP' => 'verb phrase',
'ADVP' => 'adverb phrase',
'ADJP' => 'adjective phrase',
'SBAR' => 'subordinating conjunction',
'PRT' => 'particle',
'INTJ' => 'interjection'
);
// param
$cwd = getcwd();
$sentence = $_GET['q'];
$raw = $_GET['raw'];
$parser = new StanfordNLP\Parser($cwd . '/stanford-parser-full-2016-10-31/stanford-parser.jar', $cwd . '/stanford-parser-full-2016-10-31/stanford-parser-3.7.0-models.jar');
$result = $parser->parseSentence($sentence);
$explanation = array();
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($result));
foreach ($iterator as $key => $value) {
if (array_key_exists($value, $grammatical_relations)) {
$explanation[$value] = $grammatical_relations[$value];
}
if (array_key_exists($value, $penn_treebank)) {
$explanation[$value] = $penn_treebank[$value];
}
}
$result['legend'] = $explanation;
$result['query'] = $sentence;
if ($raw === 'true') {
echo $parser->getOutput();
} else {
echo json_encode($result, JSON_PRETTY_PRINT);
}