Use dkpro semantic similarity with uby - java

I want to compute similarity between strings with dkpro similarity (https://dkpro.github.io/dkpro-similarity/), it works, like so:
import org.dkpro.similarity.algorithms.api.SimilarityException;
import org.dkpro.similarity.algorithms.api.TextSimilarityMeasure;
import org.dkpro.similarity.algorithms.lsr.LexSemResourceComparator;
import org.dkpro.similarity.algorithms.lsr.gloss.GlossOverlapComparator;
import org.dkpro.similarity.algorithms.lsr.path.JiangConrathComparator;
import org.dkpro.similarity.algorithms.lsr.path.LeacockChodorowComparator;
import org.dkpro.similarity.algorithms.lsr.path.LinComparator;
import org.dkpro.similarity.algorithms.lsr.path.ResnikComparator;
import org.dkpro.similarity.algorithms.lsr.path.WuPalmerComparator;
import de.tudarmstadt.ukp.dkpro.lexsemresource.LexicalSemanticResource;
import de.tudarmstadt.ukp.dkpro.lexsemresource.core.ResourceFactory;
import de.tudarmstadt.ukp.dkpro.lexsemresource.exception.LexicalSemanticResourceException;
import de.tudarmstadt.ukp.dkpro.lexsemresource.exception.ResourceLoaderException;
import learninggoals.analysis.controller.settingtypes.SimilarityAlgorithm;
public class SemResourceComparator implements WordsComparator{
private LexicalSemanticResource resource;
private LexSemResourceComparator comparator;
//en lang
public SemResourceComparator(String resourcetype, SimilarityAlgorithm algorithm, String lang) {
try {
resource = ResourceFactory.getInstance().get(resourcetype, lang);
} catch (ResourceLoaderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
switch(algorithm){
/*case ESA://this is vector
comparator = new GlossOverlapComparator(resource, false);
break;*/
case GLOSSOVERLAP:
comparator = new GlossOverlapComparator(resource, false);
break;
case JIANG_CONRATH:
comparator = new JiangConrathComparator(resource, resource.getRoot());
break;
case LEACOCK_CHODOROW:
comparator = new LeacockChodorowComparator(resource);
break;
case LIN:
comparator = new LinComparator(resource, resource.getRoot());
break;
case RESNIK:
comparator = new ResnikComparator(resource, resource.getRoot());
break;
case WUPALMER:
comparator = new WuPalmerComparator(resource, resource.getRoot());
break;
default:
break;
}
} catch (LexicalSemanticResourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public double compareWords(String w1, String w2) {
try {
return comparator.getSimilarity(resource.getEntity(w1), resource.getEntity(w2));
} catch (SimilarityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LexicalSemanticResourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
i use the class like this:
double intermscore = comparator.compareWords(word1, word2);
I use LexicalSemanticResource as resource for comparing, it can be wordnet, wikipedia, germanet etc. Now i noticed that all of the resources i need are in uby (https://www.ukp.tu-darmstadt.de/data/lexical-resources/uby/, https://github.com/dkpro/dkpro-uby/blob/master/de.tudarmstadt.ukp.uby.lmf.api-asl/src/main/java/de/tudarmstadt/ukp/lmf/api/Uby.java).
My question is: can i replace the resource with a resource from uby so I don't have to include a new resource again each time i need one? so instead of ResourceFactory.getInstance().get("wordnet"), i want to use the uby resource, so sth like new Uby().getLexicalResource("wordnet") - however lexicalresource from uby is not the same as LexicalSemanticResource i use now for semantic comparison. So: Instead of using e.g. LexicalSemanticResource wordnet, i want to use wordnet from uby for the comparators. Is there a way to do this?

There currently is no way to do this. Uby resources and LSR resources are not compatible.
There were plans to switch, but the issue has been open for a while:
https://github.com/dkpro/dkpro-similarity/issues/39

Related

codename1 localization don't work

I have made a new resourcebundle in the theme.res. I have 2 languages (en, da).
I've written this code:
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
String lang = L10NManager.getInstance().getLanguage();
try {
if (lang != null) {
lang = lang.toLowerCase();
switch (lang) {
case "da":
Map<String, String> localMap = theme.getL10N("local", "da");
UIManager.createInstance().setBundle(localMap);
System.out.println("Entries: " + localMap.size());
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I run the code it does get the bundle and the localMap does keep the 'da' landuage entries as it should.
But nothing happens. The GUI texts are just the keys.
Is there something I miss here?
This is wrong:
UIManager.createInstance().setBundle(localMap);
You should have used:
UIManager.getInstance().setBundle(localMap);

Mismatch of return datatype

i am facing a problem regrading specifying the return data type. I have the FOComp class which implements callabale, the call() method of the 'FOComp' returns data type List<ArrayList<Mat>> as shown in the code of 'FOComp' class below.
and the method 'getResults()' returns data of type ArrayList<Mat> as shown in the code below. and currently, at run time, when I execute the code, I receive the folowing error:
Multiple markers at this line
The return type is incompatible with Callable<ArrayList<Mat>>.call()
The return type is incompatible with Callable<List<Mat>>.call()
kindly please let me know how to fix it.
'FOComp' class:
static class FOComp implements Callable<List<Mat>> {//should return list contains 4 mats(0,45,90,135)
private ArrayList<Mat> gaussianMatList = null;
private List<ArrayList<Mat>> results_4OrientAngles_List = null;
public FOComp(ArrayList<Mat> gaussianMatList) {
// TODO Auto-generated constructor stub
this.gaussianMatList = gaussianMatList;
this.results_4OrientAngles_List = new ArrayList<ArrayList<Mat>>();
}
public List<ArrayList<Mat>> call() throws Exception {
// TODO Auto-generated method stub
try {
featOrient = new FeatOrientation(this.gaussianMatList);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
featOrient.start();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.results_4OrientAngles_List.add(featOrient.getResults());
return results_4OrientAngles_List;
}
}
'getResults':
public ArrayList<Mat> getResults() {
if (this.crossAddOrientMapsList != null) {
if (!this.crossAddOrientMapsList.isEmpty()) {
if (this.crossAddOrientMapsList.size() == 4) {
double[] theta = new double[4];
theta[0] = 0;
theta[1] = 45;
theta[2] = 90;
theta[3] = 135;
for (int i = 0; i < this.crossAddOrientMapsList.size(); i++) {
MatFactory.writeMat(FilePathUtils.newOutputPath("FinalCrossAdd_" + theta[i]+"_degs"), this.crossAddOrientMapsList.get(i));
//ImageUtils.showMat(this.crossAddOrientMapsList.get(i), "OrientMap_" + theta[i] + " degs");
}
return this.crossAddOrientMapsList;
} else {
Log.WTF(TAG, "getResults", "crossAddOrientMapsList != 4 !!");
return null;
}
} else {
Log.E(TAG, "getResults", "crossAddOrientMapsList is empty.");
return null;
}
} else {
Log.E(TAG, "getResults", "crossAddOrientMapsList is null");
return null;
}
}
class FOComp implements Callable<List<Mat>>
and
public List<ArrayList<Mat>> call()
aren't really compatible... Your call() method should be
#Override public List<Mat> call()
Also, it is good practice to avoid implementation classes in method signatures, use the interfaces instead (in this case, use List rather than ArrayList). That will also fix your problem with one of the "multiple markers" :-)
Cheers,
You class declaration says that you are going to return a List of Mat (FOComp implements Callable<List<Mat>>), but your call method signature says you are going to return a List of ArrayList of Mat (List<ArrayList<Mat>>).
You will need to make them consistent.

Cannot Start a Method In a Different Class Using a Library

I've just created my first library for an Android app I've built (I have code I need to reuse in the future across different apps) and I need to start a method I have in the main project from the library - however when I attempt to do so using the line:
com.project.sample.datasettings.UpdateActivity.success();
I'm getting a compiler error stating:
com.project.sample.UpdateActivity Cannot Be Resolved To A Type
SOURCE:
package com.project.sample.networktasklibrary;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException;
import com.project.sample.networktasklibrary.XmlParserHandlerFinal;
import com.project.sample*;
import org.xml.sax.SAXException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
// this class performs the call to webservice in the background
public class NetworkTask extends AsyncTask<String, String, InputStream> {
private static final String LOG_TAG = "STDataSettings";
private static final String TAG_RESULT = "success";
private static InputStream stream;
#Override
protected InputStream doInBackground(String... params) {
try {
stream = getQueryResults("https://dl.dropboxusercontent.com/u/31771876/GetPhoneSettings-ST-rsp-eng.xml");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stream;
}
/*
* Sends a query to server and gets back the parsed results in a bundle
* urlQueryString - URL for calling the webservice
*/
protected static synchronized InputStream getQueryResults(
String urlQueryString) throws IOException, SAXException,
SSLException, SocketTimeoutException, Exception {
Bundle queryResults = new Bundle();
HttpsURLConnection https = null;
String uri = urlQueryString;
URL urlo = new URL(uri);
https = (HttpsURLConnection) urlo.openConnection();
https.setConnectTimeout(50000); // 20 second timeout
https.setRequestProperty("Connection", "Keep-Alive");
try {
https = (HttpsURLConnection) urlo.openConnection();
if ("gzip".equals(https.getContentEncoding())) {
stream = new GZIPInputStream(stream);
} else
stream = https.getInputStream();
} catch (SSLException e) {
Log.e(LOG_TAG, e.toString());
e.printStackTrace();
} catch (SocketTimeoutException e) {
Log.e(LOG_TAG, e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e(LOG_TAG, e.toString());
e.printStackTrace();
} catch (Exception e) {
Log.e(LOG_TAG, e.toString());
e.printStackTrace();
} finally {
}
String queryResult = null;
queryResults.putString(TAG_RESULT, queryResult);
return stream;
}
public InputStream getInputStream() {
return stream;
}
protected void onPostExecute(InputStream queryResults) {
// TODO Auto-generated method stub
super.onPostExecute(queryResults);
com.project.sample.datasettings.UpdateActivity.success();
}
}
UPDATE ACTIVITY CODE SAMPLE:
public void success() {
// to parse the response
try {
handler.getQueryResponse(stream);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// to set method to save the ArryaLists from the parser
setArrayList();
Intent i = new Intent(this, ConfigFinalActivity.class);
startActivity(i);
}
Have you ever tried to add the library to the main project?
Right click the Project name -> Property -> Android -> Add. Then choose the library project.

Dropping object between classes

I have a stream of objects coming from a server to a client. There is a networking class within the client which handles taking these objects. There is then an activity which uses a get method to get this object as it is updated from the server (both client classes are constantly updating) the objects also have a position field within them which determines if they are 1 (first), 2 (middle), or 3 (end) of the current stream of data.
My problem is that using LogCat to print these I get the first object with position 1, within the networking class, but the activity misses this, and displays several 2's and one 3. So it is somehow loosing my first object.
My client network has the following relevant code:
public class ClientNetwork implements Runnable {
Drawring serverDraw;
public Drawring GetServerDraw(){
Drawring drawPass = serverDraw;
return drawPass;
}
public void resetpos(){
serverDraw = null;
}
within public void run()
while(true){
while(true){
serverDraw =(com.DrawTastic.Drawring) ois.readObject();
test.add(serverDraw);
}
}
ClientActivity loops this:
try{
Drawring fromNet = net.GetServerDraw();
net.resetpos();
int position = fromNet.getPos();
switch (position) {
case 1:
try {
Log.d(null, "1st Position");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
case 2:
try {
Log.d(null, "2nd Position");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
case 3:
try {
Log.d(null, "3rd Position");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
catch(Exception e)
{
Log.d(null, e.toString());
}

Java program terminating after ObjectMapper.writeValue(System.out, responseData) - Jackson Library

I'm using the Jackson library to create JSON objects, but when I use the mapper.writeValue(System.out, responseData) function, the program terminates. Here is my code:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
public class Test {
public static void main(String[] args){
new Test().test();
}
public void test() {
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> responseData = new HashMap<String, Object>();
responseData.put("id", 1);
try {
mapper.writeValue(System.out, responseData);
System.out.println("done");
} catch (JsonGenerationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}.
}
After this executes, the console shows {"id":1}, but does not show "done".
The problem is with the Jackson implementation, as ObjectMapper._configAndWriteValue calls UtfGenerator.close(), which calls PrintStream.close().
I'd log an issue at https://jira.codehaus.org/browse/JACKSON
To change the default behavior of target being closed you can do the following:
mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
While declaring variable names in your data files/getter files, the first letter should be small.

Categories

Resources