HasMap exemple + StopList. I can't make de stopList do the job.
in
Programming Questions
•
2 years ago
Hello.
Here's my problem in the code below.
I want to know how often a word appears in the file (mousquetaires.txt : the text of les 3 mousquetaires, by Alexandre Dumas) but i want to exclude the useless words by adding a stopList (stopList.txt : a list of french words like "et", "de", "il" ).
I tried an "if" (line 37) structure but i can't make Processing match the words in the stopList and in the HashMap that contains each word in the text (mousquetaires.txt).
Please help me, i cant figure out where i made a mistake.
Excuse my english, i'm french.
Thanks
Here's my problem in the code below.
I want to know how often a word appears in the file (mousquetaires.txt : the text of les 3 mousquetaires, by Alexandre Dumas) but i want to exclude the useless words by adding a stopList (stopList.txt : a list of french words like "et", "de", "il" ).
I tried an "if" (line 37) structure but i can't make Processing match the words in the stopList and in the HashMap that contains each word in the text (mousquetaires.txt).
Please help me, i cant figure out where i made a mistake.
Excuse my english, i'm french.
Thanks
- HashMap words; // HashMap object
- String[] tokens; // Array of all words from input file
- int counter;
- String[] stopList;
- PFont f;
- void setup() {
- size(640, 360);
- frame.setResizable(true);
- smooth();
- words = new HashMap();
- //Liste des mots a exclure
- stopList = loadStrings("stopList.txt");
- // Load file and chop it up
- String[] lines = loadStrings("mousquetaires.txt");
- String allText = join(lines, " ");
- tokens = splitTokens(allText, " ,.?!:;[]-'()");
- f = createFont("Georgia", 36, true);
- }
- void draw() {
- background(255);
- fill(0);
- // Look at words one at a time
- String s = tokens[counter];
- counter = (counter + 1) % tokens.length;
- // Is the word in the HashMap
- if (words.containsKey(s)) {
- // Get the word object and increase the count
- // We access objects from a HashMap via its key, the String
- Word w = (Word) words.get(s);
- w.count();
- }
- else {
- // Otherwise make a new word
- Word w = new Word(s);
- // the stopList will exclude the useless words.
- for (int j=0; j<stopList.length; j++) {
- if (w.word != stopList[j]) {
- // And add to the HashMap
- // put() takes two arguments, "key" and "value"
- // The key for us is the String and the value is the Word object
- words.put(s, w);
- }
- }
- }
1