0% found this document useful (0 votes)
12 views

Inverted Index Program

This method takes in two file paths and compares the contents of the files line by line. It returns true if all lines match after normalizing the file paths and false if any lines do not match. It keeps track of the line number of any mismatches found.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Inverted Index Program

This method takes in two file paths and compares the contents of the files line by line. It returns true if all lines match after normalizing the file paths and false if any lines do not match. It keeps track of the line number of any mismatches found.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

public static boolean testFiles(Path path1, Path path2) { Charset charset = java.nio.charset.StandardCharsets.UTF_8; try { BufferedReader reader1 = Files.

newBufferedReader(path1, charset); BufferedReader reader2 = Files.newBufferedReader(path2, charset); String line1 = reader1.readLine(); String line2 = reader2.readLine(); // used to output line mismatch int count = 0; // used to remove base directory from paths String pattern = Matcher.quoteReplacement(getBaseDirectory()); while(true) { count++; if ((line1 != null) && (line2 != null)) { if (!line1.trim().equals(line2.trim())) { // it is possible the base directory is different // replace base directory with CS_LAB directory line1 = line1.replaceFirst(pattern, CSLAB_DIR); line2 = line2.replaceFirst(pattern, CSLAB_DIR); // use consistent path separators line1 = line1.replaceAll(Matcher.quoteReplacement(File.separator), "/"); line2 = line2.replaceAll(Matcher.quoteReplacement(File.separator), "/"); // now compare lines again if (!line1.equals(line2)) { System.out.println("WARNING: Mismatch found on line " + count + " of " + path1.getFileName()); return false; } }

line1 = reader1.readLine(); line2 = reader2.readLine(); } else { // discard extra blank lines at end of reader1 while ((line1 != null) && line1.trim().isEmpty()) { line1 = reader1.readLine(); } // discard extra blank lines at end of reader2 while ((line2 != null) && line2.trim().isEmpty()) { line2 = reader2.readLine(); } // only true if both are null, otherwise one file had // extra non-empty lines return (line1 == line2); } } } catch (Exception ex) { return false; } } -------------------public void addToMap(String word,String fileName,Integer position) { TreeMap<String , ArrayList<Integer>> innerMap; ArrayList<Integer> pos; if(!outerMap.containsKey(word)) { innerMap=new TreeMap<String,ArrayList<Integer>>(); pos = new ArrayList<Integer>(); pos.add(position); outerMap.put(word, innerMap); innerMap.put(fileName, pos); } else { innerMap= outerMap.get(word); if(innerMap.containsKey(fileName)) { pos = innerMap.get(fileName); pos.add(position); } else { pos=new ArrayList<Integer>(); pos.add(position);

innerMap.put(fileName,pos); }

You might also like