Example usage for com.google.common.collect Multiset toArray

List of usage examples for com.google.common.collect Multiset toArray

Introduction

In this page you can find the example usage for com.google.common.collect Multiset toArray.

Prototype

<T> T[] toArray(T[] a);

Source Link

Document

Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

Usage

From source file:de.andreasschoknecht.LS3.DocumentCollection.java

/**
 * Insert a model to a model collection. This means that the underlying Term-Document Matrix has to be updated.
 *
 * @param modelPath the path to the model to be inserted.
 *//*  w w w .ja  v a2 s .  c  o m*/
public void insertModel(String modelPath) {
    // Make sure file name is correct
    if (!modelPath.endsWith(".pnml"))
        modelPath = modelPath + ".pnml";

    // Create new LS3Document object and add it to the document collection list of documents
    System.out.println("------------------------");
    System.out.println("Model to insert:");
    System.out.println("------------------------");
    System.out.println(modelPath.substring(modelPath.lastIndexOf(File.separator) + 1));
    System.out.println("------------------------");
    System.out.println("Models in list:");
    System.out.println("------------------------");

    String[] updatedFileList = new String[fileList.length + 1];
    for (int i = 0; i <= fileList.length; i++) {
        if (i != fileList.length)
            updatedFileList[i] = fileList[i];
        else
            updatedFileList[i] = modelPath.substring(modelPath.lastIndexOf(File.separator) + 1);

        System.out.println(updatedFileList[i]);

    }

    documentNumber++;

    LS3Document newDocument = new LS3Document(modelPath);
    PNMLReader pnmlReader = new PNMLReader();
    try {
        pnmlReader.processDocument(newDocument);
    } catch (JDOMException | IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("------------------------");
    System.out.println("New LS3Document data:");
    System.out.println("------------------------");
    System.out.println(newDocument.getPNMLPath());
    System.out.println("Amount of terms = " + newDocument.getAmountTerms());
    for (String term : newDocument.getTermCollection()) {
        System.out.println(term);
    }

    // Add new column to the Term-Document Matrix
    int t = tdMatrix.getRowNumber();
    double[] termFrequencies = new double[t];
    String[] termCollectionArray = new String[termCollection.size()];
    termCollection.toArray(termCollectionArray);

    Multiset<String> termsM = newDocument.getTermCollection();
    for (int i = 0; i < t; i++) {
        termFrequencies[i] = termsM.count(termCollectionArray[i]);
        termsM.remove(termCollectionArray[i]);
    }
    System.out.println("------------------------");
    System.out.println("Term frequencies:");
    System.out.println("------------------------");
    System.out.println(Arrays.toString(termFrequencies));

    System.out.println("------------------------");
    System.out.println("Old TD Matrix:");
    System.out.println("------------------------");
    for (int i = 0, k = tdMatrix.getRowNumber(); i < k; i++) {
        System.out.print(tdMatrix.getTermArray()[i] + " ");
        for (int j = 0, l = tdMatrix.getColumnNumber(); j < l; j++) {
            System.out.print(" " + tdMatrix.getMatrix()[i][j] + " ");
        }
        System.out.println("");
    }
    System.out.println("---------------------\r\n\r\n");

    tdMatrix.addColumn(termFrequencies);

    System.out.println("------------------------");
    System.out.println("New TD Matrix:");
    System.out.println("------------------------");
    for (int i = 0, k = tdMatrix.getRowNumber(); i < k; i++) {
        System.out.print(tdMatrix.getTermArray()[i] + " ");
        for (int j = 0, l = tdMatrix.getColumnNumber(); j < l; j++) {
            System.out.print(" " + tdMatrix.getMatrix()[i][j] + " ");
        }
        System.out.println("");
    }
    System.out.println("---------------------\r\n\r\n");

    // Add new terms of the new model to the term list of the document collection
    System.out.println("------------------------");
    System.out.println("Old term collection:");
    System.out.println("------------------------");
    for (String term : termCollection) {
        System.out.println(term);
    }

    System.out.println("------------------------");
    System.out.println("Terms remaining in insertion model:");
    System.out.println("------------------------");
    System.out.println(Arrays.toString(termsM.toArray(new String[termsM.size()])));

    Set<String> termSet = termsM.elementSet();
    String[] newTerms = termSet.toArray(new String[termSet.size()]);
    for (String term : newTerms) {
        termCollection.add(term);
    }

    System.out.println("------------------------");
    System.out.println("New term collection:");
    System.out.println("------------------------");
    for (String term : termCollection) {
        System.out.println(term);
    }

    System.out.println("------------------------");
    System.out.println("New term collection TD Matrix:");
    System.out.println("------------------------");
    for (String term : tdMatrix.getTermArray()) {
        System.out.println(term);
    }

    //  Add one row for each new term and add the corresponding Term-Document Matrix entries
    double[] newTermsFrequencies = new double[newTerms.length];
    for (int i = 0; i < newTerms.length; i++) {
        newTermsFrequencies[i] = termsM.count(newTerms[i]);
    }

    System.out.println("------------------------");
    System.out.println("New term frequencies:");
    System.out.println("------------------------");
    System.out.println(Arrays.toString(newTermsFrequencies));

    int n = tdMatrix.getColumnNumber();
    for (int i = 0; i < newTermsFrequencies.length; i++) {
        double[] newRow = new double[n];
        for (int j = 0; j < n - 2; j++)
            newRow[j] = 0;

        newRow[n - 1] = newTermsFrequencies[i];
        tdMatrix.addRow(newRow);
    }

    // Update term list of TDMatrix object
    tdMatrix.setTermArray(termCollection.toArray(new String[0]));

    System.out.println("------------------------");
    System.out.println("Final TD Matrix:");
    System.out.println("------------------------");
    for (int i = 0, k = tdMatrix.getRowNumber(); i < k; i++) {
        System.out.print(tdMatrix.getTermArray()[i] + " ");
        for (int j = 0, l = tdMatrix.getColumnNumber(); j < l; j++) {
            System.out.print(" " + tdMatrix.getMatrix()[i][j] + " ");
        }
        System.out.println("");
    }
    System.out.println("---------------------\r\n\r\n");

}