Question

RiWordnet wordnet = new RiWordnet();
    String word = "apple";
    String[] poss = wordnet.getPos(word);
    boolean check = false;
    String[] synonyms = null;

    for (int j = 0; j < poss.length; j++) {
        synonyms = wordnet.getAllSynonyms(word, poss[j], 50);
        for (int i = 0; i < synonyms.length; i++) {
            System.out.println(synonyms[i]);
        }
    }

    for(int i= 0; i<synonyms.length;i++){
        if(input.contains(synonyms[i])){
            check=true;
        }
    } //end of synonym method

if(synonyms.check(true))
String syn ="that is the synonym of apple.";

So I am working on Wordnet API on synonym recognition. The snippet code above works perfectly fine, but that method only works for the word "apple". I still have to use that method for many other words as well, so instead of copy and pasting the method over and over again for different words, is there a way for me to put the method inside a different class and call the it when i need to use it? If so, how?

(p.s: i've tried to put it in a class, but i'm not sure how to call the method and the boolean for the "if" statement. Below is my attempt)

public class synonyms {
public static String syn(String word){
    RiWordnet wordnet = new RiWordnet();
    String input = null;
    String[] poss = wordnet.getPos(word);
    boolean check = false;
    String[] synonyms = null;

    for (int j = 0; j < poss.length; j++) {
        synonyms = wordnet.getAllSynonyms(word, poss[j], 50);
        for (int i = 0; i < synonyms.length; i++) {
            System.out.println(synonyms[i]);
        }
    }

    for(int i= 0; i<synonyms.length;i++){
        if(input.contains(synonyms[i])){
            check=true;
        }
    }
}
}
Was it helpful?

Solution

maybe what you want is:

public class MySynonyms {
    public static boolean syn(String input, String word){
        RiWordnet wordnet = new RiWordnet();
        String[] poss = wordnet.getPos(word);
        boolean check = false;
        String[] synonyms = null;

        for (int j = 0; j < poss.length; j++) {
            synonyms = wordnet.getAllSynonyms(word, poss[j], 50);
            for (int i = 0; i < synonyms.length; i++) {
                //System.out.println(synonyms[i]);
            }
        }

        for(int i= 0; i<synonyms.length;i++){
            if(input.contains(synonyms[i])){
                check=true;
            }
        }
        return check;
    }
}

and from your main class:

if(MySynonyms.syn("apel","apple"))
{
    //...do something
}
else{
    //...do something
}

OTHER TIPS

Using classes is always a good idea, however I'm not sure what the rest of your code structure and design implementation is. To create an object (which is using a class) you can do the following:

First create a Synonym class which contains a method called syn as follows:

public class Synonym{
    public static String syn(String word){
        // put your syn checking codes here
    }
}

After this, in your main class (or wherever you need this) you can create a new object as follows

Synonym synonym_obj = new Synonym();

An object is an instance of a class, and as long as the classes methods are public, you will be able to use them through synonym_obj. For example, you can put the following code wherever you need to check if two words are synonymous:

Synonym synonym_obj = new Synonym();
String word = "apple";
if(synonym_obj(word)){
    System.out.println("This is a synonym.");
} else {
    System.out.println("This is not a synonym.");
}

Hope that clarifies things for you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top