Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class bruteForceSol {
- public static void main(String [] args) {
- String[][] arr = csvToArray("./src/abr.csv");
- ArrayList<String> list = makeSentences("./src/sxswTweets.csv");
- String[] yes = new String [40000];
- //Splits the words into an array, removing ALL punctuation.
- for(int i = 0 ; i < list.size() ; i++) {
- yes = list.get(i).replaceAll("\\p{Punct}", "").split(" ");
- }
- //Iterates through all abbreviations and checks each word in the tweet to see if it matches an abbreviation.
- for(int i = 0 ; i < yes.length; i++){
- for(int k = 0 ; k < arr.length ; k++){
- if(yes[i].toLowerCase().equals((arr[k][0]).toLowerCase())){
- yes[i] = arr[k][1];
- }
- }
- }
- for(int i = 0 ; i < yes.length; i++){
- System.out.print(yes[i]);
- }
- }
- public static String[][] csvToArray(String csvName) {
- //Notice how the array length must be the size of the number of abbreviations. Another reason to not use this solution.
- String[][] csvArray = new String[74][2]; // create a multidimensional array that can hold 100 abbreviations
- try {
- BufferedReader reader = new BufferedReader(new FileReader(csvName));
- reader.readLine(); // read first line to remove column titles
- String row;
- int index = 0;
- // O(m) runtime, where m is the number of lines(abbreviations)
- while ((row = reader.readLine()) != null) { // read until end of file
- String[] kv = row.split(","); // split the row into col 1 and 2 based on ,
- csvArray[index][0] = kv[0]; // set first index to the abbreviation
- csvArray[index][1] = kv[1]; // set second index to the full value
- index++;
- }
- return csvArray;
- // Catch Exceptions
- } catch(FileNotFoundException e) {
- System.out.println(String.format("Cannot Find %s", csvName));
- } catch (IOException e) {
- System.out.println("Error Reading File");
- } catch (IndexOutOfBoundsException e) {
- System.out.println("Too Many Abbreviations");
- }
- // If exception is caught, set first index to null
- csvArray[0][0] = null;
- return csvArray;
- }
- //Random sentences to test the program.
- public static ArrayList makeSentences(String file){
- try{
- BufferedReader buf = new BufferedReader(new FileReader(file));
- ArrayList<String> list = new ArrayList<>();
- String row;
- while((row = buf.readLine()) != null){
- list.add(row);
- }
- System.out.println(list.size());
- return list;
- }catch (FileNotFoundException e){
- System.out.println("File not found. Please place the correct file in the folder.");
- }catch (IOException e){
- System.out.println("Error, IO Exception");
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement