001package org.maltparser.core.syntaxgraph; 002 003import java.util.SortedMap; 004 005import org.maltparser.core.exception.MaltChainedException; 006import org.maltparser.core.symbol.SymbolTable; 007 008/** 009* 010* 011* @author Johan Hall 012*/ 013public class RootLabels { 014 public final static String DEFAULT_ROOTSYMBOL = "ROOT"; 015 private final LabelSet rootLabelCodes; 016 017 public RootLabels() { 018 rootLabelCodes = new LabelSet(); 019 } 020 021 public void setRootLabels(String rootLabelOption, SortedMap<String, SymbolTable> edgeSymbolTables) throws MaltChainedException { 022 if (edgeSymbolTables == null) { 023 return; 024 } else if (rootLabelOption == null || rootLabelOption.trim().length() == 0) { 025 for (SymbolTable table : edgeSymbolTables.values()) { 026 rootLabelCodes.put(table, table.addSymbol(RootLabels.DEFAULT_ROOTSYMBOL)); 027 } 028 } else if (rootLabelOption.trim().indexOf(',') == -1) { 029 int index = rootLabelOption.trim().indexOf('='); 030 if (index == -1) { 031 for (SymbolTable table : edgeSymbolTables.values()) { 032 rootLabelCodes.put(table, table.addSymbol(rootLabelOption.trim())); 033 } 034 } else { 035 String name = rootLabelOption.trim().substring(0, index); 036 if (edgeSymbolTables.get(name) == null) { 037 throw new SyntaxGraphException("The symbol table '"+ name+"' cannot be found when defining the root symbol. "); 038 } else { 039 rootLabelCodes.put(edgeSymbolTables.get(name), edgeSymbolTables.get(name).addSymbol(rootLabelOption.trim().substring(index+1))); 040 if (edgeSymbolTables.size() > 1) { 041 for (SymbolTable table : edgeSymbolTables.values()) { 042 if (!table.getName().equals(name)) { 043 rootLabelCodes.put(table, table.addSymbol(RootLabels.DEFAULT_ROOTSYMBOL)); 044 } 045 } 046 } 047 } 048 } 049 } else { 050 String[] items = rootLabelOption.trim().split(","); 051 for (int i=0; i<items.length; i++) { 052 int index = items[i].trim().indexOf('='); 053 if (index == -1) { 054 throw new SyntaxGraphException("The root symbol is undefinied. "); 055 } else { 056 String name = items[i].trim().substring(0, index); 057 if (edgeSymbolTables.get(name) == null) { 058 throw new SyntaxGraphException("The symbol table'"+ name+"' cannot be found when defining the root symbol. "); 059 } else { 060 rootLabelCodes.put(edgeSymbolTables.get(name), edgeSymbolTables.get(name).addSymbol(items[i].trim().substring(index+1))); 061 } 062 } 063 } 064 for (SymbolTable table : edgeSymbolTables.values()) { 065 if (!rootLabelCodes.containsKey(table)) { 066 rootLabelCodes.put(table, table.addSymbol(RootLabels.DEFAULT_ROOTSYMBOL)); 067 } 068 } 069 } 070 } 071 072 public void setDefaultRootLabel(SymbolTable table, String defaultRootSymbol) throws MaltChainedException { 073 rootLabelCodes.put(table, table.addSymbol(defaultRootSymbol)); 074 } 075 076 public Integer getDefaultRootLabelCode(SymbolTable table) throws MaltChainedException { 077 Integer res = rootLabelCodes.get(table); 078 if (res == null) { 079 return table.addSymbol(RootLabels.DEFAULT_ROOTSYMBOL); 080 } 081 return res; 082 } 083 084 public LabelSet getDefaultRootLabels() throws MaltChainedException { 085 return new LabelSet(rootLabelCodes); 086 } 087 088 public String getDefaultRootLabelSymbol(SymbolTable table) throws MaltChainedException { 089 return table.getSymbolCodeToString(getDefaultRootLabelCode(table)); 090 } 091 092 093 public boolean checkRootLabelCodes(LabelSet rlc) { 094 if (rlc == null && rootLabelCodes == null) { 095 return true; // or false ? 096 } else if ((rlc == null && rootLabelCodes != null) || (rlc != null && rootLabelCodes == null)) { 097 return false; 098 } else if (rlc.size() != rootLabelCodes.size()) { 099 return false; 100 } else { 101 for (SymbolTable table : rootLabelCodes.keySet()) { 102 if (!rootLabelCodes.get(table).equals(rlc.get(table))) { 103 return false; 104 } 105 } 106 return true; 107 } 108 } 109}