forked from mark-watson/Java-AI-Book-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerLoomUtils.java
More file actions
executable file
·105 lines (96 loc) · 3.04 KB
/
PowerLoomUtils.java
File metadata and controls
executable file
·105 lines (96 loc) · 3.04 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
package powerloom;
import edu.isi.powerloom.PLI;
import edu.isi.powerloom.PlIterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* PowerLoom Java utilities
*
* <p/>
* Copyright 1998-2012 by Mark Watson. All rights reserved.
* <p/>
* This software is can be used under either of the following licenses:
* <p/>
* 1. LGPL v3<br/>
* 2. Apache 2
* <p/>
*/public class PowerLoomUtils {
public PowerLoomUtils() {
System.out.print("Initializing...");
PLI.initialize();
System.out.println(" done.");
}
public int load(String fpath) {
try {
PLI.load(fpath, null);
return 1;
} catch (Exception ex) {
System.out.println("Error loading " + fpath + " is: " + ex);
return 0;
}
}
public int changeModule(String workingModule) {
try {
PLI.sChangeModule(workingModule, null);
this.workingModule = workingModule;
return 1;
} catch (Exception ex) {
System.out.println("Error changing module " + workingModule + " is: " + ex);
return 0;
}
}
public int assertProposition(String proposition) {
proposition = proposition.replace('\'', '\"');
try {
PLI.sAssertProposition(proposition, workingModule, null);
return 1;
} catch (Exception ex) {
System.out.println("Error asserting proposition '" + proposition + "' is: " + ex);
return 0;
}
}
public int createRelation(String relation, int arity) {
try {
PLI.sCreateRelation(relation, arity, workingModule, null);
return 1;
} catch (Exception ex) {
System.out.println("Error creating relation '" + relation + "' with arity=" + arity + " is: " + ex);
return 0;
}
}
private String fix(Object obj) {
String s = ""+obj;
return s.substring(3);
}
// following method donated by Thomas Russ:
public List doQuery(String query) {
ArrayList ret = null;
try {
PlIterator rawAnswer = PLI.sRetrieve(query, workingModule, null);
ret = new ArrayList<ArrayList<String>>(rawAnswer.length());
Iterator answer =
new edu.isi.stella.javalib.StellaIterator(rawAnswer);
while (answer.hasNext()) {
Object obj = answer.next();
int nItems = PLI.getColumnCount((edu.isi.stella.Stella_Object)obj);
java.util.ArrayList r2 = new java.util.ArrayList(nItems);
for (int i = 0; i < nItems; i++) {
r2.add(fix(PLI.getNthValue((edu.isi.stella.Stella_Object)obj, i, null, null)));
}
ret.add(r2);
}
} catch (Exception ex) {
System.out.println("Error with query '" + query + "': " + ex);
ret = new java.util.ArrayList(0);
}
return ret;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
private String workingModule = "PL-USER";
}