Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class bruteForceSol {
  6.  
  7.  
  8.     public static void main(String [] args) {
  9.  
  10.  
  11.  
  12.  
  13.  
  14.             String[][] arr = csvToArray("./src/abr.csv");
  15.             ArrayList<String> list =  makeSentences("./src/sxswTweets.csv");
  16.  
  17.             String[] yes = new String [40000];
  18.             //Splits the words into an array, removing ALL punctuation.
  19.         for(int i = 0 ; i < list.size() ; i++) {
  20.              yes = list.get(i).replaceAll("\\p{Punct}", "").split(" ");
  21.         }
  22.              //Iterates through all abbreviations and checks each word in the tweet to see if it matches an abbreviation.
  23.              for(int i = 0 ; i < yes.length; i++){
  24.                  for(int k = 0 ; k < arr.length ; k++){
  25.                      if(yes[i].toLowerCase().equals((arr[k][0]).toLowerCase())){
  26.                          yes[i] = arr[k][1];
  27.                      }
  28.                  }
  29.              }
  30.  
  31.              for(int i = 0 ; i < yes.length; i++){
  32.                  System.out.print(yes[i]);
  33.              }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.         }
  40.  
  41.  
  42.     public static String[][] csvToArray(String csvName) {
  43.         //Notice how the array length must be the size of the number of abbreviations. Another reason to not use this solution.
  44.         String[][] csvArray = new String[74][2];  // create a multidimensional array that can hold 100 abbreviations
  45.  
  46.         try {
  47.             BufferedReader reader = new BufferedReader(new FileReader(csvName));
  48.             reader.readLine();                                           // read first line to remove column titles
  49.  
  50.             String row;
  51.             int index = 0;
  52.  
  53.             // O(m) runtime, where m is the number of lines(abbreviations)
  54.             while ((row = reader.readLine()) != null) {                  // read until end of file
  55.                 String[] kv = row.split(",");                     // split the row into col 1 and 2 based on ,
  56.                 csvArray[index][0] = kv[0];                             // set first index to the abbreviation
  57.                 csvArray[index][1] = kv[1];                             // set second index to the full value
  58.                 index++;
  59.             }
  60.  
  61.             return csvArray;
  62.  
  63.             // Catch Exceptions
  64.         } catch(FileNotFoundException e) {
  65.             System.out.println(String.format("Cannot Find %s", csvName));
  66.         } catch (IOException e) {
  67.             System.out.println("Error Reading File");
  68.         } catch (IndexOutOfBoundsException e) {
  69.             System.out.println("Too Many Abbreviations");
  70.         }
  71.  
  72.         // If exception is caught, set first index to null
  73.         csvArray[0][0] = null;
  74.         return csvArray;
  75.     }
  76.  
  77.  
  78.     //Random sentences to test the program.
  79.     public static ArrayList makeSentences(String file){
  80.         try{
  81.             BufferedReader buf = new BufferedReader(new FileReader(file));
  82.             ArrayList<String> list = new ArrayList<>();
  83.             String row;
  84.             while((row = buf.readLine()) != null){
  85.                 list.add(row);
  86.             }
  87.             System.out.println(list.size());
  88. return list;
  89.  
  90.         }catch (FileNotFoundException e){
  91.             System.out.println("File not found. Please place the correct file in the folder.");
  92.         }catch (IOException e){
  93.             System.out.println("Error, IO Exception");
  94.         }
  95.     return null;
  96.     }
  97.  
  98.    
  99.  
  100.  
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement