All Downloads are FREE. Search and download functionalities are using the official Maven repository.

annis.visualizers.iframe.partitur.PartiturParser Maven / Gradle / Ivy

There is a newer version: 4.0.0-beta.4
Show newest version
/*
 * Copyright 2009-2011 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.visualizers.iframe.partitur;

import annis.model.AnnisNode;
import annis.model.Annotation;
import annis.model.AnnotationGraph;
import annis.model.Edge;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author thomas
 */
public class PartiturParser implements Serializable
{

  private List token;
  private Set knownTiers;
  private HashMap tier2ns;
  private TreeSet nameslist;
  private List> resultlist;

  public PartiturParser(AnnotationGraph graph, String namespace)
  {
    resultlist = new LinkedList>();
    nameslist = new TreeSet();

    for (AnnisNode n : graph.getTokens())
    {
      List helper = new LinkedList();
      for (Edge edge : n.getIncomingEdges())
      {
        if (edge.getEdgeType() == Edge.EdgeType.COVERAGE)
        {
          AnnisNode parentNode = edge.getSource();
          if (parentNode.getNamespace().equals(namespace))
          {
            for (Annotation anno : parentNode.getNodeAnnotations())
            {
              String newId = "" +  parentNode.getId() + "_" + anno.getNamespace() + "_" + anno.getName();
              helper.add(new ResultElement(newId, parentNode.getId(), anno.getName(), anno.getValue()));
              if (!nameslist.contains(anno.getName()))
              {
                nameslist.add(anno.getName());
              }
            }
          }
        }
      }
      resultlist.add(helper);
    }

    token = new LinkedList();
    knownTiers = new HashSet();
    tier2ns = new HashMap();
    for (AnnisNode n : graph.getTokens())
    {
      long tokenID = n.getId();

      Token currentToken = new Token(tokenID, new Hashtable(), n.getSpannedText());
      token.add(currentToken);

      // get parent annotations matching namespace
      for (Edge edge : n.getIncomingEdges())
      {
        if (edge.getEdgeType() == Edge.EdgeType.COVERAGE)
        {
          AnnisNode parentNode = edge.getSource();

          if (parentNode.getNamespace().equals(namespace))
          {
            for (Annotation anno : parentNode.getNodeAnnotations())
            {
              // finally, put this annotation in the list
              Event newEvent = new Event(parentNode.getId(), anno.getValue());
              currentToken.getTier2Event().put(anno.getName(), newEvent);
              // update our set of tiers
              knownTiers.add(anno.getName());
              tier2ns.put(anno.getName(), anno.getNamespace());
            }
          }
        }
      }
    }

    // now connect the token to make it easier later to find the neighbors
    Iterator it = token.iterator();

    Token current = it.hasNext() ? it.next() : null;
    Token next = it.hasNext() ? it.next() : null;
    Token last = null;

    while (current != null)
    {
      current.setBefore(last);
      current.setAfter(next);

      last = current;
      current = next;
      next = it.hasNext() ? it.next() : null;
    }

  }

  public TreeSet getNameslist()
  {
    return nameslist;
  }

  public List> getResultlist()
  {
    return resultlist;
  }

  public Set getKnownTiers()
  {
    return knownTiers;
  }

  public String namespaceForTier(String tier)
  {
    return tier2ns.get(tier);
  }

  public List getToken()
  {
    return token;
  }

  public static class Token implements Serializable
  {

    private Map tier2Event;
    private long id;
    private String value;
    private Token before;
    private Token after;

    public Token(long id, Map tier2Event, String value)
    {
      this.tier2Event = tier2Event;
      this.id = id;
      this.value = value;
      before = null;
      after = null;
    }

    public Map getTier2Event()
    {
      return tier2Event;
    }

    public long getId()
    {
      return id;
    }

    public Token getAfter()
    {
      return after;
    }

    public void setAfter(Token after)
    {
      this.after = after;
    }

    public Token getBefore()
    {
      return before;
    }

    public void setBefore(Token before)
    {
      this.before = before;
    }

    public String getValue()
    {
      return value;
    }
  }

  public static class Event implements Serializable
  {

    private long id;
    private String value;

    public Event(long id, String value)
    {
      this.id = id;
      this.value = value;
    }

    public long getId()
    {
      return id;
    }

    public String getValue()
    {
      return value;
    }
  }

  public static class ResultElement implements Serializable
  {

    private String id;
    private long nodeId;
    private String name, value;

    public String getName()
    {
      return name;
    }

    public String getId()
    {
      return id;
    }


    public String getValue()
    {
      return value;
    }

    public long getNodeId()
    {
      return nodeId;
    }



    ResultElement(String id, long nodeId, String name, String value)
    {
      this.id = id;
      this.nodeId = nodeId;
      this.name = name;
      this.value = value;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy