Skip to content

Commit

Permalink
add dependency processing and recognition (#30)
Browse files Browse the repository at this point in the history
* Add agent for dependencies

* Dependency extraction

* Add agent and extractor to get dependencies
* Initial stubs only until now

* Extend DependencyTags

* Changed descriptions, added checks for Word types, DOBJ to OBJ

* Started with pipeline for RecommendedRelationExtractor (rename?)

* Rename DependencyExtractionAgent to RecommendedRelationAgent

* Rename RecommendedRelationAgent to InstanceRelationAgent

* Adjust InstanceRelationAgent to InstanceRelation

* Interface for InstanceRelation

* InstanceRelation used by InstanceRelationAgent

* Adjust RecommendationGenerator to use InstanceRelationAgent

* Override equals for Word class

* Add InstanceRelation to RecommendationState

* Lower needed similarity for recommended instances in text extractor

* Adjust instance relation to only compare between recommended instances

* Remove lemma, only compare considering recommended instances
* Only use two instances for identification of a relation (instead of
lists)
* Add some documentation
* Adjust probability calculation (still experimental)
* If matching relation between instances is found, add relation between
words there
* Choose "from" and "to" words according to their position in text

* Add simple probability calculation with max 1

* Adjust to merge

* Add provisional IWord methods to implementing classes
* Adjust usages of ImmutableLists
* Adjust DependencyAgent

* Fix incorrect link

* Adjust similarity to retrieve more RecommendedInstances

* Higher probability to generate InstanceRelations

* Override recommended hashCode in InstanceRelation

* some cleanup

* update gitignore

* move word check for POS to WordHelper

* improve code quality

Co-authored-by: Jan Keim <[email protected]>
  • Loading branch information
maGitty and Gram21 authored Aug 26, 2021
1 parent 4cf317a commit 1991d75
Show file tree
Hide file tree
Showing 19 changed files with 523 additions and 25 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
testout
tests/src/test/resources/testout/*

tests/src/test/resources/config.properties

Expand Down Expand Up @@ -103,3 +103,8 @@ Pipeline/src/main/resources/texts/
Pipeline/evaluations/
Pipeline/ecsaEvaluations/
pipeline/src/main/resources/evaluations/

# jetbrains config
.idea

*.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package edu.kit.kastel.mcse.ardoco.core.datastructures.agents;

import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IModelState;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IRecommendationState;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IText;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.ITextState;

import java.util.Objects;

public abstract class DependencyAgent extends Agent {

protected IText text;
protected ITextState textState;
protected IModelState modelState;
protected IRecommendationState recommendationState;

protected DependencyAgent(Class<? extends Configuration> configType) {
super(configType);
}

protected DependencyAgent(Class<? extends Configuration> configType, IText text,
ITextState textState, IModelState modelState, IRecommendationState recommendationState) {
super(configType);
this.text = text;
this.textState = textState;
this.modelState = modelState;
this.recommendationState = recommendationState;
}

@Override
protected final DependencyAgent createInternal(AgentDatastructure data, Configuration config) {
Objects.requireNonNull(data.getText());
Objects.requireNonNull(data.getTextState());
Objects.requireNonNull(data.getModelState());
Objects.requireNonNull(data.getRecommendationState());

return this.create(data.getText(), data.getTextState(), data.getModelState(), data.getRecommendationState(), config);
}

public abstract DependencyAgent create(IText text, ITextState textState, IModelState modelState, IRecommendationState recommendationState,
Configuration config);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package edu.kit.kastel.mcse.ardoco.core.datastructures.definitions;

import java.util.List;

public interface IInstanceRelation {
IInstanceRelation createCopy();

boolean addLink(IWord relator, List<IWord> from, List<IWord> to);

boolean matches(IRecommendedInstance fromInstance, IRecommendedInstance toInstance);

boolean isIn(IWord relator, List<IWord> from, List<IWord> to);

double getProbability();

void setProbability(double newProbability);

IRecommendedInstance getFromInstance();

IRecommendedInstance getToInstance();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import edu.kit.kastel.mcse.ardoco.core.datastructures.modules.IState;

import java.util.List;

/**
* The Interface IRecommendationState defines the state for recommendations.
*/
Expand All @@ -23,6 +25,13 @@ public interface IRecommendationState extends IState<IRecommendationState> {
*/
ImmutableList<IRecommendedRelation> getRecommendedRelations();

/**
* Returns all instance relations.
*
* @return all instance relations as list
*/
ImmutableList<IInstanceRelation> getInstanceRelations();

/**
* Adds a new recommended relation.
*
Expand All @@ -36,6 +45,12 @@ public interface IRecommendationState extends IState<IRecommendationState> {
void addRecommendedRelation(String name, IRecommendedInstance ri1, IRecommendedInstance ri2, ImmutableList<IRecommendedInstance> otherInstances,
double probability, ImmutableList<IWord> occurrences);

void addInstanceRelation(IRecommendedInstance fromInstance,
IRecommendedInstance toInstance,
IWord relator,
List<IWord> from,
List<IWord> to);

/**
* Adds a recommended instance without a type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,4 @@ public interface IWord {
* @return the words that are dependent on this
*/
ImmutableList<IWord> getWordsThatAreDependentOnThis(DependencyTag dependencyTag);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.apache.logging.log4j.Logger;

/**
* This class represents all valid pos tags.
* This class represents all valid part-of-speech (pos) tags
*
* @author Sebastian Weigelt
* @author Markus Kocybik
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Text input parameters (one of them has to be provided):
}

/**
* Run the approach equally to {@link #runAndSave(String, File, File, File, File, boolean)} but without saving the
* Run the approach equally to {@link #runAndSave(String, File, File, File, File)} but without saving the
* output to the file system.
*
* @param name Name of the run
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package edu.kit.kastel.mcse.ardoco.core.datastructures;

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

import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IInstanceRelation;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IRecommendedInstance;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IWord;

/**
* Relation between RecommendedInstances, store specific occurrences as
*
* @see LocalRelation
*
* TODO fromInstance and toInstance to List to comprise more complex relations?!
*/
public class InstanceRelation implements IInstanceRelation {
private double probability;
private final IRecommendedInstance fromInstance;
private final IRecommendedInstance toInstance;
private final List<LocalRelation> localRelations;

@Override
public IInstanceRelation createCopy() {
InstanceRelation relation = new InstanceRelation(fromInstance, toInstance, null, null, null);
for (LocalRelation localRelation : localRelations) {
addLink(localRelation.relator, localRelation.from, localRelation.to);
}
return relation;
}

public InstanceRelation(IRecommendedInstance fromInstance, IRecommendedInstance toInstance, IWord relator, List<IWord> from, List<IWord> to) {
this.fromInstance = fromInstance;
this.toInstance = toInstance;
localRelations = new ArrayList<>();
probability = 0;
addLink(relator, from, to);
}

@Override
public boolean addLink(IWord relator, List<IWord> from, List<IWord> to) {
if (relator == null || from == null || to == null) {
return false;
}
for (LocalRelation relation : localRelations) {
if (relation.from.size() == from.size() && relation.from.containsAll(from) && relation.to.size() == to.size() && relation.to.containsAll(to)) {
return false;
}
}
localRelations.add(new LocalRelation(relator, from, to));
increaseProbability();
return true;
}

private void increaseProbability() {
probability += Math.pow(0.5, localRelations.size());
}

@Override
public boolean matches(IRecommendedInstance fromInstance, IRecommendedInstance toInstance) {
return this.fromInstance.equals(fromInstance) && this.toInstance.equals(toInstance);
}

@Override
public boolean isIn(IWord relator, List<IWord> from, List<IWord> to) {
for (LocalRelation relation : localRelations) {
var sizesAreEqual = relation.from.size() == from.size() && relation.to.size() == to.size();
var containsEqual = relation.from.containsAll(from) && relation.to.containsAll(to);
if (relation.relator.equals(relator) && containsEqual && sizesAreEqual) {
return true;
}
}
return false;
}

@Override
public double getProbability() {
return probability;
}

@Override
public void setProbability(double newProbability) {
probability = newProbability;
}

@Override
public IRecommendedInstance getFromInstance() {
return fromInstance;
}

@Override
public IRecommendedInstance getToInstance() {
return toInstance;
}

@Override
public int hashCode() {
return Objects.hash(fromInstance, toInstance);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
InstanceRelation other = (InstanceRelation) obj;
return Objects.equals(fromInstance, other.fromInstance) && Objects.equals(toInstance, other.toInstance);
}

@Override
public String toString() {
StringBuilder str = new StringBuilder("");
str.append("InstanceRelation{")
.append("probability=")
.append(probability)
.append(", fromInstance=")
.append(fromInstance.toString())
.append(", ")
.append("toInstance=")
.append(toInstance.toString())
.append(", ")
.append("localRelations=");
for (LocalRelation relation : localRelations) {
str.append(relation.toString()).append(", ");
}
str.delete(str.length() - 3, str.length() - 1);
str.append('}');
return str.toString();
}

private class LocalRelation {
IWord relator;
List<IWord> from;
List<IWord> to;

LocalRelation(IWord relator, List<IWord> from, List<IWord> to) {
this.relator = relator;
this.from = from;
this.to = to;
}

@Override
public String toString() {
StringBuilder str = new StringBuilder("Link{from=");
for (IWord fromWord : from) {
str.append(fromWord.getText()).append(", ");
}
str.deleteCharAt(str.length() - 1);
str.append("->to=");
for (IWord toWord : to) {
str.append(toWord.getText()).append(", ");
}
str.deleteCharAt(str.length() - 1);
str.append('}');
return str.toString();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package edu.kit.kastel.mcse.ardoco.core.datastructures;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;

import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.eclipse.collections.api.list.MutableList;

import edu.kit.kastel.mcse.ardoco.core.datastructures.common.SimilarityUtils;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.INounMapping;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IRecommendationState;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IRecommendedInstance;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IRecommendedRelation;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.IWord;
import edu.kit.kastel.mcse.ardoco.core.datastructures.definitions.*;

/**
* The recommendation state encapsulates all recommended instances and relations. These recommendations should be
Expand All @@ -25,12 +22,14 @@ public class RecommendationState implements IRecommendationState {

private MutableList<IRecommendedInstance> recommendedInstances;
private MutableList<IRecommendedRelation> recommendedRelations;
private MutableList<IInstanceRelation> instanceRelations;

@Override
public IRecommendationState createCopy() {
var recommendationState = new RecommendationState();
recommendationState.recommendedInstances = recommendedInstances.collect(IRecommendedInstance::createCopy);
recommendationState.recommendedRelations = recommendedRelations.collect(IRecommendedRelation::createCopy);
recommendationState.instanceRelations = instanceRelations.collect(IInstanceRelation::createCopy);
return recommendationState;
}

Expand All @@ -40,6 +39,7 @@ public IRecommendationState createCopy() {
public RecommendationState() {
recommendedInstances = Lists.mutable.empty();
recommendedRelations = Lists.mutable.empty();
instanceRelations = Lists.mutable.empty();
}

/**
Expand All @@ -62,6 +62,33 @@ public ImmutableList<IRecommendedRelation> getRecommendedRelations() {
return recommendedRelations.toImmutable();
}

/**
* Returns all instance relations.
*
* @return all instance relations as list
*/
@Override
public ImmutableList<IInstanceRelation> getInstanceRelations() {
return instanceRelations.toImmutable();
}

/**
* Adds a new instance relation.
*
* @param fromInstance source instances of the instance relation
* @param toInstance target instances of the instance relation
* @param relator relating word
* @param from source nodes of the instance relation
* @param to target nodes of the instance relation
*/
public void addInstanceRelation(IRecommendedInstance fromInstance,
IRecommendedInstance toInstance,
IWord relator,
List<IWord> from,
List<IWord> to) {
this.instanceRelations.add(new InstanceRelation(fromInstance, toInstance, relator, from, to));
}

/**
* Adds a new recommended relation.
*
Expand Down
Loading

0 comments on commit 1991d75

Please sign in to comment.