How do I loop over the words and avoid duplication for replacement in template?

Hi,

I have been working on this problem for 3 weeks. No joke but I am not able to get it run right without duplication of words being replaced.

I hope someone can kindly point out what should I do in my code to get it right.

public class GladLib {
private ArrayList adjectiveList;
private ArrayList nounList;
private ArrayList colorList;

private Random myRandom;

private static String dataSourceDirectory = "data";

public GladLib(){
initializeFromSource(dataSourceDirectory);
used = new ArrayList();
usedCategory = new ArrayList();
myRandom = new Random();
}

public GladLib(String source){
    initializeFromSource(source);
    used = new ArrayList<String>();
    usedCategory = new ArrayList<String>();
    myRandom = new Random();
}

private void initializeFromSource(String source) {
    ArrayList<String> arrayList = new ArrayList<String>();        
    adjectiveList= readIt(source+"/adjective.txt"); 
    nounList = readIt(source+"/noun.txt");       
    colorList = readIt(source+"/color.txt");

}

private String randomFrom(ArrayList source){
int index = myRandom.nextInt(source.size());
return source.get(index);
}

private String getSubstitute(String label) {                

    if (label.equals("country")) {
        return randomFrom(countryList);
    }
    if (label.equals("color")){
        return randomFrom(colorList);
    }
    if (label.equals("noun")){
        return randomFrom(nounList);
}

return "UNKNOWN";
}

private String processWord(String w){ // I am supposed to loop over the words here but I just can't figure it out :(
    int first = w.indexOf("<");
    int last = w.indexOf(">", first);
    if(first == 1 || last == -1){
        return w;
    }
    String prefix = w.substring(0, first);
    String suffix = w.substring(last+1);
    String sub = getSubstitute(w.substring(first+1, last));

    for (int i = 0; i < used.size(); i+=1){     

        int index = used.indexOf(sub);
        if(index == 1 ){
                sub = getSubstitute(w.substring(first+1, last));
                index = used.indexOf(sub);                    
            }
        if (index != 1){
            used.add(sub);
        }
    }
                return prefix + sub+suffix;

}


private void printOut(String s, int lineWidth){
    int charsWritten = 0;
    for(String w : s.split("\\s+")){
        if (charsWritten + w.length() > lineWidth){
            System.out.println();
            charsWritten = 0;
        }
        System.out.print(w+" ");
        charsWritten += w.length() + 1;
    }
}

private String fromTemplate(String source){
       FileResource resource = new FileResource(source);
        for(String word : resource.words()){
            story = story + processWord(word) + " ";
        }
    }
    return story;
}

private ArrayList<String> readIt(String source){
    ArrayList<String> list = new ArrayList<String>();
     FileResource resource = new FileResource(source);
        for(String line : resource.lines()){
            list.add(line);
        }
    }

    return list;
}


public void makeStory(){
    used.clear();      
    System.out.println("\n");
    String story = fromTemplate("data/madtemplate2.txt");         
    printOut(story, 60);
}   

}

Here's how the madtemplate file looks like :

This is a story about how a
became a . Once upon a time, about
ago, , s roamed the
earth. One of them was named . This was alone in
the world. Then it became a living in . This animal loved to and . In the morning it would eat a , and later eat a for a snack.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories