use of com.hp.gagawa.java.Node in project ANNIS by korpling.
the class CorefVisualizer method writeOutput.
/**
* writes Output for the CorefVisualizer
* @param writer writer to write with
*/
@Override
public void writeOutput(VisualizerInput input, Writer w) {
// root html element
Html html = new Html();
Head head = new Head();
Body body = new Body();
html.removeXmlns();
html.appendChild(head);
html.appendChild(body);
try {
LinkedList<String> fonts = new LinkedList<String>();
if (input.getFont() != null) {
Link linkFont = new Link();
linkFont.setHref(input.getFont().getUrl());
head.appendChild(linkFont);
fonts.add(input.getFont().getName());
}
fonts.add("serif");
Link linkJQueryUI = new Link();
linkJQueryUI.setHref(input.getResourcePath("coref/jquery-ui-1.11.4.custom/jquery-ui.min.css"));
linkJQueryUI.setRel("stylesheet");
linkJQueryUI.setType("text/css");
head.appendChild(linkJQueryUI);
Link linkJQueryUIStructure = new Link();
linkJQueryUIStructure.setHref(input.getResourcePath("coref/jquery-ui-1.11.4.custom/jquery-ui.structure.min.css"));
linkJQueryUIStructure.setRel("stylesheet");
linkJQueryUIStructure.setType("text/css");
head.appendChild(linkJQueryUIStructure);
Script scriptJquery = new Script("text/javascript");
scriptJquery.setSrc(input.getResourcePath("coref/jquery.js"));
head.appendChild(scriptJquery);
Script scriptUI = new Script("text/javascript");
scriptUI.setSrc(input.getResourcePath("coref/jquery-ui-1.11.4.custom/jquery-ui.min.js"));
head.appendChild(scriptUI);
Link linkCoref = new Link();
linkCoref.setHref(input.getResourcePath("coref/coref.css"));
linkCoref.setRel("stylesheet");
linkCoref.setType("text/css");
head.appendChild(linkCoref);
Script scriptCoref = new Script("text/javascript");
scriptCoref.setSrc(input.getResourcePath("coref/CorefVisualizer.js"));
head.appendChild(scriptCoref);
body.setStyle("font-family: '" + StringUtils.join(fonts, "', '") + "';");
// get Info
globalIndex = 0;
tokensOfNode = new HashMap<String, List<String>>();
referentList = new LinkedList<TReferent>();
komponent = new LinkedList<TComponent>();
referentOfToken = new HashMap<String, HashMap<Long, Integer>>();
componentOfToken = new HashMap<String, List<Long>>();
componenttype = new LinkedList<TComponenttype>();
SDocument saltDoc = input.getDocument();
SDocumentGraph saltGraph = saltDoc.getDocumentGraph();
if (saltGraph == null) {
body.setText("An Error occured: Could not get Graph of Result (Graph == null).");
return;
}
List<SRelation<SNode, SNode>> edgeList = saltGraph.getRelations();
if (edgeList == null) {
return;
}
for (SRelation rawRel : edgeList) {
if (includeEdge(rawRel, input.getNamespace())) {
SPointingRelation rel = (SPointingRelation) rawRel;
String relType = componentNameForRelation(rel);
visitedNodes = new LinkedList<String>();
// got type for this?
boolean gotIt = false;
int componentnr;
for (componentnr = 0; componentnr < componenttype.size(); componentnr++) {
if (componenttype.get(componentnr) != null && componenttype.get(componentnr).type != null && componenttype.get(componentnr).nodeList != null && componenttype.get(componentnr).type.equals(relType) && componenttype.get(componentnr).nodeList.contains(rel.getSource().getId())) {
gotIt = true;
break;
}
}
TComponent currentComponent;
TComponenttype currentComponenttype;
if (gotIt) {
currentComponent = komponent.get(componentnr);
currentComponenttype = componenttype.get(componentnr);
} else {
currentComponenttype = new TComponenttype();
currentComponenttype.type = relType;
componenttype.add(currentComponenttype);
componentnr = komponent.size();
currentComponent = new TComponent();
currentComponent.type = relType;
currentComponent.tokenList = new LinkedList<String>();
komponent.add(currentComponent);
currentComponenttype.nodeList.add(rel.getSource().getId());
}
TReferent ref = new TReferent();
ref.annotations = new HashSet<SerializableAnnotation>();
for (SAnnotation anno : rel.getAnnotations()) {
ref.annotations.add(new SerializableAnnotation(anno));
}
ref.component = componentnr;
referentList.add(ref);
List<String> currentTokens = getAllTokens(rel.getSource(), componentNameForRelation(rel), currentComponenttype, componentnr, input.getNamespace());
// neu
setReferent(rel.getTarget(), globalIndex, 0);
// neu
setReferent(rel.getSource(), globalIndex, 1);
for (String s : currentTokens) {
if (!currentComponent.tokenList.contains(s)) {
currentComponent.tokenList.add(s);
}
}
globalIndex++;
}
}
colorlist = new HashMap<Integer, Integer>();
// A list containing all the generated HTML elements, one list entry
// for each text.
List<List<Node>> nodesPerText = new LinkedList<List<Node>>();
// write output for each text separatly
List<STextualDS> texts = saltGraph.getTextualDSs();
if (texts != null && !texts.isEmpty()) {
for (STextualDS t : texts) {
DataSourceSequence<Integer> sequence = new DataSourceSequence<>(t, 0, (t.getText() != null) ? t.getText().length() : 0);
List<SToken> token = saltGraph.getSortedTokenByText(saltGraph.getTokensBySequence(sequence));
if (token != null) {
boolean validText = true;
if (Boolean.parseBoolean(input.getMappings().getProperty("hide_empty", "false"))) {
validText = false;
// check if the text contains any matching annotations
for (SToken tok : token) {
/*
* The token is only added to this map if an valid edge
* (according to the resolver trigger) conntected to
* this token was found.
*/
if (referentOfToken.get(tok.getId()) != null && !referentOfToken.get(tok.getId()).isEmpty()) {
validText = true;
break;
}
}
}
if (validText) {
List<Node> nodes = outputSingleText(token, input);
nodesPerText.add(nodes);
}
}
}
// end for each STexutalDS
/*
* Append the generated output to the body, wrap in table if necessary.
*/
// present all texts as columns side by side if using multiple texts
Table tableTexts = new Table();
Tr trTextRow = new Tr();
trTextRow.setCSSClass("textRow");
// only append wrapper table if we have multiple texts
if (nodesPerText.size() > 1) {
body.appendChild(tableTexts);
tableTexts.appendChild(trTextRow);
}
for (List<Node> nodes : nodesPerText) {
// multi-text mode?
if (nodesPerText.size() > 1) {
Td tdSingleText = new Td();
trTextRow.appendChild(tdSingleText);
tdSingleText.setCSSClass("text");
tdSingleText.appendChild(nodes);
} else {
body.appendChild(nodes);
}
}
} else {
Text errorTxt = new Text("Could not find any texts for the " + input.getNamespace() + " node namespace (layer).");
body.appendChild(errorTxt);
}
// write HTML4 transitional doctype
w.append(new Doctype(DocumentType.HTMLTransitional).write());
// append the html tree
w.append(html.write());
} catch (IOException ex) {
log.error(null, ex);
}
}
use of com.hp.gagawa.java.Node in project ANNIS by korpling.
the class CorefVisualizer method outputSingleText.
private List<Node> outputSingleText(List<SToken> token, VisualizerInput input) throws IOException {
List<Node> result = new LinkedList<Node>();
List<Long> prevpositions, listpositions;
List<Long> finalpositions = null;
int maxlinkcount = 0;
String lastId, currentId = null;
for (SToken tok : token) {
prevpositions = finalpositions;
if (prevpositions != null && prevpositions.size() < 1) {
prevpositions = null;
}
lastId = currentId;
currentId = tok.getId();
listpositions = componentOfToken.get(currentId);
List<Boolean> checklist = null;
if (prevpositions == null && listpositions != null) {
finalpositions = listpositions;
} else if (listpositions == null) {
finalpositions = new LinkedList<Long>();
} else {
checklist = new LinkedList<Boolean>();
for (int i = 0; prevpositions != null && i < prevpositions.size(); i++) {
if (listpositions.contains(prevpositions.get(i))) {
checklist.add(true);
} else {
checklist.add(false);
}
}
List<Long> remains = new LinkedList<Long>();
for (int i = 0; i < listpositions.size(); i++) {
if (prevpositions != null && !prevpositions.contains(listpositions.get(i))) {
remains.add(listpositions.get(i));
}
}
int minsize = checklist.size() + remains.size();
int number = 0;
finalpositions = new LinkedList<Long>();
for (int i = 0; i < minsize; i++) {
if (prevpositions != null && checklist.size() > i && checklist.get(i).booleanValue()) {
finalpositions.add(prevpositions.get(i));
} else {
if (remains.size() > number) {
Long ll = remains.get(number);
finalpositions.add(ll);
number++;
minsize--;
} else {
finalpositions.add(Long.MIN_VALUE);
}
}
}
}
String onclick = "";
String style = "";
if (input.getMarkedAndCovered().containsKey(tok)) {
MatchedNodeColors[] vals = MatchedNodeColors.values();
long match = Math.min(input.getMarkedAndCovered().get(tok) - 1, vals.length - 1);
style += ("color: " + vals[(int) match].getHTMLColor() + ";");
}
boolean underline = false;
if (!finalpositions.isEmpty()) {
style += "cursor:pointer;";
underline = true;
onclick = "togglePRAuto(this);";
}
Table tableSingleTok = new Table();
result.add(tableSingleTok);
tableSingleTok.setCSSClass("token");
int currentlinkcount = 0;
if (underline) {
boolean firstone = true;
int index = -1;
String tooltip;
if (!finalpositions.isEmpty()) {
for (Long currentPositionComponent : finalpositions) {
index++;
String left = "", right = "";
// == pir
TComponent currentWriteComponent = null;
String currentType = "";
if (!currentPositionComponent.equals(Long.MIN_VALUE) && komponent.size() > currentPositionComponent) {
currentWriteComponent = komponent.get((int) (long) currentPositionComponent);
List<String> pi = currentWriteComponent.tokenList;
List<String> preparedPi = new LinkedList<String>();
for (String s : pi) {
preparedPi.add(prepareID(s));
}
currentType = currentWriteComponent.type;
left = StringUtils.join(preparedPi, ",");
right = "" + currentPositionComponent + 1;
}
String annotations = getAnnotations(tok.getId(), currentPositionComponent);
if (firstone) {
firstone = false;
if (currentWriteComponent == null) {
String left2 = "", right2 = "";
long pr = 0;
// == pir
TComponent currentWriteComponent2;
String currentType2 = "";
String annotations2 = "";
for (Long currentPositionComponent2 : finalpositions) {
if (!currentPositionComponent2.equals(Long.MIN_VALUE) && komponent.size() > currentPositionComponent2) {
currentWriteComponent2 = komponent.get((int) (long) currentPositionComponent2);
List<String> pi2 = currentWriteComponent2.tokenList;
// prepare each single ID
List<String> preparedPi2 = new LinkedList<String>();
for (String s : pi2) {
preparedPi2.add(prepareID(s));
}
currentType2 = currentWriteComponent2.type;
left2 = StringUtils.join(preparedPi2, ",");
right2 = "" + currentPositionComponent2 + 1;
annotations2 = getAnnotations(tok.getId(), currentPositionComponent2);
pr = currentPositionComponent2;
break;
}
}
tooltip = "<b>Component</b>: " + (pr + 1) + ", <b>Type</b>: " + currentType2 + annotations2;
Tr trTok = new Tr();
tableSingleTok.appendChild(trTok);
Td tdTok = new Td();
trTok.appendChild(tdTok);
tdTok.setId("tok_" + prepareID(tok.getId()));
tdTok.setTitle(tooltip);
tdTok.setStyle(style);
tdTok.setAttribute("onclick", onclick);
tdTok.setAttribute("annis:pr_left", left2);
tdTok.setAttribute("annis:pr_right", right2);
Text textTok = new Text(" " + CommonHelper.getSpannedText(tok) + " ");
tdTok.appendChild(textTok);
} else {
// easier
tooltip = "<b>Component</b>: " + (currentPositionComponent + 1) + ", <b>Type</b> " + currentType + annotations;
Tr trTok = new Tr();
tableSingleTok.appendChild(trTok);
Td tdTok = new Td();
trTok.appendChild(tdTok);
tdTok.setId("tok_" + prepareID(tok.getId()));
tdTok.setTitle(tooltip);
tdTok.setStyle(style);
tdTok.setAttribute("onclick", onclick);
tdTok.setAttribute("annis:pr_left", left);
tdTok.setAttribute("annis:pr_right", right);
Text textTok = new Text(" " + CommonHelper.getSpannedText(tok) + " ");
tdTok.appendChild(textTok);
}
}
currentlinkcount++;
// while we've got underlines
if (currentPositionComponent.equals(Long.MIN_VALUE)) {
Tr trBlank = new Tr();
tableSingleTok.appendChild(trBlank);
Td tdBlank = new Td();
trBlank.appendChild(tdBlank);
trBlank.setCSSClass("line");
} else {
int color;
if (colorlist.containsKey((int) (long) currentPositionComponent)) {
color = colorlist.get((int) (long) currentPositionComponent);
} else {
color = getNewColor((int) (long) currentPositionComponent);
colorlist.put((int) (long) currentPositionComponent, color);
}
if (color > 16777215) {
color = 16777215;
}
String addition = ";border-style: solid; border-width: 0px 0px 0px 2px; border-color: white ";
if (lastId != null && currentId != null && checklist != null && checklist.size() > index && checklist.get(index).booleanValue() == true) {
if (connectionOf(lastId, currentId, currentPositionComponent)) {
addition = "";
}
}
tooltip = "<b>Component</b>: " + (currentPositionComponent + 1) + ", <b>Type</b>: " + currentType + annotations;
Tr trLineContainer = new Tr();
tableSingleTok.appendChild(trLineContainer);
Td tdLineContainer = new Td();
trLineContainer.appendChild(tdLineContainer);
Table tableLineContainer = new Table();
tdLineContainer.appendChild(tableLineContainer);
trLineContainer.setCSSClass("line");
tableLineContainer.setCSSClass("linecontainer");
Tr trLine = new Tr();
tableLineContainer.appendChild(trLine);
Td tdLine = new Td();
trLine.appendChild(tdLine);
tdLine.setCSSClass("clickableline");
tdLine.setStyle("background-color: #" + Integer.toHexString(color) + "; " + style + addition);
tdLine.setAttribute("onclick", onclick);
tdLine.setAttribute("annis:pr_left", left);
tdLine.setAttribute("annis:pr_right", right);
tdLine.setTitle(tooltip);
Tr trSpace = new Tr();
tableLineContainer.appendChild(trSpace);
Td tdSpace = new Td();
trSpace.appendChild(tdSpace);
tdSpace.setCSSClass("space");
}
}
}
if (currentlinkcount > maxlinkcount) {
maxlinkcount = currentlinkcount;
} else {
if (currentlinkcount < maxlinkcount) {
addEmptyLines(tableSingleTok, maxlinkcount - currentlinkcount);
}
}
} else {
// print a token without lines
Tr trTok = new Tr();
tableSingleTok.appendChild(trTok);
Td tdTok = new Td();
trTok.appendChild(tdTok);
tdTok.setId("tok_" + prepareID(tok.getId()));
tdTok.setStyle(style);
Text textTok = new Text(" " + CommonHelper.getSpannedText(tok) + " ");
tdTok.appendChild(textTok);
if (maxlinkcount > 0) {
addEmptyLines(tableSingleTok, maxlinkcount);
}
}
}
return result;
}
Aggregations