본문 바로가기

과제모음

[CMU Sphinx]DOM parser 기반 XML Parsing

반응형
import java.io.*;
import java.util.Vector;

import javax.xml.parsers.*;
import org.w3c.dom.*;

public class DOM {
public static Vector<Object> Vec = new Vector<Object>(0,1);
/**
 * @param args
 */
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder bul = fac.newDocumentBuilder();
Document TV = bul.parse("TV.xml");
Element root = TV.getDocumentElement();
NodeList NL = root.getElementsByTagName("target");
String first = root.getAttribute("name"); 
String first2 = root.getAttribute("alias");
for(int i=0; i < NL.getLength(); i++){
Element el = (Element)NL.item(i);
String second = el.getAttribute("name");
NodeList CNL = el.getElementsByTagName("command");
for(int j=0; j < CNL.getLength(); j++ ){
Element el2 = (Element)CNL.item(j);
String third = el2.getAttribute("name");
String result = first + " " + second + " " + third;
String result2 = first2 + " " + second + " " + third;
NodeList El = el2.getElementsByTagName("element");
if(third.compareTo("set")==0){
if(second.compareTo("channel")==0){
Vec.addElement(result);
Vec.addElement(result2);
}
else {
for(int k=0; k < El.getLength(); k++){
String ele = El.item(k).getTextContent();
Vec.addElement(result + " " + ele);
Vec.addElement(result2 + " " + ele);
}
}
}
else{
Vec.addElement(result);
Vec.addElement(result2);
}
}
}
DocumentBuilderFactory fac2 = DocumentBuilderFactory.newInstance();
DocumentBuilder DVDbul = fac2.newDocumentBuilder();
Document DVD = DVDbul.parse("dvd_player.xml");
Element Droot = DVD.getDocumentElement();
NodeList DNL = Droot.getElementsByTagName("target");
String Dfirst = Droot.getAttribute("name"); 
for(int i=0; i < DNL.getLength(); i++){
Element el = (Element)DNL.item(i);
String second = el.getAttribute("name");
NodeList CNL = el.getElementsByTagName("command");
for(int j=0; j < CNL.getLength(); j++ ){
Element el2 = (Element)CNL.item(j);
String third = el2.getAttribute("name");
String result = Dfirst + " " + second + " " + third;
NodeList El = el2.getElementsByTagName("element");
if(third.compareTo("set")==0){
for(int k=0; k < El.getLength(); k++){
String ele = El.item(k).getTextContent();
Vec.addElement(result + " " + ele);
}
}
else if(second.compareToIgnoreCase("dvd_player")==0){
Vec.addElement(Dfirst + " " + third);
}
else{
Vec.addElement(result);
}
}
}
DocumentBuilderFactory fac3 = DocumentBuilderFactory.newInstance();
DocumentBuilder Lbul = fac3.newDocumentBuilder();
Document Living = Lbul.parse("living_room.xml");
Element Lroot = Living.getDocumentElement();
NodeList LNL = Lroot.getElementsByTagName("target");
String Lfirst = Lroot.getAttribute("name"); 
for(int i=0; i < LNL.getLength(); i++){
Element el = (Element)LNL.item(i);
String second = el.getAttribute("name");
NodeList CNL = el.getElementsByTagName("command");
for(int j=0; j < CNL.getLength(); j++ ){
Element el2 = (Element)CNL.item(j);
String third = el2.getAttribute("name");
String result = Lfirst + " " + second + " " + third;
NodeList El = el2.getElementsByTagName("element");
if(third.compareTo("set")==0){
for(int k=0; k < El.getLength(); k++){
String ele = El.item(k).getTextContent();
Vec.addElement(result + " " + ele);
}
}
else{
Vec.addElement(result);
}
}
}
print(Vec);
}
public static void print(Vector<Object> v){
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 <int10> = ten ; \n public " +
"<int1to9> = one | two | three| four| five| six| seven| eight| nine  ; \n public " +
"<int11to19> = eleven| twelve| thirteen| fourteen| fifteen| sixteen| seventeen| eighteen | nineteen; \n " +
"public <tens> = twenty | thirty| forty| fifty| sixty| seventy| eighty| ninety; \n " +
"<int20to99> = ( <tens>   [<int1to9> ]); \n " +
"public <int10to99> = ( <int10> | <int11to19> | <int20to99> ) ; " +
"\n public <int1to99> = ( <int1to9> | <int10to99> ) ;\n\n\n" + "public <TVACTION> = ");
for(int i=0; i<n; i++){ //write to file what a XML parsing data
Object o = v.elementAt(i);
if(o.equals("television channel set")){
gram.writeBytes(o.toString());
gram.writeBytes(" <int1to99> | \n ");
}
else if(o.equals("tv channel set")){
gram.writeBytes(o.toString());
gram.writeBytes(" <int1to99> | \n ");
}
else {
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){}
}
}


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

SAX 파서 기반은 element 단위로 하향식 파싱을 해서 원하는 대로 안나왔지만 책을보고
DOM 파서 기반으로 바꾸었더니 트리구조를 생성하는 특성상 원하는 대로 문장으로 gram 파일이 저장이 됨...
내일은 소켓을 통한 데이터의 전송만 하면....진짜 휴가갈 수 있겠지? ㅠㅠ
반응형