Programming Project #1 - Step 3 (Parsing Data)



The data you collected in the class is stored in an XML format, but you need to convert that data into their compatible data structures in order to segment them using the PaleoSketch recognizer.  Sample code to convert the XML into PaleoSketch library-friendly data structures can be found below.  If you would like to try running this code sample, be sure to change the file path to an existing local XML file.
Below is be a more detailed explanation on parsing the data, based on the the above sample code.

Importing files
import edu.tamu.core.sketch.*;
import edu.tamu.recognition.paleo.*;
import ecologylab.serialization.SIMPLTranslationException;
The first import statement is to use the relevant data structures to store the user study data.  The second import statement is to use the PaleoSketch recognizer.  The third import statement is used for the exception made when deserializing the XML file.

Parsing data
File file = new File("some local XML file");
Sketch sketch = null;

try {
      sketch = Sketch.deserialize(file);
}
catch (SIMPLTranslationException e) {
      e.printStackTrace();
}
This code fragment first gets the target XML data file to deserialize, and the result is that the data is stored in a a data structure called a Sketch object.

Extracting strokes
List strokes = sketch.getRecursiveStrokes();
The Sketch object contains the entire sketch collected in the truss sketch, but the strokes need to be extracted from this Sketch object to be used for the PaleoSketch recognizer.  This is done by calling a method to recursively extract those strokes from the sketch.

Initializing PaleoSketch recognizer
PaleoSketchRecognizer paleo = new PaleoSketchRecognizer(PaleoConfig.allOn());
This line of code instantiates the PaleoSketch recognizer.  For this particular instantiation, the recognizer enables that all possible primitive shapes be recognized.  Since this call is expensive, this instantiation should be done only once.

Extracting primitive shapes
Shape bestShape;
String shapeName;
for (Stroke stroke : strokes) {

      paleo.setStroke(stroke);
      bestShape = paleo.recognize().getBestShape();
      shapeName = bestShape.getInterpretation().label;
      System.out.println(shapeName);
}
With the strokes extracted earlier, those strokes need to be given to the PaleoSketch recognizer.  In this code fragment, all the strokes are iterated.  First, the stroke is given to the recognizer.  Second, the recognizer returns the best shape interpretation of the stroke.  Third, the label (i.e., type) of the shape interpretation is given.  Lastly, the shape name is output to the console.