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

fr.univnantes.termsuite.engines.splitter.SuffixDerivationDetecter Maven / Gradle / Ivy


/*******************************************************************************
 * Copyright 2015-2016 - CNRS (Centre National de Recherche Scientifique)
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 fr.univnantes.termsuite.engines.splitter;

import java.util.List;

import org.slf4j.Logger;

import fr.univnantes.termsuite.engines.SimpleEngine;
import fr.univnantes.termsuite.framework.Index;
import fr.univnantes.termsuite.framework.InjectLogger;
import fr.univnantes.termsuite.framework.Resource;
import fr.univnantes.termsuite.framework.service.RelationService;
import fr.univnantes.termsuite.framework.service.TermService;
import fr.univnantes.termsuite.index.TermIndex;
import fr.univnantes.termsuite.index.TermIndexType;
import fr.univnantes.termsuite.model.RelationProperty;
import fr.univnantes.termsuite.model.RelationType;
import fr.univnantes.termsuite.model.Term;
import fr.univnantes.termsuite.model.TermWord;
import fr.univnantes.termsuite.resources.SuffixDerivation;
import fr.univnantes.termsuite.uima.ResourceType;
import fr.univnantes.termsuite.uima.resources.termino.SuffixDerivationList;

public class SuffixDerivationDetecter extends SimpleEngine {
	@InjectLogger Logger logger;

	@Resource(type=ResourceType.SUFFIX_DERIVATIONS)
	private SuffixDerivationList suffixDerivationList;
	
	@Index(type=TermIndexType.SWT_LEMMAS_SWT_TERMS_ONLY)
	TermIndex lemmaIndex;
	
	@Override
	public void execute() {
		int nbDerivations = 0, nbSwt = 0;
		TermWord candidateDerivateTermWord, baseTermWord;
		for(TermService swt:terminology.getTerms()) {
			if(!swt.isSingleWord())
				continue;
			nbSwt++;
			candidateDerivateTermWord = swt.getWords().get(0);
			List derivations = suffixDerivationList.getDerivationsFromDerivateForm(candidateDerivateTermWord);
			for(SuffixDerivation suffixDerivation:derivations) {
				if(suffixDerivation.isKnownDerivate(candidateDerivateTermWord)) {
					baseTermWord = suffixDerivation.getBaseForm(candidateDerivateTermWord);
					List baseTerms = lemmaIndex.getTerms(baseTermWord.getWord().getLemma());
					for(Term baseTerm:baseTerms) {
						if(baseTerm.getWords().get(0).equals(baseTermWord)) {
							nbDerivations++;
							if(logger.isTraceEnabled())
								logger.trace("Found derivation base: {} for derivate word {}", baseTerm, swt);
							RelationService relation = terminology.getRelationOrCreate(baseTerm, RelationType.DERIVES_INTO, swt.getTerm());
							relation.setProperty(RelationProperty.DERIVATION_TYPE, suffixDerivation.getType());
							watch(swt.getTerm(), baseTerm);
						}
					}
				}
			}
		}
		
		logger.debug("Number of derivations found: {} out of {} SWTs", 
				nbDerivations, 
				nbSwt);
	}

	private void watch(Term swt, Term baseTerm) {
		if(history.isPresent()) {
			if(history.get().isTermWatched(swt)) 
				history.get().saveEvent(
						swt, 
						this.getClass(), 
						"Term is a derivate of term " + baseTerm);
			
			if(history.get().isTermWatched(baseTerm)) 
				history.get().saveEvent(
						baseTerm, 
						this.getClass(), 
						"Term has a new found derivate: " + swt);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy