SkladnicaXmlToOcaml.ml
6.97 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
open SkladnicaTypes
let i_o_s x =
try int_of_string x with _ -> failwith x
let to_ocaml_startnode = function
Xml.Element("startnode",[("from",from);("to",sto)],[Xml.PCData text]) ->
{from = i_o_s from; sto = i_o_s sto; text = text}
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_stats = function
Xml.Element("stats",["trees",trees;"nodes",nodes;"inferences",inferences;"cputime",cputime],[]) ->
{trees = trees;
nodes = i_o_s nodes;
inferences = inferences;
cputime = float_of_string cputime}
| Xml.Element("stats",["trees",trees;"inferences",inferences;"cputime",cputime],[]) ->
{trees = trees;
nodes = 0;
inferences = inferences;
cputime = float_of_string cputime}
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_comment = function
Xml.Element("comment",[],[Xml.PCData text]) -> text
| Xml.Element("comment",[],[]) -> ""
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_type = function
"FULL" -> FULL
| "MORPH" -> MORPH
| "NOT_SENTENCE" -> NOT_SENTENCE
| "NO_TREE" -> NO_TREE
| "TOO_DIFFICULT" -> TOO_DIFFICULT
| "WRONG_SENTENCE" -> WRONG_SENTENCE
| text -> failwith text
let to_ocaml_base_answer = function
Xml.Element("base-answer",["type",stype;"username",username],[comment]) ->
{tree_type = to_ocaml_type stype; username = username; comment = to_ocaml_comment comment}
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_extra_answer = function
Xml.Element("extra-answer",["type",stype;"username",username],[comment]) ->
{tree_type = to_ocaml_type stype; username = username; comment = to_ocaml_comment comment}
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_answer_data = function
Xml.Element("answer-data",[],[base;extra1;extra2]) ->
begin
{ base_answer = to_ocaml_base_answer base;
extra_answers = (to_ocaml_extra_answer extra1, to_ocaml_extra_answer extra2) }
end
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_category = function
Xml.Element("category",[],[Xml.PCData text]) -> text
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_f = function
Xml.Element("f",["type",stype],[Xml.PCData text]) -> { tag = stype; text = text }
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_child = function
Xml.Element("child",["nid",nid;"from",from;"to",sto;"head",head],[]) ->
{nid = i_o_s nid;
from = i_o_s from;
sto = i_o_s sto;
head = bool_of_string head}
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_children = function
Xml.Element("children",["rule",rule],children) ->
ChoiceUndef (rule, List.fold_right (fun h a -> to_ocaml_child h :: a) children [])
| Xml.Element("children",["rule",rule;"chosen",chosen],children) ->
ChoiceDef (rule, bool_of_string chosen, List.fold_right (fun h a -> to_ocaml_child h :: a) children [])
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_base = function
Xml.Element("base",[],[Xml.PCData text]) -> text
| Xml.Element("base",[],[]) -> ""
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_orth = function
Xml.Element("orth",[],[Xml.PCData text]) -> text
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_nonterminal = function
Xml.Element("nonterminal",[],category::fs) ->
begin
to_ocaml_category category,
List.fold_right (fun h a -> to_ocaml_f h :: a) fs []
end
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_terminal = function
Xml.Element("terminal",["token_id",token_id;"interp_id",interp_id;"disamb",disamb;"nps",nps],orth::base::[f]) ->
begin
{token_id = token_id; interp_id = interp_id;
disamb = bool_of_string disamb; nps = bool_of_string nps},
to_ocaml_orth orth,
to_ocaml_base base,
to_ocaml_f f
end
| Xml.Element("terminal",["token_id",token_id;"interp_id",interp_id;"disamb",disamb],orth::base::[f]) ->
begin
{token_id = token_id; interp_id = interp_id;
disamb = bool_of_string disamb; nps = false},
to_ocaml_orth orth,
to_ocaml_base base,
to_ocaml_f f
end
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_node = function
Xml.Element("node",["nid",nid;"from",from;"to",sto;"subtrees",subtrees;"chosen",chosen],nonterminal::first_child::children) ->
Nonterminal ( {nid = i_o_s nid;
from = i_o_s from;
sto = i_o_s sto;
subtrees = subtrees;
chosen = bool_of_string chosen},
fst (to_ocaml_nonterminal nonterminal),
snd (to_ocaml_nonterminal nonterminal),
List.fold_right (fun h a -> to_ocaml_children h :: a) (first_child::children) [])
| Xml.Element("node",["nid",nid;"from",from;"to",sto;"subtrees",subtrees;"chosen",chosen],[terminal]) ->
begin
let a, b, c, d = to_ocaml_terminal terminal in
Terminal ( {nid = i_o_s nid;
from = i_o_s from;
sto = i_o_s sto;
subtrees = subtrees;
chosen = bool_of_string chosen},
a,b,c,d )
end
| Xml.Element("node",["nid",nid;"from",from;"to",sto;"subtrees",subtrees],nonterminal::first_child::children) ->
Nonterminal ( {nid = i_o_s nid;
from = i_o_s from;
sto = i_o_s sto;
subtrees = subtrees;
chosen = false},
fst (to_ocaml_nonterminal nonterminal),
snd (to_ocaml_nonterminal nonterminal),
List.fold_right (fun h a -> to_ocaml_children h :: a) (first_child::children) [])
| Xml.Element("node",["nid",nid;"from",from;"to",sto;"subtrees",subtrees],[terminal]) ->
begin
let a, b, c, d = to_ocaml_terminal terminal in
Terminal ( {nid = i_o_s nid;
from = i_o_s from;
sto = i_o_s sto;
subtrees = subtrees;
chosen = false},
a,b,c,d )
end
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")
let to_ocaml_forest = function
Xml.Element("forest",["sent_id",sent_id;"grammar_no",grammar_no],
Xml.Element("text",[],[Xml.PCData text]) ::
startnode :: stats :: answer_data :: nodes) ->
Forest ( {sent_id = sent_id; grammar_no = grammar_no}, text,
to_ocaml_startnode startnode,
to_ocaml_stats stats,
to_ocaml_answer_data answer_data,
List.fold_right (fun h a -> to_ocaml_node h :: a) nodes [])
| Xml.Element("forest",["sent_id",sent_id;"grammar_no",grammar_no],
Xml.Element("text",[],[Xml.PCData text]) ::
stats :: [answer_data]) ->
NoTree ( {sent_id = sent_id; grammar_no = grammar_no}, text,
to_ocaml_stats stats,
to_ocaml_answer_data answer_data )
| xml -> (print_endline (Xml.to_string_fmt xml); failwith "Error")