Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To find similar words (strings) in two files

I have to validate the similarity of word 1 in file 1 with word 2 in file 2 and so on. if word 1 (file 1).equals to word 2 (file 2), file 3 will be the output to show the True and False. Below is the coding but I am stuck when there is no error but giving no output. Am a beginner in JAVA.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class test2 {

    private static ArrayList<String> load(String f1) throws FileNotFoundException {
        Scanner reader = new Scanner(new File(f1));
        ArrayList<String> out = new ArrayList<String>();
        while (reader.hasNext()) {
            String temp = reader.nextLine();
            String[] sts = temp.split(" ");
            for (int i = 0; i < sts.length; i++) {
                if (sts[i].equals("") && sts[i].equals(" ") && sts[i].equals("\n")) {
                    out.add(sts[i]);
                }
            }
        }
        return out;
    }

    private static void write(ArrayList<String> out, String fname) throws IOException {
        FileWriter writer = new FileWriter(new File("out_test2.txt"));
        for (int i = 0; i < out.size(); i++) {
            writer.write(out.get(i) + "\n");
        }
        writer.close();
    }

    public static void main(String[] args) throws IOException {
        ArrayList<String> file1;
        ArrayList<String> file2;
        ArrayList<String> out = new ArrayList<String>();
        file1 = load("IbanDict.txt");
        file2 = load("AFF_outVal.txt");

        for (int i = 0; i < file1.size(); i++) {
            String word1 = file1.get(i);
            for (int z = 0; z < file2.size(); z++) {
                if (word1.equalsIgnoreCase(file2.get(z))) {
                    boolean already = false;
                    for (int q = 0; q < out.size(); q++) {
                        if (out.get(q).equalsIgnoreCase(file1.get(i))) {
                            already = true;
                        }
                    }
                    if (already == false) {
                        out.add(file1.get(i));
                    }
                }
            }
        }
        write(out, "out_test2.txt");
    }

}
like image 962
ssaee Avatar asked Aug 15 '11 01:08

ssaee


1 Answers

Here is my suggestion for your porblem

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

  private static final Pattern WORD_PATTERN = Pattern.compile("[\\w']+");

  private static Map<String, Integer> load(final String f1) throws FileNotFoundException {
    Scanner reader = new Scanner(new File(f1));
    Map<String, Integer> out = new HashMap<String, Integer>();
    while (reader.hasNext()) {
      String tempLine = reader.nextLine();
      if (tempLine != null && tempLine.trim().length() > 0) {
        Matcher matcher = WORD_PATTERN.matcher(tempLine);
        while (matcher.find()) {
          out.put(matcher.group().toLowerCase(), 0);
        }
      }
    }

    return out;
  }

  private static void write(final Map<String, Integer> out, final String fname) throws IOException {
    FileWriter writer = new FileWriter(new File(fname));
    for (Map.Entry<String, Integer> word : out.entrySet()) {
      if (word.getValue() == 1) {
        writer.write(word.getKey() + "\n");
      }
    }
    writer.close();
  }

  public static void main(final String[] args) throws IOException {
    Map<String, Integer> file1 = load("file1.txt");
    Map<String, Integer> file2 = load("file2.txt");

    // below for loop will run just one time, so it is much faster
    for (Map.Entry<String, Integer> file1Word : file1.entrySet()) {
      if (file2.containsKey(file1Word.getKey())) {
        file1.put(file1Word.getKey(), 1);
        file2.put(file1Word.getKey(), 1);
      }
    }

    write(file1, "test1.txt");
    write(file2, "test2.txt");
  }

}
like image 56
Kowser Avatar answered Oct 11 '22 22:10

Kowser