2011-02-10 6 views
2

Java를 사용하여 주어진 텍스트 문서에서 구조와 유사한 구조를 가져와야합니다. 사용 된 파일 유형은 공통이어야하며 열려 있어야합니다 (rtf, odt, ...). 현재 Apache Tika를 사용하여 여러 문서의 일반 텍스트를 구문 분석합니다.Java로 문서 구조 구문 분석

올바른 구조를 가장 신뢰할 수있게 파싱 할 수 있도록 어떤 파일 형식과 API를 사용해야합니까? 티카와 함께 할 수 있다면 데모를 보게되어 기쁩니다.

예를 들어, 우리는 주어진 문서에서 이러한 종류의 데이터를 얻어야한다 :

Main Heading 
    Heading 1 
    Heading 1.1 
    Heading 2 
    Heading 2.2 

홈페이지 제목은 논문의 제목입니다. 용지에는 제목 1과 제목 2의 두 가지 메인 제목이 있으며 둘 다 하나의 소제목이 있습니다. 각 제목 아래에 내용을 가져와야합니다 (단락 본문).

도움을 주시면 감사하겠습니다.

답변

3

OpenDocument (.odt)는 실제로 여러 xml 파일을 포함하는 zip 패키지입니다. Content.xml에는 문서의 실제 텍스트 내용이 들어 있습니다. 우리는 표제에 관심이 있고 그 표제는 h 태그 안에 있습니다. ODT에 대해 더 자세히 읽어보십시오.

QueryPath .odt 파일에서 제목을 추출하기위한 구현을 발견했습니다.

원래 질문은 Java에 관한 것이므로 여기에 있습니다. 먼저 ZipFile을 사용하여 content.xml에 액세스해야합니다. 그런 다음 SAX를 사용하여 content.xml에서 XML 컨텐트를 파싱합니다.

Test3.odt 
content.xml 
3764 
1 My New Great Paper 
2 Abstract 
2 Introduction 
2 Content 
3 More content 
3 Even more 
2 Conclusions 

샘플 코드 : 사용의 ContentHandler의

 public void printHeadingsOfOdtFIle(File odtFile) { try { ZipFile zFile = new ZipFile(odtFile); System.out.println(zFile.getName()); ZipEntry contentFile = zFile.getEntry("content.xml"); System.out.println(contentFile.getName()); System.out.println(contentFile.getSize()); XMLReader xr = XMLReaderFactory.createXMLReader(); OdtDocumentContentHandler handler = new OdtDocumentContentHandler(); xr.setContentHandler(handler); xr.parse(new InputSource(zFile.getInputStream(contentFile))); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new OdtDocumentStructureExtractor().printHeadingsOfOdtFIle(new File("Test3.odt")); } 

관련 부품과 같이 :

 @Override 
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 

    temp = ""; 

    if("text:h".equals(qName)) { 

     String headingLevel = atts.getValue("text:outline-level"); 

     if(headingLevel != null) { 

      System.out.print(headingLevel + " "); 

     } 

    } 

} 

@Override 
public void characters(char[] ch, int start, int length) throws SAXException { 

    char[] subArray = new char[length]; 
    System.arraycopy(ch, start, subArray, 0, length); 
    temp = new String(subArray); 

    fullText.append(temp); 
} 

@Override 
public void endElement(String uri, String localName, String qName) throws SAXException { 

    if("text:h".equals(qName)) { 

     System.out.println(temp); 

    } 

} 
샘플 코드는 단순히 모든 제목을 출력