semRewrite.InterpTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sigma-nlp Show documentation
Show all versions of sigma-nlp Show documentation
Natural language processing toolbox using Sigma knowledge engineering system.
package semRewrite;
import com.articulate.sigma.Formula;
import com.articulate.sigma.KBmanager;
import semRewrite.Interpreter;
import com.google.common.collect.Lists;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Function;
/*
Copyright 2014-2015 Articulate Software
Author: Adam Pease [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program ; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
*/
public class InterpTest {
public static Interpreter interp;
public static int pass = 0;
public static int fail = 0;
public static int subsumed = 0;
public static ArrayList> results = new ArrayList>();
/** **************************************************************
*/
public static Collection transform(String resourcePath, Function transformer) {
Collection result = Lists.newArrayList();
try {
FileReader fr = new FileReader(new File(resourcePath));
System.out.println("Reading JSON file: " + resourcePath);
JSONParser parser = new JSONParser();
Object obj = parser.parse(fr);
JSONArray jsonObject = (JSONArray) obj;
ListIterator li = jsonObject.listIterator();
while (li.hasNext()) {
JSONObject jo = li.next();
result.add(transformer.apply(jo));
}
}
catch (Exception e) {
System.out.println("Parse exception reading: " + resourcePath);
System.out.println(e.getMessage());
e.printStackTrace();
throw new RuntimeException("Parse exception reading: " + resourcePath);
}
return result;
}
/****************************************************************
*/
public static void initInterpreter() throws IOException {
interp = new Interpreter();
KBmanager.getMgr().initializeOnce();
interp.initialize();
}
/****************************************************************
*/
public static Collection