Respected Members,
The topic has been discussed previously but the I have tried those. I am facing an issue in reading all text files from one folder. I am calculating the probability for each text file. Each text file has round about 1500 lines.The code I have shown is reading files from folder but it does not execute method for it.I have used two loops in code chunk. I tried to run execution with a value in "i " variable" in both loops. The while loops is executed before FOR loop(showing wrong logic) . I want it to execute "get.probability()" method for each text file. Kindly please look for the issue. It is only running the first file from folder named "cs.txt",calculates it's probability and detects its language
String target_dir = "./testdataset";
int i = 0;
BufferedReader inputStream = null;
File dir = new File(target_dir);
File[] files = dir.listFiles();
for (File f : files) {
if(f.isFile()) {
System.out.println("File name in directory is: " + f);
inputStream = new BufferedReader(new FileReader(f));
//System.out.println("i in FOR loop" + " " + i);
}
String line;
try {
while((line = inputStream.readLine()) != null) {
//System.out.println("i in while loop" + " " + i); just for checking
detector.append(inputStream);
}
//i++;
String lang = detector.detect();
ArrayList<Language> langlist = detector.getProbabilities();
System.out.println("Language Detected for input file is" + " " + lang);
System.out.println("Probability of language is: " + " " + langlist);
inputStream.close();
}
catch(Exception e) {}
}
I think your problem might due to the execution of the try
block even though f
might be a directory. You can use the continue
(see this) keyword to skip to the next iteration of the loop if f
is not a file.
I know nothing about the detector
, but make sure that input is cleared after inputStream.close()
is called, otherwise you might append multiple files to a single detector.
for (File f : files) {
//This will skip the file if it is a directory
if (!f.isFile())
continue;
System.out.println("File name in directory is: " + f);
inputStream = new BufferedReader(new FileReader(f));
String line;
try {
while((line = inputStream.readLine()) != null) {
//System.out.println("i in while loop" + " " + i); just for checking
detector.append(inputStream);
}
//i++;
String lang = detector.detect();
ArrayList<Language> langlist = detector.getProbabilities();
System.out.println("Language Detected for input file is" + " " + lang);
System.out.println("Probability of language is: " + " " + langlist);
inputStream.close();
}
catch(Exception e) {}
}