/**
*
* @author marc.molins.piulachs
*
*/
package domain;
import data.FileControllerD;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
public class Main {
private static int _actualUser = 1;
private static String _path;
private static String _sep = System.getProperty("file.separator");
private static String readTextAlpha() {
String linia = null;
String text = "";
FileControllerD fc = new FileControllerD();
BufferedReader input = fc.getFileR(_path + "files" + _sep + "textalpha.txt");
try {
linia = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Boolean primer = true;
while(linia != null) {
if (primer) {
text = linia;
primer = false;
}
else {
text = text + " " + linia;
}
try {
linia = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
}
}
return text;
}
private static void restartModel() {
File file = new File(_path + "files" + _sep + "newrules.txt");
file.delete();
try {
file.createNewFile();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void readNounsDictionary(TestDictionary td) {
String nom = null;
FileControllerD fc = new FileControllerD();
BufferedReader input = fc.getFileR(_path + "files" + _sep + "nouns.txt");
try {
nom = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
while(nom != null) {
td.addWord(nom);
try {
nom = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private static void readModel(Model model) {
String parella = null;
String first = null;
String second = null;
ArrayList<Pair<String, String>> training = new ArrayList<Pair<String, String>>();
StringTokenizer tokens = null;
FileControllerD fc = new FileControllerD();
BufferedReader input = fc.getFileR(_path + "files" + _sep + "training.txt");
try {
parella = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
while (parella != null) {
tokens = new StringTokenizer(parella);
if (tokens.hasMoreTokens()) {
first = tokens.nextToken();
second = tokens.nextToken();
if (!first.equals(second)) {
Pair<String,String> pair = new Pair<String,String>(first, second);
training.add(pair);
}
}
try {
parella = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
input = fc.getFileR(_path + "files" + _sep + "newrules.txt");
try {
parella = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
while (parella != null) {
tokens = new StringTokenizer(parella);
if (tokens.hasMoreTokens()) {
first = tokens.nextToken();
second = tokens.nextToken();
if (!first.equals(second)) {
Pair<String,String> pair = new Pair<String,String>(first, second);
training.add(pair);
}
}
try {
parella = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
model.generateModel(training);
}
private static String readTextFromFile() {
String linia = null;
String text = "";
FileControllerD fc = new FileControllerD();
BufferedReader input = fc.getFileR(_path + "files" + _sep + "text.txt");
try {
linia = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
Boolean primer = true;
while(linia != null) {
if (primer) {
text = linia;
primer = false;
}
else {
text = text + "\n" + linia;
}
try {
linia = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Loaded correctly");
return text;
}
private static String readTextFromConsole() {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = null;
try {
s = br.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
String total = "";
Boolean primer = true;
while (!s.equals(".")) { // per acabar de llegir posar una nova linia amb un punt
if (primer) {
total = s;
primer = false;
}
else {
total = total + "\n" + s;
}
try {
s = br.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println("Loaded correctly");
return total;
}
private static void updateTraining(ArrayList<Pair<String,String>> newTr) {
BufferedWriter out = null;
Iterator i = newTr.iterator();
Pair<String,String> pair = new Pair<String,String>();
String linia;
try {
out = new BufferedWriter(new FileWriter(_path + "files" + _sep + "newrules.txt", true));
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
while (i.hasNext()) {
try {
pair = (Pair<String,String>) i.next();
linia = pair.first + " " + pair.second;
out.newLine();
out.write(linia);
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
out.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void printTextOptions() {
System.out.println("\n1. Enter a new text");
System.out.println("2. Load a new text from \"./files/text.txt\"");
System.out.println("3. Start nominalization");
System.out.println("4. Next nominalization");
System.out.println("5. Previous nominalization");
System.out.println("6. Add verb to dictionary");
System.out.println("7. Change parameter N (integer between 1 and 10)");
System.out.println("8. Change parameter SUGGESTIONS (integer minimum 1)");
System.out.println("9. Change parameter GAMMA (float between 0 and 1)");
System.out.println("10. Show text statistics");
System.out.println("11. Close the program and restart model");
System.out.println("12. Close the program and update model");
System.out.println("13. Close the program\n");
}
private static void textOptions(Text txt, VerbDictionary verbDict, TextStatistics textStat) {
try {
String tx;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
printTextOptions();
int var = Integer.parseInt(br.readLine());
Boolean end = false;
while (!end) {
switch (var) {
case 1:
System.out.println("Write below: (to finish, type a period in a new line)\n");
tx = readTextFromConsole();
txt.enterText(tx);
break;
case 2:
System.out.print("\n");
tx = readTextFromFile();
txt.enterText(tx);
break;
case 3:
System.out.print("\n");
txt.startNominalization();
break;
case 4:
System.out.print("\n");
txt.nextNominalization();
break;
case 5:
System.out.print("\n");
txt.previousNominalization();
break;
case 6:
System.out.print("\nAdd a new verb: ");
String nv = br.readLine();
verbDict.addVerbToDict(nv);
break;
case 7:
System.out.println("\nCurrent N: " + Constants.getN());
System.out.print("Enter new N: ");
int N = Integer.parseInt(br.readLine());
if (N < 1) {
N = 1;
}
Constants.setN(N);
break;
case 8:
System.out.println("\nCurrent SUGGESTIONS: " + Constants.getSUGGESTIONS());
System.out.print("Enter new SUGGESTIONS: ");
int SUGG = Integer.parseInt(br.readLine());
if (SUGG < 1) {
SUGG = 1;
}
Constants.setSUGGESTIONS(SUGG);
break;
case 9:
System.out.println("\nCurrent GAMMA: " + Constants.getGAMMA());
System.out.print("Enter new GAMMA: ");
float GAMMA = Float.parseFloat(br.readLine());
if (GAMMA < 0) {
GAMMA = 0;
}
else if (GAMMA > 1) {
GAMMA = 1;
}
Constants.setGAMMA(GAMMA);
break;
case 10:
textStat.setTotalWords(txt.getTotalWords());
textStat.setMarkedWords(txt.getTotalVerbs());
textStat.setLearntRules(txt.getLearntRules());
textStat.setMostCommonVerb(txt.getMostCommonVerb());
System.out.println("\nTotal words: " + textStat.getTotalWords());
System.out.println("Total verbs: " + textStat.getMarkedWords());
System.out.println("Learnt rules: " + textStat.getLearntRules());
System.out.println("Most common verb: " + textStat.getMostCommonVerb());
break;
case 11:
end = true;
restartModel();
break;
case 12:
end = true;
// updateTraining(txt.getNewTraining());
break;
case 13:
end = true;
break;
default:
System.out.println("Incorrect option");
}
try {
Thread.sleep(700);
}
catch (InterruptedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
if (!end) {
printTextOptions();
var = Integer.parseInt(br.readLine());
}
}
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String[] args) {
String jarPath = System.getProperty("java.class.path");
int lastSlash = jarPath.lastIndexOf(_sep);
_path = jarPath.substring(0,lastSlash + 1);
// String textAlpha = readTextAlpha();
// TextAlpha ta = new TextAlpha(textAlpha);
//
// VerbDictionary verbDict = new VerbDictionary();
//
// Model model = new Model(ta);
//
// readModel(model);
// TestDictionary nounDict = new TestDictionary(model);
// readNounsDictionary(nounDict);
// Text txt = new Text(verbDict, nounDict);
// TextStatistics textStat = new TextStatistics();
//
// textOptions(txt, verbDict, textStat);
// tc.enterText("I play");
// tc.startNominalization();
// tc.getNewTraining();*/
//}
}
}
/*
package javaapplication3;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
private final static String PUNCTUATION_MARKS = "?!.;,:()[]{}<>\"\'—¿¡«»*";
private static Boolean isPunctuationMark(char c) {
return PUNCTUATION_MARKS.indexOf(c) != -1;
}
private static String cleanString(String s) {
StringBuilder temp = new StringBuilder(s.length());
for (int i = 0; i < s.length(); ++i) {
if (!isPunctuationMark(s.charAt(i))) {
temp.append(Character.toLowerCase(s.charAt(i)));
}
}
return temp.toString();
}
public static void main(String[] args) {
String _sep = System.getProperty("file.separator");
String jarPath = System.getProperty("java.class.path");
int lastSlash = jarPath.lastIndexOf(_sep);
String _path = jarPath.substring(0,lastSlash + 1);
String linia = null;
String text = "";
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(_path + "newtext.txt", false));
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
FileControllerD fc = new FileControllerD();
BufferedReader input = fc.getFileR(_path + "text.txt");
try {
linia = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
while(linia != null) {
try {
out.write(cleanString(linia));
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
try {
out.newLine();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
try {
linia = input.readLine();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
try {
out.close();
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
*/