Esempio n. 1
0
  /**
   * Generates the LDA model using the documents saved Uses alpha = 0.5 and beta=0.1 ntopics = 3 and
   * 5 words pr topic.
   */
  public void LDAModel() {

    // TODO Auto-generated method stub
    LDACmdOption option = new LDACmdOption();
    CmdLineParser parser = new CmdLineParser(option);
    System.out.println("Working Directory = " + System.getProperty("user.dir"));
    String[] inputSplits =
        ("-est -alpha 0.5 -beta 0.1 -ntopics 3 -niters 1000 -savestep 100 -twords 5 -dfile newdocs.dat -inf -dir "
                + System.getProperty("user.dir")
                + "/model")
            .split(" ");
    String[] args = inputSplits;
    try {
      if (args.length == 0) {
        System.out.println("No arguments");
        return;
      }

      parser.parseArgument(args);

      if (option.est || option.estc) {
        Estimator estimator = new Estimator();
        estimator.init(option);
        estimator.estimate();
      } else if (option.inf) {
        Inferencer inferencer = new Inferencer();
        inferencer.init(option);

        Model newModel = inferencer.inference();

        for (int i = 0; i < newModel.phi.length; ++i) {
          // phi: K * V
          System.out.println("-----------------------\ntopic" + i + " : ");
          for (int j = 0; j < 10; ++j) {
            System.out.println(inferencer.globalDict.id2word.get(j) + "\t" + newModel.phi[i][j]);
          }
        }
      }
    } catch (CmdLineException cle) {
      System.out.println("Command line error: " + cle.getMessage());

      return;
    } catch (Exception e) {
      System.out.println("Error in main: " + e.getMessage());
      e.printStackTrace();
      return;
    }
  }