Copyright 2016, 2017 Institut National de la Recherche Agronomique

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


Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

package fr.inra.maiage.bibliome.alvisnlp.bibliomefactory.modules.trie;

import java.io.IOException;
import java.util.List;
import java.util.logging.Logger;

import fr.inra.maiage.bibliome.alvisnlp.bibliomefactory.modules.SectionModule.SectionResolvedObjects;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.Annotation;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.Corpus;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.NameType;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.expressions.ResolverException;
import fr.inra.maiage.bibliome.alvisnlp.core.module.ModuleException;
import fr.inra.maiage.bibliome.alvisnlp.core.module.ProcessingContext;
import fr.inra.maiage.bibliome.alvisnlp.core.module.TimerCategory;
import fr.inra.maiage.bibliome.alvisnlp.core.module.lib.AlvisNLPModule;
import fr.inra.maiage.bibliome.alvisnlp.core.module.lib.Param;
import fr.inra.maiage.bibliome.alvisnlp.core.module.lib.TimeThis;
import fr.inra.maiage.bibliome.util.filelines.FileLines;
import fr.inra.maiage.bibliome.util.filelines.InvalidFileLineEntry;
import fr.inra.maiage.bibliome.util.filelines.TabularFormat;
import fr.inra.maiage.bibliome.util.marshall.Decoder;
import fr.inra.maiage.bibliome.util.marshall.Encoder;
import fr.inra.maiage.bibliome.util.streams.SourceStream;
import fr.inra.maiage.bibliome.util.trie.Trie;

public abstract class TabularProjector extends TrieProjector<SectionResolvedObjects,List<String>> {
	private Integer[] keyIndex = new Integer[] { 0 };
	private SourceStream dictFile;
	private String[] valueFeatures;
	private Character separator = '\t';
	private Boolean strictColumnNumber = true;
	private Boolean skipEmpty = false;
	private Boolean skipBlank = false;
	private Boolean trimColumns = false;

	protected SectionResolvedObjects createResolvedObjects(ProcessingContext<Corpus> ctx) throws ResolverException {
		return new SectionResolvedObjects(ctx, this);

	protected void fillTrie(Logger logger, Trie<List<String>> trie, Corpus corpus) throws IOException, ModuleException {
		logger.info("reading dictionary from: " + dictFile);
		TabularFormat format = new TabularFormat();
		logger.info("we expect lines with " + valueFeatures.length + " columns");
		if (!strictColumnNumber)
			logger.warning("you deliberately choose to ignore malformed dictionary lines");
		if (skipEmpty)
			logger.warning("skipping empty lines");
		if (skipBlank)
			logger.warning("skipping lines with only whitespace");
		if (trimColumns)
			logger.warning("columns will be trimmed from leading and trailing whitespace");
		FileLines<Trie<List<String>>> fl = new EntryFileLines(format, keyIndex);
		try {
			fl.process(dictFile, trie);
		catch (InvalidFileLineEntry e) {

	protected void finish() {

	private static final class EntryFileLines extends FileLines<Trie<List<String>>> {
		private final Integer[] keyIndex;
		private EntryFileLines(TabularFormat format, Integer[] keyIndex) {
			this.keyIndex = keyIndex;

		public void processEntry(Trie<List<String>> data, int lineno, List<String> entry) throws InvalidFileLineEntry {
			for (int i : keyIndex) {
				String key = entry.get(i);
				data.addEntry(key, entry);

	@TimeThis(task="create-trie", category=TimerCategory.LOAD_RESOURCE)
	protected Trie<List<String>> getTrie(ProcessingContext<Corpus> ctx, Logger logger, Corpus corpus) throws IOException, ModuleException {
		return super.getTrie(ctx, logger, corpus);

	protected void handleMatch(List<String> value, Annotation a) {
		final int len = Math.min(valueFeatures.length, value.size());
		for (int i = 0; i < len; ++i)
			a.addFeature(valueFeatures[i], value.get(i));

	protected boolean marshallingSupported() {
		return true;

	protected Decoder<List<String>> getDecoder() {
		return StringListCodex.INSANCE;

	protected Encoder<List<String>> getEncoder() {
		return StringListCodex.INSANCE;

	public Integer[] getKeyIndex() {
		return keyIndex;

	public SourceStream getDictFile() {
		return dictFile;

	public String[] getValueFeatures() {
		return valueFeatures;

	public Character getSeparator() {
		return separator;

	public Boolean getStrictColumnNumber() {
		return strictColumnNumber;

	public Boolean getSkipEmpty() {
		return skipEmpty;

	public Boolean getSkipBlank() {
		return skipBlank;

	public Boolean getTrimColumns() {
		return trimColumns;

	public void setKeyIndex(Integer[] keyIndex) {
		this.keyIndex = keyIndex;

	public void setDictFile(SourceStream dictFile) {
		this.dictFile = dictFile;

	public void setValueFeatures(String[] valueFeatures) {
		this.valueFeatures = valueFeatures;

	public void setSeparator(Character separator) {
		this.separator = separator;

	public void setStrictColumnNumber(Boolean strictColumnNumber) {
		this.strictColumnNumber = strictColumnNumber;

	public void setSkipEmpty(Boolean skipEmpty) {
		this.skipEmpty = skipEmpty;

	public void setSkipBlank(Boolean skipBlank) {
		this.skipBlank = skipBlank;

	public void setTrimColumns(Boolean trimColumns) {
		this.trimColumns = trimColumns;