package org.texttechnologylab.project.gruppe_05_1.domain.nlp;

import org.bson.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

public class NamedEntity {
    String  type;      // PER, LOC etc.
    String  text;

    public NamedEntity() {
    }

    public NamedEntity(String type, String text) {
        this.type = type;
        this.text = text;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof NamedEntity that)) return false;
        return Objects.equals(type, that.type) && Objects.equals(text, that.text);
    }

    @Override
    public int hashCode() {
        return Objects.hash(type, text);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", NamedEntity.class.getSimpleName() + "[", "]")
                .add("type='" + type + "'")
                .add("text='" + text + "'")
                .toString();
    }

    public static List<NamedEntity> readNamedEntitiesFromMongo(List<Document> nadocs) {
        List<NamedEntity> nes = new ArrayList<>();
        for (Document doc : nadocs) {
            nes.add(new NamedEntity(
                    doc.getString("type"),
                    doc.getString("text")
            ));
        }
        return nes;
    }
}
