Indexer.java~ 2.02 KB
Newer Older
Tien's avatar
Tien committed
1
/*
Tien's avatar
Tien committed
2
 * Copyright (C) 2015 UNIVERSITE JOSEPH FOURIER (Grenoble 1)/ Springer-Verlag GmbH
Tien's avatar
Tien committed
3
 * author Nguyen Minh Tien - minh-tien.nguyen@imag.fr
Tien's avatar
Tien committed
4
 *
Tien's avatar
Tien committed
5 6 7 8
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
Tien's avatar
Tien committed
9 10 11 12 13 14 15
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
Tien's avatar
Tien committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
Tien's avatar
Tien committed
18
 */
Tien's avatar
Tien committed
19
package fr.imag.Scidetect.Checker;
Tien's avatar
Tien committed
20 21 22 23 24 25 26 27

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashMap;

/**
 *
Tien's avatar
Tien committed
28
 * @author tien
Tien's avatar
Tien committed
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
 */
public class Indexer {

    private Object content;

    public void index(String content, File textfile) throws FileNotFoundException {
        String filename = textfile.getName().substring(0,textfile.getName().lastIndexOf("."));
        filename+=".txt";
        String path = textfile.getParent();

        String[] words = content.split(" ");
        //System.out.println(words.length);
        HashMap<String, Integer> counter = new HashMap<String, Integer>();
        for (int i = 0; i < words.length; i++) {
            if (!counter.containsKey(words[i])) {
                counter.put(words[i], 1);
            } else {
                counter.put(words[i], counter.get(words[i]) + 1);
            }
        }

        File indexout = new File(path + "/INDEX-" + filename);
       // String filepath = (indexout.getPath());
        PrintWriter out = new PrintWriter(indexout);

        for (String key : counter.keySet()) {
            out.println(key + "\t" + counter.get(key));
        }
        out.close();
    }
}