본문 바로가기

과제모음

[CMU Sphinx]SAX Parse를 이용한 gram 파일 자동작성

반응형
## SettingContentHandler.java##

import javax.xml.parsers.*;
import org.xml.sax.*;

public class SettingContentHandler {
public static void main(String args[]) throws Exception{
SAXParserFactory factory = SAXParserFactory.newInstance(); //initialize SAXparse
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
ContentHandler contentHandler = new MyContentHandler();
reader.setContentHandler(contentHandler);
reader.parse("television.xml"); // read XML file & order parse
}
}

## MyContentHandler.java##

import org.xml.sax.*;
import java.util.*;
import java.io.*;

public class MyContentHandler implements ContentHandler {
Vector<Object> vector = new Vector<Object>(0,1);
static int i = 0;
public void setDocumentLocator(Locator locator){}
public void startDocument() throws SAXException {} // start point of XML document
public void endDocument() throws SAXException {
print(vector); 
System.out.println("parsing XML complete...");} //result output
public void startPrefixMapping(String prefix, String uri) throws SAXException {}
public void startPrefixMapping(String prefix) throws SAXException {}
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException{
// start point of element
if(qName.equals("system")){ // add element data to vector array
vector.addElement(atts.getValue("name"));
vector.addElement(atts.getValue("alias"));
} else if(qName.equals("target")){
vector.addElement(atts.getValue("name"));
} else if(qName.equals("command")){
vector.addElement(atts.getValue("name"));
}
}
public void endElement(String uri, String localName, String qName) throws SAXException{}//end point of element

public void characters(char[] ch, int start, int length) throws SAXException { //add charactor data to vector array
String content = new String(ch, start, length);
if(content.charAt(0) != '1'){
if(content.charAt(0)!='\n'){
if(content.charAt(0)!= ' '){
vector.addElement(content);
}
}
}
else if(content.charAt(0)=='1'){ //if element value ==1 loop to until value == 98
for(int j=1; j<99; j++)
{
vector.addElement(j);
}
}
}
public static void print(Vector<Object> v){ //print vector array & write file
int n =v.size();
File file = new File("c:\\TV.gram"); //create new object
if(file.exists())file.delete(); // always write the new file
if(!file.exists())
try {
try {
Thread.sleep(2000);
} catch (InterruptedException e){} 
DataOutputStream gram = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(
new File("c:\\TV.gram")))); //make new gram file
gram.writeBytes("grammar TV;" + "\n" +
"import <commands.standard_commands>;\n\n\n" + "public <TVACTION> = ");
for(; i<n; i++){ //write to file what a XML parsing data
Object o = v.elementAt(i);
gram.writeBytes(o.toString());
if(i==(n-1))gram.writeBytes(" ;\n\n\n");
else gram.writeBytes(" |\n ");
}
gram.writeBytes("\n public <commands> = <standatd_commands>;\n");
gram.close();
} catch (IOException e){}
}


public void ignorableWithespace(char[] ch, int start, int length) throws SAXException {}
public void processingInstruction(String target, String data) throws SAXException {}
public void skippedEntity(String name) throws SAXException {}
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
}

======================================================================================================================

단어별 파싱은 성공적으로 진행되었지만 원하는 만큼의 문장별 파싱은 힘들듯 하다
 => XML의 element 단위로 파싱을 하기 때문에 한번 파싱한 element를 다시 불러오기가 번거로움

JAVA 공부하자....
하나도 모르고 책만보고 한것치곤 그래도 그럳저럭 한것같다 후후후
이클립스 디버거가 무수히 돌아간 결과지만 ㅋㅋㅋㅋㅋ
출력부분 손보느라 많은 힘이 들었네... element 단위로 출력을 시키는 것이 아니라
document 단위로 끊어서 하는 것으로 해결 
이정도만 하고 데모받고 모레부터 휴가 고고고

반응형