Menu

Code Snippets

George Giannakopoulos

Overview
The main tasks one usually needs to perform using n-gram graphs are:
Create an n-gram graph from a string
Output a graph as a DOT string
Create an n-gram graph from a file
Compare two graphs, extracting their similarity
Merge two (or more) graphs
Save and load a graph

We will provide samples in the following sections, tackling the above problems.

Create an n-gram graph from a string

...
import gr.demokritos.iit.jinsect.documentModel.representations.DocumentNGramGraph;

...
 // The string we want to represent
 String sTmp = "Hello graph!";

 // The default document n-gram graph, with min n-gram size and max n-gram size set to 3, and the dist parameter set to 3.
DocumentNGramGraph dngGraph = new DocumentNGramGraph(); 

// Create the graph
dngGraph.setDataString(sTmp); 

Output a graph as a DOT string

        /* The following command gets the first n-gram graph level (with the minimum n-gram
        size) and renders it, using the utils package, as a DOT string */
        System.out.println(utils.graphToDot(dngGraph.getGraphLevel(0), true));

Create an n-gram graph from a file

...
import gr.demokritos.iit.jinsect.documentModel.comparators.NGramCachedGraphComparator;
import gr.demokritos.iit.jinsect.documentModel.representations.DocumentNGramGraph;
import gr.demokritos.iit.jinsect.structs.GraphSimilarity;
import gr.demokritos.iit.jinsect.utils;
import java.io.IOException;
...
        // The filename of the file the contents of which will form the graph
        String sFilename = "file.txt";
        DocumentNGramGraph dngGraph = new DocumentNGramGraph(); 
        // Load the data string from the file, also dealing with exceptions
        try {
            dngGraph.loadDataStringFromFile(sFilename);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

Compare two graphs, extracting their similarity

...
import gr.demokritos.iit.jinsect.documentModel.comparators.NGramCachedGraphComparator;
import gr.demokritos.iit.jinsect.documentModel.representations.DocumentNGramGraph;
import gr.demokritos.iit.jinsect.structs.GraphSimilarity;
import gr.demokritos.iit.jinsect.utils;
import java.io.IOException;
...

        String sTmp = "Hello graph!";
        DocumentNGramGraph dngGraph = new DocumentNGramGraph(); 
        dngGraph.setDataString(sTmp);
        String sTmp2 = "Hello other graph!";
        DocumentNGramGraph dngGraph2 = new DocumentNGramGraph(); 
        dngGraph2.setDataString(sTmp2);
         // Create a comparator object
         NGramCachedGraphComparator ngc = new NGramCachedGraphComparator();
         // Extract similarity
         GraphSimilarity gs = ngc.getSimilarityBetween(dngGraph, dngGraph2);
         // Output similarity (all three components: containment, value and size)
         System.out.println(gs.toString());

Merge two graphs
Will be added soon (I hope).

Save and load a graph
Same as above.