<commit_before>/*
 * Copyright 2009 Collaborative Research Centre SFB 632 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.frontend.servlets.visualizers.partitur;

import annis.frontend.servlets.visualizers.WriterVisualizer;
import annis.service.ifaces.AnnisToken;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringEscapeUtils;

/**
 *
 * @author thomas
 */
public class PartiturVisualizer extends WriterVisualizer
{

  public enum ElementType
  {

    begin,
    end,
    middle,
    single,
    noEvent
  }

  @Override
  public void writeOutput(Writer writer)
  {
    try
    {
      // get partitur
      PartiturParser partitur = new PartiturParser(getResult().getGraph(), getNamespace());

      // check right to left
      boolean isRTL = checkRTL(getResult().getTokenList());

      List<String> tierNames = new LinkedList<String>(partitur.getKnownTiers());
      Collections.sort(tierNames);

      writer.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
      writer.append("<link href=\"" + getContextPath() + "/css/visualizer/partitur.css\" rel=\"stylesheet\" type=\"text/css\" >");
      writer.append("<link href=\"" + getContextPath() + "/javascript/extjs/resources/css/ext-all.css\" rel=\"stylesheet\" type=\"text/css\" >");
      writer.append("<script type=\"text/javascript\" src=\"" + getContextPath() + "/javascript/extjs/adapter/ext/ext-base.js\"></script>");
      writer.append("<script type=\"text/javascript\" src=\"" + getContextPath() + "/javascript/extjs/ext-all.js\"></script>");

      writer.append("<script>\nvar levelNames = [");
      int i = 0;
      for (String levelName : tierNames)
      {
        writer.append((i++ > 0 ? ", " : "") + "\"" + levelName + "\"");
      }
      writer.append("];\n</script>");
      writer.append("<script type=\"text/javascript\" src=\"" + getContextPath() + "/javascript/annis/visualizer/PartiturVisualizer2.js\"></script>");

      writer.append("</head>");
      writer.append("<body>\n");

      writer.append("<div id=\"toolbar\"></div>");
      writer.append("<div id=\"partiture\" style=\"position: absolute; top: 30px; left: 0px;\">");

      if (isRTL)
      {
        writer.append("<table class=\"partitur_table\" dir=\"rtl\">\n");
      }
      else
      {
        writer.append("<table class=\"partitur_table\" >\n");
      }

     for(int iterator=0;iterator<partitur.getNameslist().size();iterator++){

          String tier = (String)partitur.getNameslist().toArray()[iterator];
          List<Long> indexlist = new LinkedList<Long>();

          for(int iterator2=0; iterator2<partitur.getResultlist().size();iterator2++){
                for (PartiturParser.ResultElement strr: partitur.getResultlist().get(iterator2)){
                    if (strr.getName().equals(tier) && !indexlist.contains(strr.getId())) {indexlist.add(strr.getId());}
                }
          }

          List<Long> currentarray = new LinkedList<Long>(); //Saves annotation-ids of the current row

          while (!indexlist.isEmpty()){ //Create Rows until all Annotations fit in
            List<Long> currentdontuselist = new LinkedList<Long>(); //Lists all Annotations that should not be added to the current row
            writer.append("<tr class=\"level_"+tier+"\"><th>" + tier +  "</th>"); //new row
            currentarray.clear();
            for (int iterator3=0; iterator3<partitur.getResultlist().size();iterator3++) {currentarray.add(Long.MIN_VALUE);} //Long.MIN_VALUE == empty
            for(int iterator3=0; iterator3<partitur.getResultlist().size();iterator3++){ //for each Token
                    for (PartiturParser.ResultElement annotationelement: partitur.getResultlist().get(iterator3)){ // for each Annotation annotationelement of that Token
                        if (indexlist.contains(annotationelement.getId()) && !currentdontuselist.contains(annotationelement.getId())){
                            boolean neu=false; //Should the Annotation be added?
                            if (currentarray.get(iterator3).equals(Long.MIN_VALUE)) {
                                indexlist.remove(annotationelement.getId());
                                currentarray.set(iterator3, annotationelement.getId());
                                neu=true;
                            }
                            //get all other annotationelement.id (earlier Ids => dontuselist)
                            for(int iterator4=0; iterator4<partitur.getResultlist().size();iterator4++){
                                for (PartiturParser.ResultElement strr2: partitur.getResultlist().get(iterator4)){
                                    if (strr2.getId()==annotationelement.getId() && neu) //{
                                        if (currentarray.get(iterator4)==Long.MIN_VALUE) currentarray.set(iterator4, annotationelement.getId());
                                        if (iterator4<=iterator3 && !currentdontuselist.contains(strr2.getId())) {
                                            currentdontuselist.add(strr2.getId());
                                        }
                                }
                            }
                            //break; //Not needed?
                        }
                    }
                }
         

            //Write Row
            int length=1;
            for(int iterator5=0; iterator5<currentarray.size();iterator5+=length){
                StringBuffer tokenIdsArray = new StringBuffer();
                StringBuffer eventIdsArray = new StringBuffer();
                boolean unused = true;
                length=1;
                if (currentarray.get(iterator5)==Long.MIN_VALUE){ //empty entry
                     writer.append("<td></td>");
                }else{
                    PartiturParser.ResultElement element=null;
                    HashSet<Integer> common = new HashSet<Integer>();
                    boolean found=false;
                    for(int iterator6=0; iterator6<partitur.getResultlist().size();iterator6++){
                                for (PartiturParser.ResultElement strr: partitur.getResultlist().get(iterator6)){
                                    if (strr.getId()==currentarray.get(iterator5)) {
                                        if (!found) element=strr;
                                        if (!common.contains(iterator6)) common.add(iterator6);
                                        found=true;
                                        if (unused) {
                                            tokenIdsArray.append("" + strr.getId() + "_" + iterator6);
                                            eventIdsArray.append(tier + "_" + strr.getId() + "_" + iterator6);
                                            unused=false;
                                        } else{
                                            tokenIdsArray.append("," + strr.getId() + "_" + iterator6);
                                            eventIdsArray.append(","+tier + "_" + strr.getId() + "_" + iterator6);
                                        }
                                    }
                                }
                    }
                    for (int iterator7=iterator5+1; iterator7<currentarray.size();iterator7++)
                        if (common.contains(iterator7)) {
                            length++;
                        }
                        else break;

                    for (int iterator8=0; iterator8<currentarray.size();iterator8++)
                        if (common.contains(iterator8)) {
                            Long id = ((PartiturParser.Token)partitur.getToken().toArray()[iterator8]).getId();
                            if (unused) {
                                tokenIdsArray.append("" + id);
                                eventIdsArray.append(tier + "_" + id);
                                unused=false;
                            } else{
                                tokenIdsArray.append("," + id);
                                eventIdsArray.append(","+tier + "_" + id);
                            }
                        }
                    if (found)
                         writer.append("<td class=\"single_event\" "
                            + "id=\"event_" + tier + "_" + element.getId() + "_" + iterator5 + "\" "
                            + "style=\"color:black;\" "
                            + "colspan=" + length + " "
                            + "annis:tokenIds=\"" + tokenIdsArray + "\" "
                            + "annis:eventIds=\"" + eventIdsArray + "\" "
                            + "ext:qtip=\"" + partitur.namespaceForTier(tier) + ":" + tier + " = " + StringEscapeUtils.escapeXml(element.getValue()) + "\"  " //tier =tier, event.getValue()= element.name
                            + "onMouseOver=\"toggleAnnotation(this, true);\" "
                            + "onMouseOut=\"toggleAnnotation(this, false);\""
                             + ">" + element.getValue() + "</td>");
                    else writer.append("<td class=\"single_event\" >error</td>");
                }
            }
            writer.append("</tr>");     //finish row
          }
      }

      // add token itself
      writer.append("<tr><th>tok</th>");<commit_msg>- color in the partitur table was lost<commit_after>/*
 * Copyright 2009 Collaborative Research Centre SFB 632 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.frontend.servlets.visualizers.partitur;

import annis.frontend.servlets.visualizers.WriterVisualizer;
import annis.service.ifaces.AnnisToken;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.StringEscapeUtils;

/**
 *
 * @author thomas
 */
public class PartiturVisualizer extends WriterVisualizer
{

  public enum ElementType
  {

    begin,
    end,
    middle,
    single,
    noEvent
  }

  @Override
  public void writeOutput(Writer writer)
  {
    try
    {
      // get partitur
      PartiturParser partitur = new PartiturParser(getResult().getGraph(), getNamespace());

      // check right to left
      boolean isRTL = checkRTL(getResult().getTokenList());

      List<String> tierNames = new LinkedList<String>(partitur.getKnownTiers());
      Collections.sort(tierNames);

      writer.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
      writer.append("<link href=\"" + getContextPath() + "/css/visualizer/partitur.css\" rel=\"stylesheet\" type=\"text/css\" >");
      writer.append("<link href=\"" + getContextPath() + "/javascript/extjs/resources/css/ext-all.css\" rel=\"stylesheet\" type=\"text/css\" >");
      writer.append("<script type=\"text/javascript\" src=\"" + getContextPath() + "/javascript/extjs/adapter/ext/ext-base.js\"></script>");
      writer.append("<script type=\"text/javascript\" src=\"" + getContextPath() + "/javascript/extjs/ext-all.js\"></script>");

      writer.append("<script>\nvar levelNames = [");
      int i = 0;
      for (String levelName : tierNames)
      {
        writer.append((i++ > 0 ? ", " : "") + "\"" + levelName + "\"");
      }
      writer.append("];\n</script>");
      writer.append("<script type=\"text/javascript\" src=\"" + getContextPath() + "/javascript/annis/visualizer/PartiturVisualizer2.js\"></script>");

      writer.append("</head>");
      writer.append("<body>\n");

      writer.append("<div id=\"toolbar\"></div>");
      writer.append("<div id=\"partiture\" style=\"position: absolute; top: 30px; left: 0px;\">");

      if (isRTL)
      {
        writer.append("<table class=\"partitur_table\" dir=\"rtl\">\n");
      }
      else
      {
        writer.append("<table class=\"partitur_table\" >\n");
      }

     for(int iterator=0;iterator<partitur.getNameslist().size();iterator++){

          String tier = (String)partitur.getNameslist().toArray()[iterator];
          List<Long> indexlist = new LinkedList<Long>();

          for(int iterator2=0; iterator2<partitur.getResultlist().size();iterator2++){
                for (PartiturParser.ResultElement strr: partitur.getResultlist().get(iterator2)){
                    if (strr.getName().equals(tier) && !indexlist.contains(strr.getId())) {indexlist.add(strr.getId());}
                }
          }

          List<Long> currentarray = new LinkedList<Long>(); //Saves annotation-ids of the current row

          while (!indexlist.isEmpty()){ //Create Rows until all Annotations fit in
            List<Long> currentdontuselist = new LinkedList<Long>(); //Lists all Annotations that should not be added to the current row
            writer.append("<tr class=\"level_"+tier+"\"><th>" + tier +  "</th>"); //new row
            currentarray.clear();
            for (int iterator3=0; iterator3<partitur.getResultlist().size();iterator3++) {currentarray.add(Long.MIN_VALUE);} //Long.MIN_VALUE == empty
            for(int iterator3=0; iterator3<partitur.getResultlist().size();iterator3++){ //for each Token
                    for (PartiturParser.ResultElement annotationelement: partitur.getResultlist().get(iterator3)){ // for each Annotation annotationelement of that Token
                        if (indexlist.contains(annotationelement.getId()) && !currentdontuselist.contains(annotationelement.getId())){
                            boolean neu=false; //Should the Annotation be added?
                            if (currentarray.get(iterator3).equals(Long.MIN_VALUE)) {
                                indexlist.remove(annotationelement.getId());
                                currentarray.set(iterator3, annotationelement.getId());
                                neu=true;
                            }
                            //get all other annotationelement.id (earlier Ids => dontuselist)
                            for(int iterator4=0; iterator4<partitur.getResultlist().size();iterator4++){
                                for (PartiturParser.ResultElement strr2: partitur.getResultlist().get(iterator4)){
                                    if (strr2.getId()==annotationelement.getId() && neu) //{
                                        if (currentarray.get(iterator4)==Long.MIN_VALUE) currentarray.set(iterator4, annotationelement.getId());
                                        if (iterator4<=iterator3 && !currentdontuselist.contains(strr2.getId())) {
                                            currentdontuselist.add(strr2.getId());
                                        }
                                }
                            }
                            //break; //Not needed?
                        }
                    }
                }
         

            //Write Row
            int length=1;
            for(int iterator5=0; iterator5<currentarray.size();iterator5+=length){
                StringBuffer tokenIdsArray = new StringBuffer();
                StringBuffer eventIdsArray = new StringBuffer();
                boolean unused = true;
                length=1;
                if (currentarray.get(iterator5)==Long.MIN_VALUE){ //empty entry
                     writer.append("<td></td>");
                }else{
                    PartiturParser.ResultElement element=null;
                    HashSet<Integer> common = new HashSet<Integer>();
                    boolean found=false;
                    for(int iterator6=0; iterator6<partitur.getResultlist().size();iterator6++){
                                for (PartiturParser.ResultElement strr: partitur.getResultlist().get(iterator6)){
                                    if (strr.getId()==currentarray.get(iterator5)) {
                                        if (!found) element=strr;
                                        if (!common.contains(iterator6)) common.add(iterator6);
                                        found=true;
                                        if (unused) {
                                            tokenIdsArray.append("" + strr.getId() + "_" + iterator6);
                                            eventIdsArray.append(tier + "_" + strr.getId() + "_" + iterator6);
                                            unused=false;
                                        } else{
                                            tokenIdsArray.append("," + strr.getId() + "_" + iterator6);
                                            eventIdsArray.append(","+tier + "_" + strr.getId() + "_" + iterator6);
                                        }
                                    }
                                }
                    }
                    for (int iterator7=iterator5+1; iterator7<currentarray.size();iterator7++)
                        if (common.contains(iterator7)) {
                            length++;
                        }
                        else break;

                    for (int iterator8=0; iterator8<currentarray.size();iterator8++)
                        if (common.contains(iterator8)) {
                            Long id = ((PartiturParser.Token)partitur.getToken().toArray()[iterator8]).getId();
                            if (unused) {
                                tokenIdsArray.append("" + id);
                                eventIdsArray.append(tier + "_" + id);
                                unused=false;
                            } else{
                                tokenIdsArray.append("," + id);
                                eventIdsArray.append(","+tier + "_" + id);
                            }
                        }

                    String color = "black";
                    if (getMarkableMap().containsKey("" + element.getId()))
                    {
                      color = getMarkableMap().get("" + element.getId());
                    }
                    if (found)
                         writer.append("<td class=\"single_event\" "
                            + "id=\"event_" + tier + "_" + element.getId() + "_" + iterator5 + "\" "
                            + "style=\"color:" + color + ";\" "
                            + "colspan=" + length + " "
                            + "annis:tokenIds=\"" + tokenIdsArray + "\" "
                            + "annis:eventIds=\"" + eventIdsArray + "\" "
                            + "ext:qtip=\"" + partitur.namespaceForTier(tier) + ":" + tier + " = " + StringEscapeUtils.escapeXml(element.getValue()) + "\"  " //tier =tier, event.getValue()= element.name
                            + "onMouseOver=\"toggleAnnotation(this, true);\" "
                            + "onMouseOut=\"toggleAnnotation(this, false);\""
                             + ">" + element.getValue() + "</td>");
                    else writer.append("<td class=\"single_event\" >error</td>");
                }
            }
            writer.append("</tr>");     //finish row
          }
      }

      // add token itself
      writer.append("<tr><th>tok</th>");
{'fraction_non_alphanumeric': 0.13509149623250807, 'fraction_numerical': 0.007828554653097172, 'mean_word_length': 1.6328738889604535, 'pattern_counts': {'":': 2, '<': 109, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1923757', 'n_tokens_mistral': 5233, 'n_tokens_neox': 5056, 'n_words': 980}