Example #1
0
  private void printTopWordsUnderTopics(
      ArrayList<ArrayList<ItemWithValue>> topWordsUnderTopics, String filepath) {
    StringBuilder sbOutput = new StringBuilder();

    // Print out the first row with "Topic k".
    for (int k = 0; k < topWordsUnderTopics.size(); ++k) {
      sbOutput.append("Topic " + k);
      sbOutput.append("\t");
    }
    sbOutput.append(System.getProperty("line.separator"));

    for (int pos = 0; pos < topWordsUnderTopics.get(0).size(); ++pos) {
      StringBuilder sbLine = new StringBuilder();
      for (int k = 0; k < topWordsUnderTopics.size(); ++k) {
        ArrayList<ItemWithValue> topWords = topWordsUnderTopics.get(k);
        ItemWithValue iwv = topWords.get(pos);
        String wordstr = iwv.getIterm().toString();
        sbLine.append(wordstr);
        sbLine.append("\t");
      }
      sbOutput.append(sbLine.toString().trim());
      sbOutput.append(System.getProperty("line.separator"));
    }
    FileReaderAndWriter.writeFile(filepath, sbOutput.toString());
  }
Example #2
0
 public void printToFile(String filepath) {
   StringBuilder sbOutput = new StringBuilder();
   for (Map.Entry<Integer, String> entry : wordidToWordstrMap.entrySet()) {
     int wordid = entry.getKey();
     String wordstr = entry.getValue();
     sbOutput.append(wordid + ":" + wordstr);
     sbOutput.append(System.getProperty("line.separator"));
   }
   FileReaderAndWriter.writeFile(filepath, sbOutput.toString());
 }
Example #3
0
 private void printDocs(int[][] docs, String filePath) {
   StringBuilder sbOutput = new StringBuilder();
   for (int[] doc : docs) {
     StringBuilder sbLine = new StringBuilder();
     for (int word : doc) {
       sbLine.append(word + " ");
     }
     sbOutput.append(sbLine.toString().trim());
     sbOutput.append(System.getProperty("line.separator"));
   }
   FileReaderAndWriter.writeFile(filePath, sbOutput.toString());
 }
Example #4
0
  /**
   * Read the vocabulary from the file.
   *
   * @param filePath
   * @return
   */
  public static Vocabulary getVocabularyFromFile(String filePath) {
    Vocabulary vocab = new Vocabulary();

    ArrayList<String> lines = FileReaderAndWriter.readFileAllLines(filePath);
    for (String line : lines) {
      String[] splits = line.trim().split(":");
      ExceptionUtility.assertAsException(splits.length == 2);
      int wordid = Integer.parseInt(splits[0]);
      String wordstr = splits[1];
      vocab.addWordstrWithWordid(wordid, wordstr);
    }

    return vocab;
  }
Example #5
0
 public void printToFile(String filePath) {
   FileReaderAndWriter.writeFile(filePath, toString());
 }