Debian Codegraph @

bindings/java/org/linkgrammar/LinkGrammar.java

raw
1 /*************************************************************************/
2 /* */
3 /* Use of the link grammar parsing system is subject to the terms of the */
4 /* license set forth in the LICENSE file included with this software. */
5 /* This license allows free redistribution and use in source and binary */
6 /* forms, with or without modification, subject to certain conditions. */
7 /* */
8 /*************************************************************************/
9
10 package org.linkgrammar;
11
12 /**
13 * This class serves as a wrapper to the C Link Grammar Parser library.
14 * It provides a simple public Java API to the equivalent public C API.
15 *
16 * Unfortunately, this class is not at all OOP in style; it operates on
17 * the single, current sentence and linkage. This could be improved.
18 */
19
20 public class LinkGrammar
21 {
22 static
23 {
24 // On a Linux system, the actual name of the library
25 // is prefixed with "lib" and suffixed with ".so"
26 // -- e.g. "liblink-grammar-java.so"
27 // Windows looks for "link-grammar-java.dll"
28 // MacOS looks for "liblink-grammar-java.dylib"
29 //
30 // On a Windows system, we also need to load the prerequisite
31 // libraries first. (Linux loaders do this automatically).
32 // Actually, I guess Windows does this too, unless the user
33 // failed to add the working directory to %PATH
34 //
35 try
36 {
37 String osname = System.getProperty("os.name");
38 if (osname.indexOf("win") > -1 || osname.indexOf("Win") > -1)
39 {
40 System.loadLibrary("link-grammar");
41 }
42 // if (osname.indexOf("Mac OS X") > -1) {}
43 System.loadLibrary("link-grammar-java");
44 }
45 catch (Throwable ex)
46 {
47 // If we don't catch here, then the thread pool will
48 // silently eat the exception and make us into fools.
49 // https://www.securecoding.cert.org/confluence/display/java/TPS03-J.+Ensure+that+tasks+executing+in+a+thread+pool+do+not+fail+silently
50 System.err.println(ex);
51 System.exit(-1);
52 }
53 }
54
55 //! Get the version string for the parser.
56 public static native String getVersion();
57
58 //! Get the version string for the dictionary.
59 public static native String getDictVersion();
60
61 // C functions for changing linkparser options
62 public static native void setMaxParseSeconds(int maxParseSeconds);
63
64 public static native void setMaxCost(double maxCost);
65
66 public static native void setMaxLinkages(int maxLinkages);
67 public static native int getMaxLinkages();
68
69 // Defaults to /usr/local/share/link-grammar/
70 public static native void setDictionariesPath(String path);
71
72 public static native void setLanguage(String lang);
73
74 // C functions in the linkparser API
75 public static native void init();
76
77 public static native void parse(String sent);
78
79 public static native void close();
80 public static native void doFinalize();
81
82 // C sentence access functions
83 public static native int getNumWords();
84
85 // Get the subscripted form of the word.
86 public static native String getLinkageWord(int i);
87
88 // Get string representing the disjunct actually used.
89 public static native String getLinkageDisjunct(int i);
90
91 public static native int getNumSkippedWords();
92
93 // C linkage access functions
94 public static native int getNumLinkages();
95
96 public static native void makeLinkage(int index);
97
98 public static native int getLinkageNumViolations();
99
100 public static native double getLinkageDisjunctCost();
101
102 public static native double getLinkageLinkCost();
103
104 public static native int getNumLinks();
105
106 public static native int getLinkLWord(int link);
107
108 public static native int getLinkRWord(int link);
109
110 public static native String getLinkLLabel(int link);
111
112 public static native String getLinkRLabel(int link);
113
114 public static native String getLinkLabel(int link);
115
116 public static native String getConstituentString();
117
118 public static native String getLinkString();
119 }
120