public String expandTemplateTopic(String remark)    {

        String random_word =  findKeyWord(remark);
        String str=" ";
        String kok2=" ";
        int num1=0;
        int num2=0;
        int num3=0;

        //find a random template
        int number = generator.nextInt(sentenceList.size());
        String argument=sentenceList.get(number).getTemplate();

        //split it into words using space as delimiter
       List  ls=new LinkedList();
        String[] array =argument.split (" ");

        //find if there are %NOUN% words and if yes replace them with a synonym of the random_word
        for (int i=0; i < array.length; i++) {
            if (array[i].equals("%NOUN%"))   {
                System.out.println("Array["+i+"] is: "+array[i]);
              [B] ls.addAll(word.getSynonyms(POS.NOUN,random_word) );[/B]
               
                   if (ls.size()>0) {
                    str=(String)ls.get(num1);
                    array[i]=str; 
                    num1++;
                    for(int j=0;j<ls.size();j++) 
               System.out.println("ls "+j+"  is:"+ls.get(j) );
                } else array[i]=word.getNounAtRandom();
            }   ////find if there are %VERB% words and if yes replace them with a synonym of the random_word
            else if (array[i].equals("%VERB%") )    {
                System.out.println("Array["+i+"] is: "+array[i]);
              [B] ls.addAll(word.getSynonyms(POS.VERB,random_word) );[/B]
                if (ls.size()>0) {
                    str=(String)ls.get(num2);
                    array[i]=str;
                    num2++;
                    
                }    else array[i]=word.getVerbAtRandom();
            }   //find if there are %ADJECTIVE% words and if yes replace them with a synonym of the random_word
            else if (array[i].equals("%ADJECTIVE%"))  {
                System.out.println("Array["+i+"] is: "+array[i]);
              [B]ls.addAll(word.getSynonyms(POS.ADJECTIVE,random_word) );[/B]
                if (ls.size()>0)  {
                    str=(String)ls.get(num3);
                    array[i]=str;
                    num3++;
                    
                } else array[i]=word.getAdjectiveAtRandom();
            }//if
            System.out.println("Array["+i+"] is: "+array[i]);
            //clearing the List before the next loop
            ls.clear();
            
        }//for

        //re-assemble the sentence with the replaced words
        for (int k=1; k < array.length+1; k++) {
            kok2=kok2.concat(" "+array[k-1]);
        }
        //and return it
        return kok2;
    }

Function compiles just fine but it pops warnings that i have unsafe or unchecked operations and with a little search i found that the problem is on the bold lines on my code when i use addAll()

Just to clarify, the function getSynonyms returns an arraylist with the synonyms of the word we give it and in the end all this is fed to addAll() as an argument.

Whats wrong with my code and the addAll?

Recommended Answers

All 6 Replies

It's because you're not using generics. Also, if you look at the compiler message it is a warning, and, in this case, I believe you could ignore it. However, since you are doing all your work with Strings, then, currently, every where in your code where you currently have List (and I do mean everywhere), change it to List<String> .

Edit: Then again, that also depends on this "word" instance and what it's "getSynonyms" returns.

I tried everything you said with (String) but nothing, same thing. So i guess it has to do with getSynonyms as you mention:

public List getSynonyms(POS pos, String lookupWord) {

        Word[] words;
        Set synonymSet = new HashSet();
        try {
            IndexWord word = d.lookupIndexWord(pos, lookupWord);
            if (word != null) {
                //iterate over each sense of this word
                for (int j = 1; j < word.getSenseCount() + 1; j++) {

                    Synset syn = word.getSense(j);

                    //now add all the associated words from this synset to the 
                    //synonyms set
                    for (int k = 0; k < syn.getWords().length; k++) {
                        synonymSet.add(syn.getWords()[k].getLemma());

                    }
                    PointerTargetNodeList synonyms = PointerUtils.getSynonyms(syn);

                    if (synonyms != null) {
                        //if this sense of the word has synonyms, iterate over them
                        Iterator i = synonyms.iterator();
                        while (i.hasNext()) {

                            PointerTargetNode targetNode = (PointerTargetNode) i.next();
                            //if this synonym is just a string (lexical) 
                            //add it to the synonym list
                            if (targetNode.isLexical()) {
                                synonymSet.add(targetNode.getWord().getLemma());

                            } else {
                                // It's a synset rather than a word, 
                                //so retrieve all the words and add them to the 
                                //synonym list
                                words = targetNode.getSynset().getWords();
                                for (int t = 0; t < words.length; t++) {
                                    synonymSet.add(words[t].getLemma());

                                }

                            }

                        }

                    }
                }

            }

        } catch (JWNLException err) {
            err.printStackTrace();
        }
        return new ArrayList(synonymSet);
    }

