Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.text.*;
- import java.io.*;
- import java.nio.charset.StandardCharsets;
- public class Week2{
- public static void main(String []args){
- /*String text = readUTF8File("English.txt");
- text = RNA(text);
- String[] testText = string2Array(text);
- arrayWrite("EngArrayOut.txt", testText);*/
- //writeUTF8File("EnglishOut.txt", nonalpha);
- bigramTest("blue");
- }
- public static String readUTF8File(String filePath){
- StringBuilder fileContent = new StringBuilder();
- try{
- Reader reader = new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8);
- BufferedReader bufferedReader = new BufferedReader(reader);
- String s;
- while ((s = bufferedReader.readLine()) != null){
- fileContent.append(s + "\n");
- }
- bufferedReader.close();
- reader.close();
- } catch (Exception e){
- e.printStackTrace();
- }
- return fileContent.toString();
- }//End readUTF8File
- public static void writeUTF8File(String filePath, String content){
- try{
- Writer writer = new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8);
- PrintWriter printWriter = new PrintWriter(writer);
- writer.write(content);
- writer.close();
- printWriter.close();
- } catch (Exception e){
- e.printStackTrace();
- }
- }//End writeUTF8File
- public static String RNA(String content){ //RemoveNonAlphabeticals
- content = content.replaceAll("[^a-zA-Z ]", "");
- content = content.toLowerCase();
- return content;
- }//end RNA
- public static String[] string2Array(String toConvert){
- String[] newArray = toConvert.split(" ");
- return newArray;
- }
- //Just for testing
- public static void arrayWrite (String fileName, String[] content){
- try {
- Writer writer = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);
- PrintWriter printWriter = new PrintWriter(writer);
- for (int i = 0; i < content.length; i++){
- writer.write(content[i]+",");
- }
- writer.close();
- printWriter.close();
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- public static void bigramTest(String word){
- String[] letters = word.split("");
- for(int i = 0; i < letters.length; i++){
- System.out.print(letters[i]);
- System.out.print(" ");
- }
- ArrayList<String> bigrams = new ArrayList<String>();
- for(int i = 0; i < letters.length; i++){
- int y = i+1;
- if(y <= i){
- String x = letters[i];
- x = x.concat(letters[y]);
- bigrams.add(x);
- }
- }
- String bigramList = Arrays.toString(bigrams.toArray());
- System.out.println(bigramList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement