@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
try {
Recognizer recognizer;
//Clone necessario, istanzia un nuovo Configuration Manager per ogni process()
//in caso di più sphinxAE in esecuzione, le risorse vengono duplicate da quello
//creato nel metodo inizilize() per non reimpostare tutti i parametri
ConfigurationManager cm = this.cm.clone();
recognizer = (Recognizer) cm.lookup("recognizer");
List<File> batch = new ArrayList<File>();
ArrayList<Integer> beginAnnot = new ArrayList<Integer>();
ArrayList<Integer> endAnnot = new ArrayList<Integer>();
ArrayList<Long> beginTimeAnnot = new ArrayList<Long>();
ArrayList<Long> endTimeAnnot = new ArrayList<Long>();
StringBuffer document = new StringBuffer();
Iterator audioIt = aJCas.getAnnotationIndex(Audio.type).iterator();
Audio audio = (Audio) audioIt.next();
Result result;
// getContext().getLogger().log("CF File is " + ConfigFile);
// if(audioIt.hasNext())
ConcatAudioFileDataSource data = (ConcatAudioFileDataSource) cm.lookup("dataSource");
for(String i: audio.getAudioPath().toArray())
batch.add(new File(i));
data.setBatchFiles(batch);
recognizer.allocate();
getContext().getLogger().log(Level.INFO, "Start recognition of " + audio.getAudioPath().toString(0));
Long comp1 = System.currentTimeMillis();
while((result = recognizer.recognize()) != null){
ArrayList<TextAndTime> stt = TextAndTime.convert(result);
if(stt != null)
for(TextAndTime t: stt){
String word = t.getWord();
if(word.matches(TextAndTime.PATTERN_SIL))
document.append(word);
else{
beginAnnot.add(document.length());
document.append(word);
endAnnot.add(document.length());
beginTimeAnnot.add(t.getIniTime());
endTimeAnnot.add(t.getEndTime());
}
document.append(" ");
}
}
Long comp2 = System.currentTimeMillis();
getContext().getLogger().log(Level.INFO, "End recognition of " + audio.getAudioPath().toString(0));
getContext().getLogger().log(Level.INFO, TextAndTime.getFormattedLong(comp2 - comp1)
+ " is total recognition time of this audio files " + audio.getAudioPath().toString(0));
getContext().getLogger().log(Level.INFO, "Text " + document.toString());
aJCas.setDocumentText(document.toString());
Iterator<Long> itb = beginTimeAnnot.iterator(),
ite = endTimeAnnot.iterator();
Iterator<Integer> itbb = beginAnnot.iterator(),
itee = endAnnot.iterator();
while(itb.hasNext()){
STText stt = new STText(aJCas);
stt.setBegin(itbb.next());
stt.setEnd(itee.next());
stt.setStartTime(itb.next());
stt.setEndTime(ite.next());
stt.addToIndexes();
}
recognizer.deallocate();
} catch (CloneNotSupportedException e) {
getContext().getLogger().log(Level.WARNING, "Configuration Manager CloneError: " + e.getMessage());
}
}