Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) throws Exception {
  8.         //ArrayList<String> l1 = new ArrayList<>(Arrays.asList(new String("Happy families are all alike; every unhappy family is unhappy in its own way Everything was in confusion in the Oblonskys' house.  The wife had discovered that the husband was carrying on an intrigue with a French girl, who had been a governess in their family, and she had announced to her husband that she could not go on living in the same house with him.").toLowerCase().split(" ")));
  9.         //ArrayList<String> l2 = new ArrayList<>(Arrays.asList(new String("In the words of Leo Tolstoy: Every unhappy family is unhappy in its own way").toLowerCase().split(" ")));
  10.         ArrayList l1 = readDocument("/home/lofibytes/Downloads/moby_dick.txt");
  11.         ArrayList l2 = readDocument("/home/lofibytes/Downloads/huck_finn_plagiarized.txt");
  12.         ArrayList<String> duplicates = getDuplicatesList(Arrays.copyOf(l1.toArray(), l1.size(),String[].class), Arrays.copyOf(l2.toArray(), l2.size(),String[].class));
  13.         System.out.println(duplicates);
  14.     }
  15.  
  16.     private static ArrayList<String> getDuplicatesList(String[] l1, String[] l2) {
  17.         long millis = System.currentTimeMillis();
  18.         ArrayList<String> duplicates = new ArrayList<>();
  19.         HashMap<String, ArrayList<Integer>> words = new HashMap<>();
  20.         for (int i = 0; i < l2.length; i += 1) { // unindexed word create arraylist of locations of word
  21.             if (words.get(l2[i]) == null) {
  22.                 ArrayList wordLocations = new ArrayList();
  23.                 wordLocations.add(i);
  24.                 words.put(l2[i], wordLocations);
  25.             } else {
  26.                 words.get(l2[i]).add(i); // Word already indexed so just add the next location
  27.             }
  28.         }
  29.         ArrayList<Integer> indexes;
  30.         String match;
  31.         int i, j, k, offset;
  32.         for (i = 0; i < l1.length; i ++) {
  33.             if (!words.containsKey(l1[i])) continue;
  34.             indexes = words.get(l1[i]);
  35.             if (indexes != null) {
  36.                 for (j = 0; j < indexes.size(); j++) {
  37.                     match = l1[i];
  38.                     offset = 0;
  39.                     for (k = indexes.get(j) + 1; k < l2.length && i + offset < l1.length; k++) {
  40.                         offset++;
  41.                         if (l1[i + offset].equalsIgnoreCase(l2[k]))
  42.                             match += " " + l2[k];
  43.                         else {
  44.                             i += offset - 1;
  45.                             break;
  46.                         }
  47.                     }
  48.                     if (offset <= 3) continue;
  49.                     duplicates.add(match);
  50.                 }
  51.             }
  52.         }
  53.         System.out.println((System.currentTimeMillis() - millis) / 1000);
  54.         return duplicates;
  55.     }
  56.     /*private static ArrayList<String> getDuplicatesList(ArrayList<String> l1, ArrayList<String> l2) {
  57.         ArrayList<String> duplicates = new ArrayList<>();
  58.         long n1 = System.nanoTime();
  59.         for (int i = 0; i < l1.size(); i++)
  60.             for (int j = 0; j < l2.size(); j++)
  61.                 if (l1.get(i).equalsIgnoreCase(l2.get(j))) {
  62.                     String match = l1.get(i);
  63.                     for (int k = 1; i + k < l1.size() && j + k < l2.size(); k++)
  64.                         if (l1.get(i + k).equalsIgnoreCase(l2.get(j + k)))
  65.                             match += " " + l2.get(k + j);
  66.                         else {
  67.                             j += k - 1;
  68.                             break;
  69.                         }
  70.                     int longest = 0;
  71.                     for (int l = 0; l < duplicates.size(); l++)
  72.                         if (longest < duplicates.get(l).length())
  73.                             longest = duplicates.get(l).length();
  74.                     if (longest < match.length())
  75.                         duplicates.add(match);
  76.                 }
  77.         System.out.println(System.nanoTime() - n1);
  78.         return duplicates;
  79.     }
  80. */
  81. /*
  82.     private static ArrayList<String> getDuplicatesList(ArrayList<String> l3, ArrayList<String> l4) {
  83.         ArrayList<String> duplicates = new ArrayList<>();
  84.         LinkedHashSet<String> l1 = new LinkedHashSet<>(l3);
  85.         LinkedHashSet<String> l2 = new LinkedHashSet<>(l4);
  86.         Iterator<String> i = l1.iterator();
  87.         int _i = -1;
  88.         long n1 = System.nanoTime();
  89.         while (i.hasNext()) {
  90.             _i++;
  91.             String iVal = i.next();
  92.             if (l2.contains(iVal)) {
  93.                 System.out.println(iVal);
  94.                 for (int e = _i; e < l3.size(); e++) {
  95.                     for (int j = 0; j < l4.size(); j++) {
  96.                         if (l3.get(e).equalsIgnoreCase(iVal) && l4.get(j).equalsIgnoreCase(iVal)) {
  97.                             String match = iVal;
  98.                             for (int k = 1; e + k < l3.size() && j + k < l4.size(); k++)
  99.                                 if (l3.get(e + k).equalsIgnoreCase(l4.get(j + k)))
  100.                                     match += " " + l4.get(k + j);
  101.                                 else
  102.                                     break;
  103.                             int longest = 0;
  104.                             for (int l = 0; l < duplicates.size(); l++)
  105.                                 if (longest < duplicates.get(l).length())
  106.                                     longest = duplicates.get(l).length();
  107.                             if (longest < match.length())
  108.                                 duplicates.add(match);
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.         System.out.println(System.nanoTime() - n1);
  115.         return duplicates;
  116.     }*/
  117.  
  118.  
  119.  
  120.     public static ArrayList<String> readDocument(String fileName) throws FileNotFoundException
  121.     {
  122.         Scanner scanner;
  123.         ArrayList<String> words = new ArrayList<String>();
  124.  
  125.         scanner = new Scanner(new File(fileName));
  126.         scanner.useDelimiter("[\\W]"); // all non-word characters
  127.         int k = 0;
  128.         String word = "";
  129.         while (scanner.hasNext())
  130.         {
  131.             String cur = scanner.next();
  132.             if (!cur.equals(""))
  133.             {
  134.                 k++;
  135.                 if (k == 1) {
  136.                     word = cur.toLowerCase();
  137.                 } else
  138.                     word += " " + cur.toLowerCase();
  139.                 if (k >= 3 || !scanner.hasNext()) {
  140.                     words.add(word.trim());
  141.                     word = "";
  142.                 }
  143.             }
  144.         }
  145.         scanner.close();
  146.         return words;
  147.     }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement