org.maltparser.core.syntaxgraph.writer.TigerXMLWriter Maven / Gradle / Ivy
Show all versions of maltparser Show documentation
package org.maltparser.core.syntaxgraph.writer;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.regex.PatternSyntaxException;
import org.maltparser.core.exception.MaltChainedException;
import org.maltparser.core.helper.Util;
import org.maltparser.core.io.dataformat.ColumnDescription;
import org.maltparser.core.io.dataformat.DataFormatException;
import org.maltparser.core.io.dataformat.DataFormatInstance;
import org.maltparser.core.symbol.SymbolTableHandler;
import org.maltparser.core.syntaxgraph.PhraseStructure;
import org.maltparser.core.syntaxgraph.TokenStructure;
import org.maltparser.core.syntaxgraph.node.NonTerminalNode;
import org.maltparser.core.syntaxgraph.node.PhraseStructureNode;
import org.maltparser.core.syntaxgraph.node.TokenNode;
import org.maltparser.core.syntaxgraph.reader.TigerXMLHeader;
/**
*
*
* @author Johan Hall
*/
public class TigerXMLWriter implements SyntaxGraphWriter {
private enum RootHandling {
TALBANKEN, NORMAL
};
private BufferedWriter writer;
private DataFormatInstance dataFormatInstance;
private String optionString;
private int sentenceCount;
private TigerXMLHeader header;
// private boolean hasWriteTigerXMLHeader = false;
private RootHandling rootHandling;
private String sentencePrefix = "s";
private StringBuilder sentenceID;
private StringBuilder tmpID;
private StringBuilder rootID;
private int START_ID_OF_NONTERMINALS = 500;
private boolean labeledTerminalID;
private String VROOT_SYMBOL = "VROOT";
private boolean useVROOT = false;
// private String fileName = null;
// private String charsetName = null;
private boolean closeStream = true;
public TigerXMLWriter() {
sentenceID = new StringBuilder();
tmpID = new StringBuilder();
rootID = new StringBuilder();
labeledTerminalID = false;
}
public void open(String fileName, String charsetName) throws MaltChainedException {
try {
// this.fileName = fileName;
// this.charsetName = charsetName;
open(new OutputStreamWriter(new FileOutputStream(fileName),charsetName));
} catch (FileNotFoundException e) {
throw new DataFormatException("The output file '"+fileName+"' cannot be found.", e);
} catch (UnsupportedEncodingException e) {
throw new DataFormatException("The character encoding set '"+charsetName+"' isn't supported.", e);
}
}
public void open(OutputStream os, String charsetName) throws MaltChainedException {
try {
if (os == System.out || os == System.err) {
closeStream = false;
}
open(new OutputStreamWriter(os, charsetName));
} catch (UnsupportedEncodingException e) {
throw new DataFormatException("The character encoding set '"+charsetName+"' isn't supported.", e);
}
}
private void open(OutputStreamWriter osw) throws MaltChainedException {
setWriter(new BufferedWriter(osw));
setSentenceCount(0);
}
public void writeProlog() throws MaltChainedException {
// if (fileName == null || charsetName == null) {
writeHeader();
// }
}
public void writeSentence(TokenStructure syntaxGraph) throws MaltChainedException {
if (syntaxGraph == null || dataFormatInstance == null) {
return;
}
if (syntaxGraph.hasTokens()) {
sentenceCount++;
final PhraseStructure phraseStructure = (PhraseStructure)syntaxGraph;
try {
sentenceID.setLength(0);
sentenceID.append(sentencePrefix);
if (phraseStructure.getSentenceID() != 0) {
sentenceID.append(Integer.toString(phraseStructure.getSentenceID()));
} else {
sentenceID.append(Integer.toString(sentenceCount));
}
writer.write(" \n");
setRootID(phraseStructure);
writer.write(" \n");
writeTerminals(phraseStructure);
if (phraseStructure.nTokenNode() != 1 || rootHandling.equals(RootHandling.TALBANKEN)) {
writeNonTerminals(phraseStructure);
} else {
writer.write(" \n");
}
writer.write(" \n");
writer.write(" \n");
} catch (IOException e) {
throw new DataFormatException("The TigerXML writer could not write to file. ", e);
}
}
}
private void setRootID(PhraseStructure phraseStructure) throws MaltChainedException {
useVROOT = false;
PhraseStructureNode root = phraseStructure.getPhraseStructureRoot();
final SymbolTableHandler symbolTables = phraseStructure.getSymbolTables();
for (ColumnDescription column : dataFormatInstance.getPhraseStructureNodeLabelColumnDescriptionSet()) {
if (root.hasLabel(symbolTables.getSymbolTable(column.getName())) && root.getLabelSymbol(symbolTables.getSymbolTable(column.getName())).equals(VROOT_SYMBOL)) {
useVROOT = true;
break;
}
}
if (useVROOT) {
rootID.setLength(0);
rootID.append(sentenceID);
rootID.append('_');
rootID.append(VROOT_SYMBOL);
} else if (phraseStructure.nTokenNode() == 1 && phraseStructure.nNonTerminals() == 0 && !root.isLabeled()) {
rootID.setLength(0);
rootID.append(sentenceID);
rootID.append("_1");
} else {
rootID.setLength(0);
rootID.append(sentenceID);
rootID.append('_');
// if (rootHandling.equals(RootHandling.NORMAL)) {
rootID.append(Integer.toString(START_ID_OF_NONTERMINALS+phraseStructure.nNonTerminals()));
// } else if (rootHandling.equals(RootHandling.TALBANKEN)) {
// rootID.append(Integer.toString(START_ID_OF_NONTERMINALS+1));
// }
}
}
public void writeEpilog() throws MaltChainedException {
writeTail();
}
public BufferedWriter getWriter() {
return writer;
}
public void setWriter(BufferedWriter writer) {
this.writer = writer;
}
public void close() throws MaltChainedException {
try {
if (writer != null) {
writer.flush();
if (closeStream) {
writer.close();
}
writer = null;
}
} catch (IOException e) {
throw new DataFormatException("Could not close the output file. ", e);
}
}
private void writeHeader() throws MaltChainedException {
try {
if (header == null) {
header = new TigerXMLHeader();
}
writer.write(header.toTigerXML());
// hasWriteTigerXMLHeader = true;
} catch (IOException e) {
throw new DataFormatException("The TigerXML writer could not write to file. ", e);
}
}
private void writeTerminals(PhraseStructure phraseStructure) throws MaltChainedException {
try {
writer.write(" \n");
for (int index : phraseStructure.getTokenIndices()) {
final PhraseStructureNode t = phraseStructure.getTokenNode(index);
writer.write(" \n");
}
writer.write(" \n");
} catch (IOException e) {
throw new DataFormatException("The TigerXML writer is not able to write. ", e);
}
}
public void writeNonTerminals(PhraseStructure phraseStructure) throws MaltChainedException {
try {
SortedMap heights = new TreeMap();
for (int index : phraseStructure.getNonTerminalIndices()) {
heights.put(index, ((NonTerminalNode)phraseStructure.getNonTerminalNode(index)).getHeight());
}
writer.write(" \n");
boolean done = false;
int h = 1;
while (!done) {
done = true;
for (int index : phraseStructure.getNonTerminalIndices()) {
if (heights.get(index) == h) {
NonTerminalNode nt = (NonTerminalNode)phraseStructure.getNonTerminalNode(index);
tmpID.setLength(0);
tmpID.append(sentenceID);
tmpID.append('_');
tmpID.append(Integer.toString(nt.getIndex()+START_ID_OF_NONTERMINALS-1));
writeNonTerminal(phraseStructure.getSymbolTables(), nt, tmpID.toString());
done = false;
}
}
h++;
}
writeNonTerminal(phraseStructure.getSymbolTables(), (NonTerminalNode)phraseStructure.getPhraseStructureRoot(),rootID.toString());
writer.write(" \n");
} catch (IOException e) {
throw new DataFormatException("The TigerXML writer is not able to write. ", e);
}
}
public void writeNonTerminal(SymbolTableHandler symbolTables, NonTerminalNode nt, String id) throws MaltChainedException {
try {
writer.write(" \n");
for (int i = 0, n = nt.nChildren(); i < n; i++) {
PhraseStructureNode child = nt.getChild(i);
writer.write(" \n");
}
writer.write(" \n");
} catch (IOException e) {
throw new DataFormatException("The TigerXML writer is not able to write. ", e);
}
}
private void writeTail() throws MaltChainedException {
try {
writer.write("