Skip to content
Snippets Groups Projects
Commit c5b89a54 authored by anirudhash's avatar anirudhash
Browse files

Annotator class

parent b8721726
No related branches found
No related tags found
No related merge requests found
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
//import net.guides.springboot.springbootasyncexample.model.User;
public class Annotator {
public String getAnnotations(String input) throws IOException {
String endpoint = "https://labs.tib.eu/falcon/api?mode=long";
String endpoint1 = "https://labs.tib.eu/falcon/falcon2/api?mode=long";
ArrayList<String> realArr = new ArrayList<>();
String res = "";
Implementation impl = new Implementation();
// curl --header "Content-Type: application/json" \
// --request POST \
// --data '{"text":"Who painted The Storm on the Sea of Galilee?"}' \
//Curl to array
String[] command = {"curl", "--header", "Content-Type:application/json", "--request", "POST", "--data", "{" + "\"text\"" + ":" +
"\"" + input + "\"" + "}", endpoint1};
Process p;
try {
p = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(System.getProperty("line.separator"));
}
// String result = builder.toString();
JSONObject jsonObject = new JSONObject(builder.toString());
JSONArray relations = null;
JSONArray relsrrsy = null;
String y = "";
relations = jsonObject.getJSONArray("relations_wikidata");
if(relations.length() != 0)
{
relsrrsy = relations.getJSONArray(0);
int i = 0;
//rrayList<String> rel = (ArrayList<String>) relations.get(0);
String x = relsrrsy.get(1).toString();
JSONObject jsonObject1 = new JSONObject(builder.toString());
JSONArray relations1 = jsonObject.getJSONArray("entities_wikidata");
for(int j=1; j<relations1.length(); j++)
{
JSONArray relsrrsy1 = relations1.getJSONArray(j);
String strrel = relsrrsy1.get(1).toString();
realArr.add(strrel);
}
//int i = 0;
JSONArray relsrrsy1 = relations1.getJSONArray(0);
//rrayList<String> rel = (ArrayList<String>) relations.get(0);
y = relsrrsy1.get(1).toString();
}
//String c = jsonObject.getString("")
if(!y.equals(""))
{
String[] arrStr = y.split(" ");
String newStr = "";
final StringBuilder ret = new StringBuilder(y.length());
for (final String word : y.split(" ")) {
if (!word.isEmpty()) {
ret.append(Character.toUpperCase(word.charAt(0)));
ret.append(word.substring(1).toLowerCase());
}
if (!(ret.length() == y.length()))
ret.append(" ");
}
y = ret.toString();
String url = impl.getURL(y);
String content = impl.getPageContent(url);
for(int a=0; a<realArr.size(); a++)
{
String out[] = content.split("\\n");
content = ifKeyword(out, relations.toString());
if(content.contains(realArr.get(a)))
{
res = "true";
}
else {
res = "false";
}
}
}
if (res.equals(""))
{
res = "false";
}
// jsonObject.
} catch (IOException e) {
System.out.print("error");
e.printStackTrace();
}
return res;
}
private String ifKeyword(String[] out, String toString) {
String x[] = toString.split(",");
String in = x[1].replaceAll("[^aA-zZ]" , "");
if(in.contains("]"))
{
in = in.replace("]", "");
}
String rtr = "";
for(int i = 0; i< out.length; i++)
{
if(out[i].contains(in))
{
rtr = rtr + out[i];
}
}
return rtr;
}
// private List<String> parseJsonStream(InputStream in) throws IOException, ParseException {
// List<String> annotations = new ArrayList<>();
// JSONArray namedEntities = (JSONArray) this.jsonParser.parse(new InputStreamReader(in, "UTF-8"));
// JSONObject namedEntity;
// String url;
// long start, length;
// for (Object obj : namedEntities) {
// namedEntity = (JSONObject) obj;
// start = (long) namedEntity.get("start");
// length = (long) namedEntity.get("offset");
// url = (String) namedEntity.get("disambiguatedURL");
// }
// return annotations;
// }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment