Problème accès à un dossier+fichiers
Bonjour,
je vous souhaite à toutes et à tous une bonne année 2014, avec un peu de retard.
Je viens vous voir aujourd'hui car j'ai fais un script, qui prend en entrée un dossier (qui comprend deux autres dossiers, et chacun de ces deux dossiers comprend des fichiers .txt). Le but étant de lire les fichiers txt, comparer tous les mots de tout les fichiers, et supprimer les mots non fréquents.
Sauf, qu'à ma surprise, il me dit qu'il n'y pas de dossier, alors qu'il y est. Je ne comprend pas :s
Voici le script entier.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class suppMotsNonFrequents
{
//---------------------------------------------------------------------------------------------------------------
//NECESSARY OBJECTS
protected ArrayList<String> corpusClasses = new ArrayList<String>();
protected ArrayList<ArrayList<String>> corpusDocuments = new ArrayList<ArrayList<String>>();
protected ArrayList<ArrayList<ArrayList<String>>> corpus = new ArrayList<ArrayList<ArrayList<String>>>();
protected HashMap<String,Integer> tableWordsIndex = new HashMap<String,Integer>();
protected HashMap<Integer,String> tableIndexWords = new HashMap<Integer,String>();
//---------------------------------------------------------------------------------------------------------------
//CONSTRUCTOR
public suppMotsNonFrequents(){ super();}
//---------------------------------------------------------------------------------------------------------------
//CORPUS INITIALIZER
public void initializer(String corpusLocation) throws Exception
{
File corpusFilePath = new File(corpusLocation); //root directory
File[] corpusFiles = null;
File[] documentsFromClass = null;
File documentLocationFile = null;
ArrayList<String> document = new ArrayList<String>();
ArrayList<String> tempdocument = new ArrayList<String>();
ArrayList<ArrayList<String>> documents = new ArrayList<ArrayList<String>>();
//CHECKING IF IT EXISTS A FOLDER AND CHEACKING IF CLASS INSIDE
if(corpusFilePath.exists() && corpusFilePath.isDirectory() && (corpusFilePath.listFiles().length > 0) )
{
//GETTING CLASS LIST
corpusFiles = corpusFilePath.listFiles();
//LOOP FOR EACH CLASS
for(int i =0; i < corpusFiles.length; i++)
{
//SETTING UP ALL CLASS NAME
this.corpusClasses.add(corpusFiles[i].getName());
//GETTING DOCUMENTS LIST FILE FROM CLASSE
documentsFromClass = corpusFiles[i].listFiles();
//LOOP FOR EACH DOCUMENT
for(int j =0; j < documentsFromClass.length; j++)
{
//GETTING DOCUMENTS LOCATION FILE NAME
documentLocationFile = new File(documentsFromClass[j].getPath());
//GETTING EACH DOCUMENTS NAME
tempdocument.add(documentLocationFile.getName());
//PROCESSING DOCUMENT
this.processingDocument(documentLocationFile.getPath(),document);
//CREATING DOCUMENTS LIST FOR EACH CLASS
documents.add(document);
//RESTARTING LISTS FOR NEXT LOOP
document = new ArrayList<String>();
}
//SETTING UP CORPUS DOCUMENTS NAME
this.corpusDocuments.add(tempdocument);
//SETTING UP THE CORPUS
corpus.add(documents);
//RESTARTING LISTS FOR NEXT LOOP
documents = new ArrayList<ArrayList<String>>();
}
}
else
{
System.out.println("File doesn't exists..");
}
}
//---------------------------------------------------------------------------------------------------------------
//DOCUMENT READER METHOD
public void processingDocument(String documentPath,ArrayList<String> document) throws Exception
{
Scanner documentReader = new Scanner(new File(documentPath));
ArrayList<String> lineWords = new ArrayList<String>();
String documentLine = new String();
//CREATING DOCUMENT
while (documentReader.hasNext())
{
documentLine = documentReader.next();
if(documentLine != null)
{
//GETTING LIST WORDS PAR LINE
lineWords = processingLine(documentLine);
for(int i =0; i < lineWords.size(); i++)
{
//CREATING DOCUMENT
document.add(lineWords.get(i));
}
}
}
//CLOSING BUFFED-READER
documentReader.close();
}
//---------------------------------------------------------------------------------------------------------------
//LINE READER METHOD
public ArrayList<String> processingLine(String documentLine)
{
StringTokenizer tokens = new StringTokenizer(documentLine);
ArrayList<String> words = new ArrayList<String>();
String word = new String();
while (tokens.hasMoreTokens())
{
//SETTING ALL CHARACTERS ON LOWERCASE
word = tokens.nextToken().toLowerCase();
words.add(word);
}
return words;
}
//---------------------------------------------------------------------------------------------------------------
//GETTING HOW MANY TIMES THE WORD IS USED ON THE CORPUS
public int getFrequencyOfWordInCorpus(String word)
{
int frequencyOnCorpus = 0;
for(int i =0; i< this.corpus.size();i++)
{
for(int j=0; j< this.corpus.get(i).size();j++)
{
for(int k = 0; k < corpus.get(i).get(j).size();k++)
{
if(word.equals(corpus.get(i).get(j).get(k).toString()))
{
frequencyOnCorpus ++;
}
}
}
}
return frequencyOnCorpus;
}
//---------------------------------------------------------------------------------------------------------------
//NEW CORPUS CREATION METHOD
public void createNewCorpus(String fileNamePath) throws Exception
{
if(corpus.size() > 0)
{
//CREATING FILE
File file = null;
PrintWriter writer = null;
//GENERATING ELEMENTS
for(int i =0; i< this.corpus.size();i++)
{
//FOLDER CLASS CREATION
file = new File(fileNamePath+"/"+this.corpusClasses.get(i).toString());
if(file.mkdir())
{
for(int j= 0; j< this.corpus.get(i).size();j++)
{
//DOCUMENT CREATION
File file1 = new File(file.getPath()+"/"+this.corpusDocuments.get(i).get(j).toString());
file1.createNewFile();
//RESTARTING OBJECTS
writer = new PrintWriter(file1.getPath());
String stringLine = new String();
for(int k = 0; k < this.corpus.get(i).get(j).size();k++)
{
//FREQUENCY PRESICION
if(this.getFrequencyOfWordInCorpus(this.corpus.get(i).get(j).get(k).toString()) >= 10)
{
//TO CONCAT WORDS OF DOCUMENT
stringLine += this.corpus.get(i).get(j).get(k).toString()+" ";
}
}
//WRITING ON FILE
writer.println(stringLine.substring(0, (stringLine.length()-1)));
//CLOSING FILE
writer.close();
}
}
}
System.out.println("New corpus created successfully...");
}
else {System.out.println("The corpus is not started...");}
}
//---------------------------------------------------------------------------------------------------------------
//MAIN FUCTION
public static void main(String[] args) throws Exception
{
//CLASS INSTANCE
suppMotsNonFrequents eraser = new suppMotsNonFrequents();
//NEEDED PATHS
String corpusPath = "/Corpus";
String outputLocationPath = "/corpusNew";
//STARTING CORPUS
eraser.initializer(corpusPath);
//CERATING NEW CORPUS ORDER
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
eraser.createNewCorpus(outputLocationPath);
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
//---------------------------------------------------------------------------------------------------------------
} |
Voilà ce que ça me renvoi :
Citation:
File doesn't exists..
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The corpus is not started...
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Pour info je suis sur eclipse (Mac Os X)
En vous remerciant. :ccool: