brilliant_moves

SwearWordRemover.java

Jan 7th, 2016
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.28 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.FileReader;
  4. import java.io.BufferedReader;
  5. import java.io.FileWriter;
  6. import java.io.BufferedWriter;
  7.  
  8. public class SwearWordRemover {
  9.  
  10.     /*
  11.      *  Program: SwearWordRemover.java
  12.      *  Purpose: Remove swear words from file, replace them with blanked-out versions of the words.
  13.      *  Creator: Chris Clarke
  14.      *  Created: 07.01.2016
  15.      */
  16.  
  17. /*
  18.  
  19. Required: a file called "swearwords.txt" containing the following words (or similar):
  20.  
  21. stupid s***i*
  22. bloody bl**dy
  23. fuck f**k
  24. fucking f***ing
  25. fucker f***er
  26. arsehole a****ole
  27. shit sh*t
  28. shite sh*t*
  29. twat tw*t
  30. cunt c*nt
  31. prick pr**k
  32. wanker w**k*r
  33. pissed pi**ed
  34. damn d*mn
  35.  
  36. Also required: make a test file: "SwearTestData.txt" which includes swearwords.
  37.  
  38. Note that the output is sent to file "output.txt".
  39.  
  40. */
  41.  
  42.     public static String[] openFile(String path) throws IOException {
  43.         int numberOfLines = readLines(path);
  44.  
  45.         FileReader fr = new FileReader(path);
  46.         BufferedReader textReader = new BufferedReader(fr);
  47.  
  48.         String[] textData = new String[numberOfLines];
  49.  
  50.         int i;
  51.  
  52.         for(i=0; i < numberOfLines; i++) {
  53.             textData[i] = textReader.readLine();
  54.             //System.out.println(textData[i]);  // display line on screen
  55.         }
  56.         textReader.close();
  57.         return textData;
  58.     } // openFile()
  59.  
  60.     public static int readLines(String path) throws IOException {
  61.  
  62.         FileReader file_to_read = new FileReader(path);
  63.         BufferedReader bf = new BufferedReader(file_to_read);
  64.  
  65.         String aLine;
  66.         int numberOfLines = 0;
  67.  
  68.         while (( aLine = bf.readLine()) != null) {
  69.             numberOfLines++;
  70.         }
  71.         bf.close();
  72.         return numberOfLines;
  73.     } // readLines()
  74.  
  75.     public static void main(String[] args) throws IOException {
  76.  
  77.         String[] data = openFile("SwearTestData.txt");
  78.         String[] sweary = openFile("swearwords.txt");
  79.         String[] wordsInput;
  80.         String out = "", word="";
  81.         char punct = ' '; // punctuation
  82.         char firstLetter = ' ';
  83.         String[] swearLine;
  84.         boolean foundSwear;
  85.  
  86.         for (int i=0; i<data.length; i++) {
  87.             wordsInput = data[i].split(" ");
  88.             for (int j=0; j<wordsInput.length; j++) {
  89.                 word = wordsInput[j];
  90.                 punct = ' ';
  91.                 if (word.endsWith(",") || word.endsWith(".") || word.endsWith(";")
  92.                  || word.endsWith(":") || word.endsWith("?") || word.endsWith("!")) {
  93.                     punct = word.charAt(word.length()-1);
  94.                     word = word.substring(0, word.length()-1);
  95.                 } // if
  96.                 foundSwear = false;
  97.                 if (Character.isUpperCase (word.charAt(0))) {
  98.                     firstLetter = word.charAt(0);
  99.                 } else {
  100.                     firstLetter = ' ';
  101.                 } // if
  102.                 for (int k=0; k<sweary.length; k++) {
  103.                     swearLine = sweary[k].split(" ");
  104.                     if (word.equalsIgnoreCase(swearLine[0])) {
  105.                         foundSwear = true;
  106.                         if (firstLetter==' ') {
  107.                             out += swearLine[1]+punct;
  108.                         } else {
  109.                             out += firstLetter+swearLine[1].substring(1, word.length())+punct;
  110.                         } // if
  111.                     } // if
  112.                 } // for k
  113.                 if (!foundSwear) {
  114.                     if (firstLetter==' ') {
  115.                         out += word+punct;
  116.                     } else {
  117.                         out += firstLetter+word.substring(1, word.length())+punct;
  118.                     } // if
  119.                 } // if
  120.             } // for j
  121.             out += "\r\n"; // (windows)
  122.         } // for i
  123.  
  124.         BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Output.txt")));
  125.         bw.write(out);
  126.         bw.close();
  127.     } // main()
  128.  
  129. } // class SwearWordRemover
Advertisement
Add Comment
Please, Sign In to add comment