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.
getVersion()56     public static native String getVersion();
57 
58     //! Get the version string for the dictionary.
getDictVersion()59     public static native String getDictVersion();
60 
61     // C functions for changing linkparser options
setMaxParseSeconds(int maxParseSeconds)62     public static native void setMaxParseSeconds(int maxParseSeconds);
63 
setMaxCost(double maxCost)64     public static native void setMaxCost(double maxCost);
65 
setMaxLinkages(int maxLinkages)66     public static native void setMaxLinkages(int maxLinkages);
getMaxLinkages()67     public static native int getMaxLinkages();
68 
69     // Defaults to /usr/local/share/link-grammar/
setDictionariesPath(String path)70     public static native void setDictionariesPath(String path);
71 
setLanguage(String lang)72     public static native void setLanguage(String lang);
73 
74     // C functions in the linkparser API
init()75     public static native void init();
76 
parse(String sent)77     public static native void parse(String sent);
78 
close()79     public static native void close();
doFinalize()80     public static native void doFinalize();
81 
82     // C sentence access functions
getNumWords()83     public static native int getNumWords();
84 
85     // Get the subscripted form of the word.
getLinkageWord(int i)86     public static native String getLinkageWord(int i);
87 
88     // Get string representing the disjunct actually used.
getLinkageDisjunct(int i)89     public static native String getLinkageDisjunct(int i);
90 
getNumSkippedWords()91     public static native int getNumSkippedWords();
92 
93     // C linkage access functions
getNumLinkages()94     public static native int getNumLinkages();
95 
makeLinkage(int index)96     public static native void makeLinkage(int index);
97 
getLinkageNumViolations()98     public static native int getLinkageNumViolations();
99 
getLinkageDisjunctCost()100     public static native double getLinkageDisjunctCost();
101 
getLinkageLinkCost()102     public static native double getLinkageLinkCost();
103 
getNumLinks()104     public static native int getNumLinks();
105 
getLinkLWord(int link)106     public static native int getLinkLWord(int link);
107 
getLinkRWord(int link)108     public static native int getLinkRWord(int link);
109 
getLinkLLabel(int link)110     public static native String getLinkLLabel(int link);
111 
getLinkRLabel(int link)112     public static native String getLinkRLabel(int link);
113 
getLinkLabel(int link)114     public static native String getLinkLabel(int link);
115 
getConstituentString()116     public static native String getConstituentString();
117 
getLinkString()118     public static native String getLinkString();
119 }
120 
121