package com.formulasearchengine.mathosphere.mlp.pojos;


import com.formulasearchengine.mathosphere.mlp.text.PosTag;

import org.apache.flink.api.java.tuple.Tuple2;

import java.util.Map;

public class Relation implements Comparable<Relation> {

  private String identifier;
  private String definition;
  private double score;
  private int identifierPosition;
  private int wordPosition;
  private Sentence sentence;
  private Integer relevance;

  public Relation() {
  }

  public Relation(String identifier, String definition) {
    this.identifier = identifier;
    this.definition = definition;
  }

  public Relation(Object json) {
    Map item = (Map) json;
    identifier = (String) item.get("identifier");
    definition = (String) item.get("top_definition");
    score = (double) item.get("top_definition_score");
  }

  public Double getScore() {
    return score;
  }

  public void setScore(double score) {
    this.score = score;
  }

  public String getIdentifier() {
    return identifier;
  }

  public void setIdentifier(String identifier) {
    this.identifier = identifier;
  }

  public String getDefinition() {
    return definition;
  }

  public void setDefinition(Word word) {
    if (word.getPosTag().equals(PosTag.LINK)) {
      this.definition = "[[" + word.getWord() + "]]";
    } else {
      this.definition = word.getWord();
    }
  }

  public void setDefinition(String definition) {
    this.definition = definition;
  }

  public Sentence getSentence() {
    return sentence;
  }

  public void setSentence(Sentence sentence) {
    this.sentence = sentence;
  }

  public int getIdentifierPosition() {
    return identifierPosition;
  }

  public void setIdentifierPosition(int identifierPosition) {
    this.identifierPosition = identifierPosition;
  }

  public int getWordPosition() {
    return wordPosition;
  }

  public void setWordPosition(int wordPosition) {
    this.wordPosition = wordPosition;
  }

  @Override
  public String toString() {
    return "Relation [" + identifier + ", score=" + score + ", word=" + definition + "]";
  }


  @Override
  public int compareTo(Relation o) {
    return (o.getScore()).compareTo(getScore());
  }

  public int compareToName(Relation o) {
    int res = getIdentifier().compareTo(o.getIdentifier());
    if (res == 0) {
      res = getDefinition().compareToIgnoreCase(o.getDefinition());
    }
    if ( res == 0 ){
      res = countLowerCaseChars(o.getDefinition()).compareTo(countLowerCaseChars(getDefinition()));
    }
    return res;
  }

  public int compareNameScore(Relation o) {
    int res = getIdentifier().compareTo(o.getIdentifier());
    if (res == 0) {
      res = getDefinition().compareToIgnoreCase(o.getDefinition());
    }
    if ( res == 0 ){
      res = compareTo(o); //Sort by score desc
    }
    return res;
  }
  private Integer countLowerCaseChars(String s){
    int upperCase = 0;
    for (int k = 0; k < s.length(); k++) {
      if (Character.isUpperCase(s.charAt(k))) upperCase++;

    }
    return upperCase;
  }
  public void setDefinition(Word word, ParsedWikiDocument doc) {
    if (word.getPosTag().equals(PosTag.LINK)) {
      String hash = word.getWord().replaceAll("^LINK_", "");
      WikidataLink link = doc.getLinkMap().get(hash);
      if (link != null) {
        this.definition = "[[" + link.getContent() + "]]";
      } else {
        this.definition = "[[" + word.getWord() + "]]";
      }
    } else {
      this.definition = word.getWord();
    }
  }

  public Integer getRelevance() {
    return relevance;
  }

  public Relation setRelevance(int relevance) {
    this.relevance = relevance;
    return this;
  }

  public Tuple2<String,String> getTuple(){
    return new Tuple2<>(identifier,definition);
  }
}

@@ -182,16 +182,101 @@
 l.Map;%0A%0A
+import static com.formulasearchengine.mathosphere.mlp.text.WikiTextUtils.deLinkify;%0A%0A
 public c
@@ -378,24 +378,63 @@
 definition;%0A
+  /**%0A   * The calculated score.%0A   */%0A
   private do
@@ -485,62 +485,303 @@
 ;%0A  
-private int wordPosition;%0A  private Sentence sentence;
+/**%0A   * Position of the definiens.%0A   */%0A  private int wordPosition;%0A  /**%0A   * The sentence containing the relation.%0A   */%0A  private Sentence sentence;%0A  /**%0A   * The relevance score, when compared to e.g. a gold standard.%0A   * 0 = irrelevant; 1 = partly relevant; 2 = correct definiens.%0A   */
 %0A  p
@@ -2745,33 +2745,32 @@
   %7D%0A    if (
- 
 res == 0
  )%7B%0A      re
@@ -2749,34 +2749,34 @@
     if (res == 0
- 
 )
+ 
 %7B%0A      res = co
@@ -3098,17 +3098,16 @@
 if (
- 
 res == 0
  )%7B%0A
@@ -3106,10 +3106,10 @@
 == 0
- 
 )
+ 
 %7B%0A  
@@ -3179,16 +3179,17 @@
 es;%0A  %7D%0A
+%0A
   privat
@@ -3227,16 +3227,17 @@
 tring s)
+ 
 %7B%0A    in
@@ -3388,16 +3388,17 @@
 se;%0A  %7D%0A
+%0A
   public
@@ -3462,389 +3462,47 @@
     
-if (word.getPosTag().equals(PosTag.LINK)) %7B%0A      String hash = word.getWord().replaceAll(%22%5ELINK_%22, %22%22);%0A      WikidataLink link = doc.getLinkMap().get(hash);%0A      if (link != null) %7B%0A        this.definition = %22%5B%5B%22 + link.getContent() + %22%5D%5D%22;%0A      %7D else %7B%0A        this.definition = %22%5B%5B%22 + word.getWord() + %22%5D%5D%22;%0A      %7D%0A    %7D else %7B%0A      this.definition = word.getWord();%0A    %7D
+this.definition = deLinkify(word, doc);
 %0A  %7D
@@ -3689,16 +3689,17 @@
 %3CString,
+ 
 String%3E 
@@ -3708,16 +3708,17 @@
 tTuple()
+ 
 %7B%0A    re
@@ -3746,16 +3746,17 @@
 ntifier,
+ 
 definiti

Add documentation Use utility method