TEIMorphImpl.java 4.1 KB
package ipipan.clarin.tei.impl.entities;

import ipipan.clarin.tei.api.entities.EntitiesFactory;
import ipipan.clarin.tei.api.entities.TEIInterpretation;
import ipipan.clarin.tei.api.entities.TEILex;
import ipipan.clarin.tei.api.entities.TEIMorph;
import ipipan.clarin.tei.api.entities.TEINamedEntity;
import ipipan.clarin.tei.api.entities.TEISegment;
import ipipan.clarin.tei.api.entities.TEIWord;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 
 * @author mlenart
 */
class TEIMorphImpl extends TEIAbstractEntity implements TEIMorph {

	private final TEISegment corresp;
	private final String orth;
	private final boolean nps;
	private final List<TEILex> lexems;
	private final Map<TEIInterpretation, String> interp2MsdId;
	private TEIInterpretation chosenInterp;

	public TEIMorphImpl(String id, TEISegment corresp, String orth,
			boolean nps, List<TEILex> lexems) {
		super(id);
		this.corresp = corresp;
		this.orth = orth;
		this.nps = nps;
		this.lexems = lexems;

		this.interp2MsdId = new LinkedHashMap<TEIInterpretation, String>();
		for (TEILex lex : lexems)
			for (Map.Entry<String, String> entry : lex.getMsd().entrySet()) {
				String msdId = entry.getKey();
				String msd = entry.getValue();
				interp2MsdId.put(
						EntitiesFactory.getInstance().createInterpretation(
								lex.getBase(), lex.getCTag(), msd), msdId);
			}
	}

	TEIMorphImpl(String id, TEISegment seg) {
		super(id);
		this.corresp = seg;
		this.orth = seg.getOrth();
		this.nps = seg.hasNps();
		this.lexems = Lists.newLinkedList();
		this.interp2MsdId = Maps.newHashMap();
	}

	@Override
	public TEISegment getCorrespSegment() {
		return corresp;
	}

	@Override
	public String getOrth() {
		return orth;
	}

	@Override
	public boolean hasNps() {
		return nps;
	}

	@Override
	public List<TEILex> getLexems() {
		return lexems;
	}

	@Override
	public List<TEIInterpretation> getAllInterpretations() {
		return new LinkedList<TEIInterpretation>(interp2MsdId.keySet());
	}

	@Override
	public TEIInterpretation getChosenInterpretation() {
		return chosenInterp;
	}

	@Override
	public void setChosenInterpretation(TEIInterpretation interp) {
		if (!getAllInterpretations().contains(interp)) {
			addInterpretation(interp);
		}
		this.chosenInterp = interp;
	}

	@Override
	public void addInterpretation(TEIInterpretation interp) {
		TEILex lex = null;
		for (TEILex lex1 : lexems) {
			if (lex1.getBase().equals(interp.getBase())
					&& lex1.getCTag().equals(interp.getCtag())) {
				lex = lex1;
			}
		}
		if (lex != null && lex.getMsd().values().contains(interp.getMorph())) {
			// do nothing
		} else if (lex != null) {
			String newMsdId = lex.getId() + "." + (lex.getMsd().size() + 1);
			lex.getMsd().put(newMsdId, interp.getMorph());
			interp2MsdId.put(interp, newMsdId);
		} else {
			String newLexId = this.getId() + "."
					+ (this.getLexems().size() + 1);
			String newMsdId = newLexId + "." + 1;
			Map<String, String> msdMap = Maps.newHashMap();
			msdMap.put(newMsdId, interp.getMorph());
			lex = EntitiesFactory.getInstance().createLex(newLexId,
					interp.getBase(), interp.getCtag(), msdMap);
			lexems.add(lex);
			interp2MsdId.put(interp, newMsdId);
		}
	}

	@Override
	public boolean isChosenInterpretation(TEIInterpretation interp) {
		return interp.equals(chosenInterp);
	}

	@Override
	public boolean isWord() {
		return false;
	}

	@Override
	public boolean isMorph() {
		return true;
	}

	@Override
	public boolean isNamedEntity() {
		return false;
	}

	@Override
	public String getInterpMsdId(TEIInterpretation interp) {
		return interp2MsdId.get(interp);
	}

	@Override
	public String getChosenInterpMsdId() {
		return interp2MsdId.get(getChosenInterpretation());
	}

	@Override
	public TEIMorph asMorph() {
		return this;
	}

	@Override
	public TEINamedEntity asNamedEntity() {
		throw new UnsupportedOperationException(
				"Cannot treat morph as named entity");
	}

	@Override
	public TEIWord asWord() {
		throw new UnsupportedOperationException("Cannot treat morph as word");
	}

	public String toString() {
		return orth;
	}

}