Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.IOException;
- import java.io.FileReader;
- import java.io.BufferedReader;
- import java.io.FileWriter;
- import java.io.BufferedWriter;
- public class SwearWordRemover {
- /*
- * Program: SwearWordRemover.java
- * Purpose: Remove swear words from file, replace them with blanked-out versions of the words.
- * Creator: Chris Clarke
- * Created: 07.01.2016
- */
- /*
- Required: a file called "swearwords.txt" containing the following words (or similar):
- stupid s***i*
- bloody bl**dy
- fuck f**k
- fucking f***ing
- fucker f***er
- arsehole a****ole
- shit sh*t
- shite sh*t*
- twat tw*t
- cunt c*nt
- prick pr**k
- wanker w**k*r
- pissed pi**ed
- damn d*mn
- Also required: make a test file: "SwearTestData.txt" which includes swearwords.
- Note that the output is sent to file "output.txt".
- */
- public static String[] openFile(String path) throws IOException {
- int numberOfLines = readLines(path);
- FileReader fr = new FileReader(path);
- BufferedReader textReader = new BufferedReader(fr);
- String[] textData = new String[numberOfLines];
- int i;
- for(i=0; i < numberOfLines; i++) {
- textData[i] = textReader.readLine();
- //System.out.println(textData[i]); // display line on screen
- }
- textReader.close();
- return textData;
- } // openFile()
- public static int readLines(String path) throws IOException {
- FileReader file_to_read = new FileReader(path);
- BufferedReader bf = new BufferedReader(file_to_read);
- String aLine;
- int numberOfLines = 0;
- while (( aLine = bf.readLine()) != null) {
- numberOfLines++;
- }
- bf.close();
- return numberOfLines;
- } // readLines()
- public static void main(String[] args) throws IOException {
- String[] data = openFile("SwearTestData.txt");
- String[] sweary = openFile("swearwords.txt");
- String[] wordsInput;
- String out = "", word="";
- char punct = ' '; // punctuation
- char firstLetter = ' ';
- String[] swearLine;
- boolean foundSwear;
- for (int i=0; i<data.length; i++) {
- wordsInput = data[i].split(" ");
- for (int j=0; j<wordsInput.length; j++) {
- word = wordsInput[j];
- punct = ' ';
- if (word.endsWith(",") || word.endsWith(".") || word.endsWith(";")
- || word.endsWith(":") || word.endsWith("?") || word.endsWith("!")) {
- punct = word.charAt(word.length()-1);
- word = word.substring(0, word.length()-1);
- } // if
- foundSwear = false;
- if (Character.isUpperCase (word.charAt(0))) {
- firstLetter = word.charAt(0);
- } else {
- firstLetter = ' ';
- } // if
- for (int k=0; k<sweary.length; k++) {
- swearLine = sweary[k].split(" ");
- if (word.equalsIgnoreCase(swearLine[0])) {
- foundSwear = true;
- if (firstLetter==' ') {
- out += swearLine[1]+punct;
- } else {
- out += firstLetter+swearLine[1].substring(1, word.length())+punct;
- } // if
- } // if
- } // for k
- if (!foundSwear) {
- if (firstLetter==' ') {
- out += word+punct;
- } else {
- out += firstLetter+word.substring(1, word.length())+punct;
- } // if
- } // if
- } // for j
- out += "\r\n"; // (windows)
- } // for i
- BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Output.txt")));
- bw.write(out);
- bw.close();
- } // main()
- } // class SwearWordRemover
Advertisement
Add Comment
Please, Sign In to add comment