开发者

java how to avoid duplicate in recursion

i'm trying to search synonym(which i declare as 'synset') recursively. Unfortunately, there are duplicate of synonym. For example: when i search the word student, the output will be like this:

Search word: student

Synset 0: pupil
  Synset 0: student
    Synset 0: pupil
    .
    .
    .
  Synset 1: educatee
  Synset 2: schoolchild
Synset 1: educatee
Synset 2: scholar
Synset 3: bookman

I want to store all the output into database and I do not need the duplicate output.This is part of my code that consist recursive function. Hope anyone can help me..Thanks

public String printSynset(String word)
    {
        //call wordnet library

RiWordnet wordnet = new RiWordnet(); //call stemmer method PorterStemmer s = new PorterStemmer();

Vector<String> synsetVec = new Vector<String>(); String[] synset = wordnet.getAllSynsets(word, "n"); for (int k=0; k<synset.length; k++) { synsetVec.add(synset[k]); if (!synsetVec.isEmpty()) { for (int j = 0; j < synsetVec.size();) { GUIsynonymTA.append("\n"); GUIsynonymTA.append(" No." + j + ": " + (s.Stem(synsetVec.get(j)))); GUIsyn开发者_如何学GoonymTA.append("\n"); return printSynset(synsetVec.get(j)); } } else if (synsetVec.isEmpty()) return word; } return word; }//end printSynset()


You should maintain a Set of the items you've already seen. Every time you hit an item, first check whether it's been seen before; if it is, stop the recursion; if it's not, add it to the set and proceed.

The result is the classic depth-first search for general graphs, which you can find in any algorithms textbook or in Russell & Norvig chapter 3. Pseudocode:

Set<Node> printSynset(Node root) {
    HashSet<Node> closed;
    printDFS(root, closed);
}

// recursive graph dfs
void printDFS(Node n, Set<Node> closed) {
    if (!closed.contains(n)) {
        print(n.item);
        closed.add(n);
        for (Node s : n.neighbors())
            printDFS(n, closed);
    }
}

Note that when printDFS returns to printSynset, it will have filled closed with all nodes it has visited, so you could also choose to return that Set<Node> and loop over it in printSynset, instead of doing the printing in printDFS. That would leave you with a general, re-usable DFS routine.


Use a Set to store the previously found matches. If the word is in the Set don't output it again.

Set

Maintain the Set as a class level field so that all the recursions of the method have access to the Set. If Set.add(word) returns false you know that the word was already in the Set.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