1(****************************************************************)
2(* Unit Test for the LinkGrammar Ocaml API                      *)
3(* also doubles as an example of API usage                      *)
4(*                                                              *)
5(* Given a file name, with one sentence per line, parses all    *)
6(* sentences in the file -- it prints the linkage diagram, and  *)
7(* the constituent tree for each linkage                        *)
8(*                                                              *)
9(* The unit test is to run this on the file "4.0.batch"         *)
10(* 4.0.batch is a                                               *)
11(* file containing about 900 sentences that is part of the      *)
12(* linkgrammar distribution under "data" directory              *)
13(*                                                              *)
14(* Author: Ramu Ramamurthy ramu_ramamurthy at yahoo dot com     *)
15(* (C) 2006                                                     *)
16(*                                                              *)
17(* This is released under the BSD license                       *)
18(****************************************************************)
19
20open Linkgrammar;;
21
22(* LinkGrammar constituent tree algorithm has a bug
23   on the 3rd linkage on the following sentence in
24   4.0.batch
25*)
26let buggyStr = "This is the man whose dog I bought";;
27
28let printLinkages sentparse po =
29  let numLinkages = sentNumValidLinkages sentparse in
30  let () = Printf.printf "num of linkages = %d\n" numLinkages in
31    if numLinkages > 0 then
32      for i = 0 to (numLinkages-1)
33      do
34	let linkage = linkageCreate sentparse i po in
35	let () = linkageSetSublinkage linkage 0 in (
36	    Printf.printf "linkage %d is: %s\n" i (linkagePrintDiagram linkage);
37	    flush stdout;
38	    printConstituentTree linkage
39	  );
40      done
41;;
42
43let parseAndPrint sent po =
44  let numLinkages = sentParse sent po in
45  let () = Printf.printf "num of linkages = %d\n" numLinkages in
46  let () = flush stdout in
47    if numLinkages > 0 then
48      printLinkages sent po
49    else
50      let () = poSetMaxNullCount po (sentLength sent) in
51      let () = poSetMinNullCount po 1 in
52      let numLinkages = sentParse sent po in
53      let () = Printf.printf "num of linkages = %d\n" numLinkages in
54      let () = flush stdout in
55	if numLinkages > 0 then
56	  printLinkages sent po
57;;
58
59let parseFromFile dict po fname =
60  let in_c = open_in fname in
61    try
62      while true do
63	let str = input_line in_c in
64	  if (String.length str) > 0 then
65	    if str.[0] <> '!' && str <> buggyStr then
66	      (
67		let () = Printf.printf "---------- parsing %s\n" str in
68		let sent = sentCreate dict str in
69		  parseAndPrint sent po
70	      );
71      done
72    with x -> ()
73;;
74
75let po = poCreate ();;
76let () = poSetLinkageLimit po 1000;;
77
78let defaultDict = dictCreate ("en");;
79
80let () = Printf.printf "Enter input file:\n";;
81
82let inp = read_line ();;
83
84let () = parseFromFile defaultDict po inp;;
85