001package org.maltparser.core.syntaxgraph;
002
003import java.io.BufferedWriter;
004import java.io.FileNotFoundException;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.io.OutputStreamWriter;
008import java.io.UnsupportedEncodingException;
009
010import org.maltparser.core.config.ConfigurationDir;
011import org.maltparser.core.exception.MaltChainedException;
012import org.maltparser.core.flow.FlowChartInstance;
013import org.maltparser.core.flow.item.ChartItem;
014import org.maltparser.core.flow.spec.ChartItemSpecification;
015import org.maltparser.core.io.dataformat.DataFormatException;
016import org.maltparser.core.options.OptionManager;
017import org.maltparser.core.syntaxgraph.node.DependencyNode;
018
019public class GraphAnalyzerChartItem extends ChartItem {
020        private String idName;
021        private String sourceName;
022        private String task;
023        private ConfigurationDir configDir;
024        private DependencyStructure cachedSource = null;
025        private BufferedWriter writer;
026        private boolean closeStream = true;
027        private int graphCounter = 1;
028        
029        public void initialize(FlowChartInstance flowChartinstance, ChartItemSpecification chartItemSpecification) throws MaltChainedException {
030                super.initialize(flowChartinstance, chartItemSpecification);
031                for (String key : chartItemSpecification.getChartItemAttributes().keySet()) {
032                        if (key.equals("id")) {
033                                idName = chartItemSpecification.getChartItemAttributes().get(key);
034                        } else if (key.equals("source")) {
035                                sourceName = chartItemSpecification.getChartItemAttributes().get(key);
036                        }
037                }
038                if (idName == null) {
039                        idName = getChartElement("analyzer").getAttributes().get("id").getDefaultValue();
040                } else if (sourceName == null) {
041                        sourceName = getChartElement("analyzer").getAttributes().get("source").getDefaultValue();
042                }
043                task = OptionManager.instance().getOptionValue(getOptionContainerIndex(), "analyzer", "task").toString();
044                configDir = (ConfigurationDir)flowChartinstance.getFlowChartRegistry(org.maltparser.core.config.ConfigurationDir.class, idName);
045                open(task+".dat",OptionManager.instance().getOptionValue(getOptionContainerIndex(), "input", "charset").toString());
046        }
047
048        public int preprocess(int signal) throws MaltChainedException {
049                return signal;
050        }
051        
052        public int process(int signal) throws MaltChainedException {
053                if (task.equals("projectivity")) {
054                        if (cachedSource == null) {
055                                cachedSource = (DependencyStructure)flowChartinstance.getFlowChartRegistry(org.maltparser.core.syntaxgraph.DependencyStructure.class, sourceName);
056                        }
057                        try {
058                                writer.append("graph # ");
059                                writer.append(Integer.toString(graphCounter));
060                                writer.append('\n');
061                                for (int index : cachedSource.getTokenIndices()) {
062                                        DependencyNode node = cachedSource.getDependencyNode(index);
063                                        
064                                        writer.append(Integer.toString(node.getIndex()));
065                                        writer.append('\t');
066                                        writer.append(Integer.toString(node.getHead().getIndex()));
067                                        writer.append('\t');
068                                        writer.append('#');
069                                        writer.append('\t');
070                                        if (node.isProjective()) {
071                                                writer.append("@P");
072                                        } else {
073                                                writer.append("@N");
074                                        }
075                                        writer.append('\n');
076                                }
077                                writer.append('\n');
078                        } catch (IOException e) {
079                                throw new MaltChainedException("", e);
080                        }
081                        graphCounter++;
082                }
083                return signal;
084        }
085        
086        public int postprocess(int signal) throws MaltChainedException {
087                return signal;  
088        }
089
090        
091        public void terminate() throws MaltChainedException {
092                cachedSource = null;
093                close();
094        }
095        
096        private void open(String fileName, String charsetName) throws MaltChainedException {
097                try {
098                        open(new OutputStreamWriter(new FileOutputStream(fileName),charsetName));
099                } catch (FileNotFoundException e) {
100                        throw new DataFormatException("The output file '"+fileName+"' cannot be found.", e);
101                } catch (UnsupportedEncodingException e) {
102                        throw new DataFormatException("The character encoding set '"+charsetName+"' isn't supported.", e);
103                }       
104        }
105        
106        private void open(OutputStreamWriter osw) throws MaltChainedException {
107                setWriter(new BufferedWriter(osw));
108        }
109
110        private void setWriter(BufferedWriter writer) throws MaltChainedException  {
111                close();
112                this.writer = writer;
113        }
114        
115        private void close() throws MaltChainedException {
116                try {
117                        if (writer != null) {
118                                writer.flush();
119                                if (closeStream) {
120                                        writer.close();
121                                }
122                                writer = null;
123                        }
124                }   catch (IOException e) {
125                        throw new DataFormatException("Could not close the output file. ", e);
126                } 
127
128        }
129}