Package domain

Source Code of domain.DictionaryController

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package domain;

import data.FileControllerD;
import data.UserController;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author oriol.cano
*/
public class DictionaryController {
    private static String _path;
    private static String _sep = System.getProperty("file.separator");


  public DictionaryController() {
//    String jarPath = System.getProperty("java.class.path");
//    int lastSlash = jarPath.lastIndexOf(_sep);
//    _path = jarPath.substring(0,lastSlash + 1);

       String jarPath = System.getProperty("java.class.path");
       int lastDot = jarPath.lastIndexOf(";");
       String aux = jarPath;
       if (lastDot != -1) {
            aux = jarPath.substring(0, lastDot);
       }
       int lastSlash = aux.lastIndexOf(_sep);
       _path = aux.substring(0,lastSlash + 1);
  }
   
    public TestDictionary createNounDictionary(int userID) {
        String textAlpha = readTextAlpha(); //SENSE SIGNES O CLEANSTRING
      TextAlpha ta = new TextAlpha(textAlpha);
        Model model = new Model(ta);
        readModel(model, userID);
        TestDictionary NounDictionary = new TestDictionary(model);
        readDictionary(NounDictionary);
        return NounDictionary;
    }

     public VerbDictionary createVerbDictionary(int userID) {
        UserController _uc = new UserController();
        BufferedReader added = _uc.getVerbs(userID);
    FileControllerD _fc = new FileControllerD();
    BufferedReader verbs = _fc.getFileR(_path+"files"+_sep+"verbs.txt");
    BufferedReader irregulars = _fc.getFileR(_path+"files"+_sep+"irregulars.txt");
        VerbDictionary _vd = new VerbDictionary();
        _vd.inicialize(verbs, irregulars, added);
    try {
      if(added != null) added.close();
      verbs.close();
      irregulars.close();
    } catch (IOException ex) {
      //Logger.getLogger(DictionaryController.class.getName()).log(Level.SEVERE, null, ex);
    }
   
        return _vd;
    }
    /*protected Model createModel() {
        String textAlpha = readTextAlpha(); //SENSE SIGNES O CLEANSTRING
  TextAlpha ta = new TextAlpha(textAlpha);
        Model model = new Model(ta);
        readModel(model);
        return model;
    }*/
   
   
    private void readDictionary(TestDictionary NounDictionary) {
        String noun = null;
        FileControllerD fc = new FileControllerD();
        BufferedReader input = fc.getFileR(_path + "files" + _sep + "nouns.txt");
        try {
            noun = input.readLine();
        } catch (IOException ex) {
            Logger.getLogger(DictionaryController.class.getName()).log(Level.SEVERE, null, ex);
        }
        while(noun != null) {
            try {
          NounDictionary.addWord(noun);
                    noun = input.readLine();
            }
            catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

  

    private static void readModel(Model model, int userID) {

    String parella = null;
    String first = null;
    String second = null;
        String user = userID+"";
    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()) {
        second = tokens.nextToken(); ///SWAP FET
        first = 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);
      }

    }

        if (userID != 0) {
            input = fc.getFileR(_path + "users" + _sep + user + _sep + "usertraining.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()) {
                    second = tokens.nextToken(); ///SWAP FET
                    first = 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 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;
    }

    public void addVerbToDictionary(String s, int userID) {
        try {

        String user = userID+"";
        FileControllerD _fc = new FileControllerD();
        BufferedWriter _output = _fc.getFileW(_path+"users"+_sep+user+_sep+"added.txt");

       _output.append(s);
       _output.newLine();
       _output.close();


       }
       catch(Exception e) {
          /* String user = userID+"";
           File aux = new File(_path+"users"+_sep+user);
       boolean b = aux.mkdir();
       File _file = new File(_path+"users"+_sep+user+_sep+"added.txt");

            try {
                b = _file.createNewFile();
                _output = _fc.getFileW(_path+"users"+_sep+user+_sep+"added.txt");

                _output.append(s);
                _output.newLine();
                _output.close();
                 includeVerb(s);
            } catch (IOException ex) {
                Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, ex);
            }
           //_file = _fc.getFile("."+_sep+"users"+_sep+user+_sep+"added.txt");



        //Logger.getLogger(VerbDictionary.class.getName()).log(Level.SEVERE, null, e);
       */}
    }

    public void addTrainingPair(String noun, String infinitive, int userID) {
        try {

        String user = userID+"";
        FileControllerD _fc = new FileControllerD();
        BufferedWriter _output = _fc.getFileW(_path+"users"+_sep+user+_sep+"usertraining.txt");

    for (int i = 0; i < 5; ++i) {
       _output.append(noun+" "+infinitive);
       _output.newLine();
    }
    _output.close();


       }
       catch(Exception e) {

       }
    }

}
TOP

Related Classes of domain.DictionaryController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.