this is the whole function, as i see it returns a arraylist. And addAll() from LinkedList class takes arraylists as arguments so i dont understand what might the problem be.

P.S How do i compile with Xlint? Thats what it says i should do on the warning as well, but if this is just to pinpoint the spot of the warning its useless cause i found the point already.

I tried everything you said with (String) but nothing, same thing. So i guess it has to do with getSynonyms as you mention:

public List getSynonyms(POS pos, String lookupWord) {

        Word[] words;
        Set synonymSet = new HashSet();
        try {
            IndexWord word = d.lookupIndexWord(pos, lookupWord);
            if (word != null) {
                //iterate over each sense of this word
                for (int j = 1; j < word.getSenseCount() + 1; j++) {

                    Synset syn = word.getSense(j);

                    //now add all the associated words from this synset to the 
                    //synonyms set
                    for (int k = 0; k < syn.getWords().length; k++) {
                        synonymSet.add(syn.getWords()[k].getLemma());

                    }
                    PointerTargetNodeList synonyms = PointerUtils.getSynonyms(syn);

                    if (synonyms != null) {
                        //if this sense of the word has synonyms, iterate over them
                        Iterator i = synonyms.iterator();
                        while (i.hasNext()) {

                            PointerTargetNode targetNode = (PointerTargetNode) i.next();
                            //if this synonym is just a string (lexical) 
                            //add it to the synonym list
                            if (targetNode.isLexical()) {
                                synonymSet.add(targetNode.getWord().getLemma());

                            } else {
                                // It's a synset rather than a word, 
                                //so retrieve all the words and add them to the 
                                //synonym list
                                words = targetNode.getSynset().getWords();
                                for (int t = 0; t < words.length; t++) {
                                    synonymSet.add(words[t].getLemma());

                                }

                            }

                        }

                    }
                }

            }

        } catch (JWNLException err) {
            err.printStackTrace();
        }
        return new ArrayList(synonymSet);
    }

this is the whole function, as i see it returns a arraylist. And addAll() from LinkedList class takes arraylists as arguments so i dont understand what might the problem be.

P.S How do i compile with Xlint? Thats what it says i should do on the warning as well, but if this is just to pinpoint the spot of the warning its useless cause i found the point already.

By adding the xlint option shown in the warning to the javac command (how you do this with any IDE you might be using depends on the IDE, read its manual). In any case, as I said, that depended on what getSynonyms returned. Your problem, if you want this warning to go away, goes much deeper. See http://java.sun.com/docs/books/tutorial/extra/generics/index.html.

By adding the xlint option shown in the warning to the javac command (how you do this with any IDE you might be using depends on the IDE, read its manual). In any case, as I said, that depended on what getSynonyms returned. Your problem, if you want this warning to go away, goes much deeper. See http://java.sun.com/docs/books/tutorial/extra/generics/index.html.

It was not generics, i just replaced:

ls.addAll(word.getSynonyms(POS.NOUN,random_word) );

with

ls = (word.getSynonyms(POS.NOUN,random_word) );

Okay, now the question is whether you really want to replace the current list with the list that method returns, or if you want to add the items (not already included in the list) returned from that method into the current list. Before it was doing the second, now it is doing the first and they are two drastically different things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.