vendredi 31 juillet 2015

Change randomly indexed vowel in a string:bound must be positive

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;


 public class MachinePedagogy {


     public static void main(String[] args) throws IOException {
         changeVowel("dictionary.txt");

}
private static void changeVowel(String path) throws IOException {
    char ch;

    BufferedWriter bWriter = new BufferedWriter(new FileWriter(new File("changevowel.txt")));
    String aeiou = "aeiou";
    char[] vowels = aeiou.toCharArray();
    BufferedReader bReader = new BufferedReader(new FileReader(new File(path)));
    for(String temp = bReader.readLine(); temp != null; temp = bReader.readLine()){
        int counter = 0;
        char[] characters = temp.toCharArray();
            for(int i = 0; i < temp.length(); i++){

                ch = temp.charAt(i);


                if(
                ch == 'a' || 
                ch == 'e' || 
                ch == 'i' || 
                ch == 'o' || 
                ch == 'u'){
                    counter++;
                    }
                }
            Random rand = new Random();
            int vIndex[] = new int[counter];
            int index = 0;  
            for (int j = 0; j <temp.length(); j++){
                ch = temp.charAt(j);
                if(
                        ch == 'a' || 
                        ch == 'e' || 
                        ch == 'i' || 
                        ch == 'o' || 
                        ch == 'u'){
                    vIndex[index] = j;
                    index++;
                }
            }
            int random2 = (rand.nextInt(vIndex.length));
            int random1 = (rand.nextInt(vowels.length));
            characters[vIndex[random2]] = vowels[random1];
            temp = String.valueOf(characters);
        bWriter.write(temp + "\n");
    }
    bReader.close();
    bWriter.close();
     }
 }

I want to edit a random vowel in a string and change it to another random vowel, preferably different from what it was previously. Dictionary.txt is just a Stanford dictionary if you need that to compile the code.

Many examples I have found in replacing vowels follow this logic.

temp.replaceAll( "[aeiou]", "?" );

I don't want to replace all the vowels, just a single randomly indexed vowel. I think it might have something to do with nextInt() but I am confused when I read the documentation Random#nextInt(int) as to why that might be. What I have read from other StackOverflow questions asked is that this is a valid way of producing random indexes for an array.

I wrote a spell checker and want to test its capabilities for accuracy with a commonly made error of changing a single vowel of a word. I plan on removing correct words from the big list that I've created by running the changevowel.txt against dictionary.txt later & removing correct words from changevowel.txt but that is unimportant to the immediate problem at hand.




Aucun commentaire:

Enregistrer un commentaire