<commit_before>package de.spektrumprojekt.i.ranker;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import de.spektrumprojekt.configuration.ConfigurationDescriptable;
import de.spektrumprojekt.datamodel.user.UserModel;
import de.spektrumprojekt.i.informationextraction.InformationExtractionCommand;
import de.spektrumprojekt.i.informationextraction.InformationExtractionConfiguration;
import de.spektrumprojekt.i.learner.adaptation.UserModelAdapterConfiguration;
import de.spektrumprojekt.i.term.TermVectorSimilarityStrategy;
import de.spektrumprojekt.i.term.TermWeightStrategy;

public class RankerConfiguration implements ConfigurationDescriptable, Cloneable {

    private Set<RankerConfigurationFlag> flags = new HashSet<RankerConfigurationFlag>();

    // the minimum user similarity that must be fullfilled to be eglible to take the score of
    private float minUserSimilarity = 0.5f;

    // the minimum score the cmf feature of a similar user must have such that it will be used.
    private float minContentMessageScore = 0.5f;

    // the message rank threshold determines for which users the adaption will take place.
    private float messageRankThreshold = 0.5f;

    private float nonParticipationFactor = 1f;
    private int minimumCleanTextLengthForInvokingLearner;

    private TermWeightStrategy termWeightStrategy;

    private TermVectorSimilarityStrategy termVectorSimilarityStrategy;
    private boolean immutable;

    private float interestTermTreshold = 0.75f;

    private boolean treatMissingUserModelEntriesAsZero;

    private String termUniquenessLogfile;

    private String userModelType = UserModel.DEFAULT_USER_MODEL_TYPE;

    private final InformationExtractionConfiguration informationExtractionConfiguration;

    private final InformationExtractionCommand<MessageFeatureContext> informationExtractionCommand;

    private final UserModelAdapterConfiguration userModelAdapterConfiguration =
            new UserModelAdapterConfiguration();

    public RankerConfiguration(TermWeightStrategy strategy, TermVectorSimilarityStrategy aggregation) {
        this(strategy, aggregation, null, null, (RankerConfigurationFlag[]) null);
    }

    private RankerConfiguration(
            TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation,
            InformationExtractionCommand<MessageFeatureContext> informationExtractionCommand,
            InformationExtractionConfiguration informationExtractionConfiguration,
            RankerConfigurationFlag... flags) {
        if (termWeightStrategy == null) {
            throw new IllegalStateException("termWeightStrategy cannot be null.");
        }
        if (termWeightAggregation == null) {
            throw new IllegalStateException("termWeightAggregation cannot be null.");
        }
        this.termWeightStrategy = termWeightStrategy;
        this.termVectorSimilarityStrategy = termWeightAggregation;
        if (flags != null) {
            this.flags.addAll(Arrays.asList(flags));
        }
        if (informationExtractionConfiguration == null) {
            informationExtractionConfiguration = new InformationExtractionConfiguration();
        }
        this.informationExtractionConfiguration = informationExtractionConfiguration;
        this.informationExtractionCommand = informationExtractionCommand;
    }

    public RankerConfiguration(TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation,
            InformationExtractionCommand<MessageFeatureContext> informationExtractionCommand,
            RankerConfigurationFlag... flags) {
        this(termWeightStrategy, termWeightAggregation,
                informationExtractionCommand,
                informationExtractionCommand.getInformationExtractionConfiguration(), flags);
    }

    public RankerConfiguration(TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation,
            InformationExtractionConfiguration informationExtractionConfiguration,
            RankerConfigurationFlag... flags) {
        this(termWeightStrategy, termWeightAggregation,
                (InformationExtractionCommand<MessageFeatureContext>) null,
                informationExtractionConfiguration, flags);
    }

    public RankerConfiguration(TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation, RankerConfigurationFlag... flags) {
        this(termWeightStrategy, termWeightAggregation, null, null, flags);
    }

    public void addFlags(RankerConfigurationFlag... flags) {
        assertCanSet();
        this.flags.addAll(Arrays.asList(flags));

        this.informationExtractionConfiguration.setBeMessageGroupSpecific(this
                .hasFlag(RankerConfigurationFlag.USE_MESSAGE_GROUP_SPECIFIC_USER_MODEL));
    }

    private void assert01(float value, String field) {
        assertCanSet();
        if (value < 0 || value > 1) {
            throw new IllegalArgumentException(field + " must be between 0 and 1 but is: " + value);
        }
    }

    private void assertCanSet() {
        if (immutable) {
            throw new IllegalStateException(
                    "cannot change values anymore after calling immutable()");
        }
    }

    public RankerConfiguration cloneMe() {
        RankerConfiguration clone;
        try {
            clone = (RankerConfiguration) this.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        clone.immutable = false;
        clone.flags = new HashSet<RankerConfigurationFlag>(this.getFlags());
        return clone;
    }

    @Override
    public String getConfigurationDescription() {
        return this.getClass().getSimpleName() + " " + this.toString();
    }

    public Collection<RankerConfigurationFlag> getFlags() {
        return Collections.unmodifiableSet(flags);
    }

    public InformationExtractionCommand<MessageFeatureContext> getInformationExtractionCommand() {
        return informationExtractionCommand;
    }

    public InformationExtractionConfiguration getInformationExtractionConfiguration() {
        return informationExtractionConfiguration;
    }

    public float getInterestTermTreshold() {
        return interestTermTreshold;
    }

    public float getMessageRankThreshold() {
        return messageRankThreshold;
    }

    public float getMinContentMessageScore() {
        return minContentMessageScore;
    }

    public int getMinimumCleanTextLengthForInvokingLearner() {
        return minimumCleanTextLengthForInvokingLearner;
    }

    public float getMinUserSimilarity() {
        return minUserSimilarity;
    }

    public float getNonParticipationFactor() {
        return nonParticipationFactor;
    }

    public String getTermUniquenessLogfile() {
        return termUniquenessLogfile;
    }

    public TermVectorSimilarityStrategy getTermVectorSimilarityStrategy() {
        return termVectorSimilarityStrategy;
    }

    public TermWeightStrategy getTermWeightStrategy() {
        return termWeightStrategy;
    }

    public UserModelAdapterConfiguration getUserModelAdapterConfiguration() {
        return userModelAdapterConfiguration;
    }

    public String getUserModelType() {
        return userModelType;
    }

    public boolean hasFlag(RankerConfigurationFlag flag) {
        return this.flags.contains(flag);
    }

    public void immutable() {

        this.immutable = true;
    }

    public boolean isImmutable() {
        return immutable;
    }

    public boolean isTreatMissingUserModelEntriesAsZero() {
        return treatMissingUserModelEntriesAsZero;
    }

    public void setFlags(RankerConfigurationFlag... flags) {
        assertCanSet();
        this.flags.clear();
        if (flags != null) {
            addFlags(flags);
        }
        this.informationExtractionConfiguration.setBeMessageGroupSpecific(this
                .hasFlag(RankerConfigurationFlag.USE_MESSAGE_GROUP_SPECIFIC_USER_MODEL));

    }

    public void setInterestTermTreshold(float interestTermTreshold) {
        assert01(interestTermTreshold, "interestTermTreshold");
        this.interestTermTreshold = interestTermTreshold;
    }

    public void setMessageRankThreshold(float messageRankThreshold) {
        assert01(messageRankThreshold, "messageRankThreshold");
        this.messageRankThreshold = messageRankThreshold;
    }

    public void setMinContentMessageScore(float minContentMessageScore) {
        assert01(minContentMessageScore, "minContentMessageScore");
        this.minContentMessageScore = minContentMessageScore;
    }

    public void setMinimumCleanTextLengthForInvokingLearner(
            int minimumCleanTextLengthForInvokingLearner) {
        this.minimumCleanTextLengthForInvokingLearner = minimumCleanTextLengthForInvokingLearner;
    }

    public void setMinUserSimilarity(float minUserSimilarity) {
        assert01(minUserSimilarity, "minUserSimilarity");
        this.minUserSimilarity = minUserSimilarity;
    }

    public void setNonParticipationFactor(float nonParticipationFactor) {
        this.nonParticipationFactor = nonParticipationFactor;
    }

    public void setTermUniquenessLogfile(String termUniquenessLogfile) {
        // assertCanSet();
        this.termUniquenessLogfile = termUniquenessLogfile;
    }

    public void setTermVectorSimilarityStrategy(
            TermVectorSimilarityStrategy termVectorSimilarityStrategy) {
        assertCanSet();
        if (termVectorSimilarityStrategy == null) {
            throw new IllegalStateException("termVectorSimilarityStrategy cannot be null.");
        }
        this.termVectorSimilarityStrategy = termVectorSimilarityStrategy;
    }

    public void setTermWeightStrategy(TermWeightStrategy termWeightStrategy) {
        assertCanSet();
        if (termWeightStrategy == null) {
            throw new IllegalStateException("termWeightStrategy cannot be null.");
        }
        this.termWeightStrategy = termWeightStrategy;
    }

    public void setTreatMissingUserModelEntriesAsZero(boolean treatMissingUserModelEntriesAsZero) {
        assertCanSet();
        this.treatMissingUserModelEntriesAsZero = treatMissingUserModelEntriesAsZero;
    }

    public void setUserModelType(String userModelType) {
        if (userModelType == null) {
            throw new IllegalArgumentException("userModelType cannot be null.");
        }
        assertCanSet();
        this.userModelType = userModelType;
    }

    @Override
    public String toString() {
        return "RankerConfiguration [flags=" + flags + ", minUserSimilarity=" + minUserSimilarity
                + ", minContentMessageScore=" + minContentMessageScore + ", messageRankThreshold="
                + messageRankThreshold + ", nonParticipationFactor=" + nonParticipationFactor
                + ", minimumCleanTextLengthForInvokingLearner="
                + minimumCleanTextLengthForInvokingLearner + ", termWeightStrategy="
                + termWeightStrategy + ", termVectorSimilarityStrategy="
                + termVectorSimilarityStrategy + ", immutable=" + immutable
                + ", interestTermTreshold=" + interestTermTreshold
                + ", treatMissingUserModelEntriesAsZero=" + treatMissingUserModelEntriesAsZero
                + ", termUniquenessLogfile=" + termUniquenessLogfile + ", userModelType="
                + userModelType + ", informationExtractionConfiguration="
                + informationExtractionConfiguration + ", informationExtractionCommand="
                + informationExtractionCommand + ", userModelAdapterConfiguration="
                + userModelAdapterConfiguration + "]";
    }

}<commit_msg>Commenting<commit_after>package de.spektrumprojekt.i.ranker;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import de.spektrumprojekt.configuration.ConfigurationDescriptable;
import de.spektrumprojekt.datamodel.user.UserModel;
import de.spektrumprojekt.i.informationextraction.InformationExtractionCommand;
import de.spektrumprojekt.i.informationextraction.InformationExtractionConfiguration;
import de.spektrumprojekt.i.learner.adaptation.UserModelAdapterConfiguration;
import de.spektrumprojekt.i.term.TermVectorSimilarityStrategy;
import de.spektrumprojekt.i.term.TermWeightStrategy;

public class RankerConfiguration implements ConfigurationDescriptable, Cloneable {

    private Set<RankerConfigurationFlag> flags = new HashSet<RankerConfigurationFlag>();

    // for adaption the message rank (not the user model): the minimum user similarity that must be
    // fullfilled to be eglible to take the score of
    private float minUserSimilarity = 0.5f;

    // for adaption the message rank (not the user model): the minimum score the cmf feature of a
    // similar user must have such that it will be used.
    private float minContentMessageScore = 0.5f;

    // for adaption the message rank (not the user model): the message rank threshold determines for
    // which users the adaption will take place.
    private float messageRankThreshold = 0.5f;

    private float nonParticipationFactor = 1f;
    private int minimumCleanTextLengthForInvokingLearner;

    private TermWeightStrategy termWeightStrategy;

    private TermVectorSimilarityStrategy termVectorSimilarityStrategy;
    private boolean immutable;

    private float interestTermTreshold = 0.75f;

    private boolean treatMissingUserModelEntriesAsZero;

    private String termUniquenessLogfile;

    private String userModelType = UserModel.DEFAULT_USER_MODEL_TYPE;

    private final InformationExtractionConfiguration informationExtractionConfiguration;

    private final InformationExtractionCommand<MessageFeatureContext> informationExtractionCommand;

    private final UserModelAdapterConfiguration userModelAdapterConfiguration =
            new UserModelAdapterConfiguration();

    public RankerConfiguration(TermWeightStrategy strategy, TermVectorSimilarityStrategy aggregation) {
        this(strategy, aggregation, null, null, (RankerConfigurationFlag[]) null);
    }

    private RankerConfiguration(
            TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation,
            InformationExtractionCommand<MessageFeatureContext> informationExtractionCommand,
            InformationExtractionConfiguration informationExtractionConfiguration,
            RankerConfigurationFlag... flags) {
        if (termWeightStrategy == null) {
            throw new IllegalStateException("termWeightStrategy cannot be null.");
        }
        if (termWeightAggregation == null) {
            throw new IllegalStateException("termWeightAggregation cannot be null.");
        }
        this.termWeightStrategy = termWeightStrategy;
        this.termVectorSimilarityStrategy = termWeightAggregation;
        if (flags != null) {
            this.flags.addAll(Arrays.asList(flags));
        }
        if (informationExtractionConfiguration == null) {
            informationExtractionConfiguration = new InformationExtractionConfiguration();
        }
        this.informationExtractionConfiguration = informationExtractionConfiguration;
        this.informationExtractionCommand = informationExtractionCommand;
    }

    public RankerConfiguration(TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation,
            InformationExtractionCommand<MessageFeatureContext> informationExtractionCommand,
            RankerConfigurationFlag... flags) {
        this(termWeightStrategy, termWeightAggregation,
                informationExtractionCommand,
                informationExtractionCommand.getInformationExtractionConfiguration(), flags);
    }

    public RankerConfiguration(TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation,
            InformationExtractionConfiguration informationExtractionConfiguration,
            RankerConfigurationFlag... flags) {
        this(termWeightStrategy, termWeightAggregation,
                (InformationExtractionCommand<MessageFeatureContext>) null,
                informationExtractionConfiguration, flags);
    }

    public RankerConfiguration(TermWeightStrategy termWeightStrategy,
            TermVectorSimilarityStrategy termWeightAggregation, RankerConfigurationFlag... flags) {
        this(termWeightStrategy, termWeightAggregation, null, null, flags);
    }

    public void addFlags(RankerConfigurationFlag... flags) {
        assertCanSet();
        this.flags.addAll(Arrays.asList(flags));

        this.informationExtractionConfiguration.setBeMessageGroupSpecific(this
                .hasFlag(RankerConfigurationFlag.USE_MESSAGE_GROUP_SPECIFIC_USER_MODEL));
    }

    private void assert01(float value, String field) {
        assertCanSet();
        if (value < 0 || value > 1) {
            throw new IllegalArgumentException(field + " must be between 0 and 1 but is: " + value);
        }
    }

    private void assertCanSet() {
        if (immutable) {
            throw new IllegalStateException(
                    "cannot change values anymore after calling immutable()");
        }
    }

    public RankerConfiguration cloneMe() {
        RankerConfiguration clone;
        try {
            clone = (RankerConfiguration) this.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        clone.immutable = false;
        clone.flags = new HashSet<RankerConfigurationFlag>(this.getFlags());
        return clone;
    }

    @Override
    public String getConfigurationDescription() {
        return this.getClass().getSimpleName() + " " + this.toString();
    }

    public Collection<RankerConfigurationFlag> getFlags() {
        return Collections.unmodifiableSet(flags);
    }

    public InformationExtractionCommand<MessageFeatureContext> getInformationExtractionCommand() {
        return informationExtractionCommand;
    }

    public InformationExtractionConfiguration getInformationExtractionConfiguration() {
        return informationExtractionConfiguration;
    }

    public float getInterestTermTreshold() {
        return interestTermTreshold;
    }

    public float getMessageRankThreshold() {
        return messageRankThreshold;
    }

    public float getMinContentMessageScore() {
        return minContentMessageScore;
    }

    public int getMinimumCleanTextLengthForInvokingLearner() {
        return minimumCleanTextLengthForInvokingLearner;
    }

    public float getMinUserSimilarity() {
        return minUserSimilarity;
    }

    public float getNonParticipationFactor() {
        return nonParticipationFactor;
    }

    public String getTermUniquenessLogfile() {
        return termUniquenessLogfile;
    }

    public TermVectorSimilarityStrategy getTermVectorSimilarityStrategy() {
        return termVectorSimilarityStrategy;
    }

    public TermWeightStrategy getTermWeightStrategy() {
        return termWeightStrategy;
    }

    public UserModelAdapterConfiguration getUserModelAdapterConfiguration() {
        return userModelAdapterConfiguration;
    }

    public String getUserModelType() {
        return userModelType;
    }

    public boolean hasFlag(RankerConfigurationFlag flag) {
        return this.flags.contains(flag);
    }

    public void immutable() {

        this.immutable = true;
    }

    public boolean isImmutable() {
        return immutable;
    }

    public boolean isTreatMissingUserModelEntriesAsZero() {
        return treatMissingUserModelEntriesAsZero;
    }

    public void setFlags(RankerConfigurationFlag... flags) {
        assertCanSet();
        this.flags.clear();
        if (flags != null) {
            addFlags(flags);
        }
        this.informationExtractionConfiguration.setBeMessageGroupSpecific(this
                .hasFlag(RankerConfigurationFlag.USE_MESSAGE_GROUP_SPECIFIC_USER_MODEL));

    }

    public void setInterestTermTreshold(float interestTermTreshold) {
        assert01(interestTermTreshold, "interestTermTreshold");
        this.interestTermTreshold = interestTermTreshold;
    }

    public void setMessageRankThreshold(float messageRankThreshold) {
        assert01(messageRankThreshold, "messageRankThreshold");
        this.messageRankThreshold = messageRankThreshold;
    }

    public void setMinContentMessageScore(float minContentMessageScore) {
        assert01(minContentMessageScore, "minContentMessageScore");
        this.minContentMessageScore = minContentMessageScore;
    }

    public void setMinimumCleanTextLengthForInvokingLearner(
            int minimumCleanTextLengthForInvokingLearner) {
        this.minimumCleanTextLengthForInvokingLearner = minimumCleanTextLengthForInvokingLearner;
    }

    public void setMinUserSimilarity(float minUserSimilarity) {
        assert01(minUserSimilarity, "minUserSimilarity");
        this.minUserSimilarity = minUserSimilarity;
    }

    public void setNonParticipationFactor(float nonParticipationFactor) {
        this.nonParticipationFactor = nonParticipationFactor;
    }

    public void setTermUniquenessLogfile(String termUniquenessLogfile) {
        // assertCanSet();
        this.termUniquenessLogfile = termUniquenessLogfile;
    }

    public void setTermVectorSimilarityStrategy(
            TermVectorSimilarityStrategy termVectorSimilarityStrategy) {
        assertCanSet();
        if (termVectorSimilarityStrategy == null) {
            throw new IllegalStateException("termVectorSimilarityStrategy cannot be null.");
        }
        this.termVectorSimilarityStrategy = termVectorSimilarityStrategy;
    }

    public void setTermWeightStrategy(TermWeightStrategy termWeightStrategy) {
        assertCanSet();
        if (termWeightStrategy == null) {
            throw new IllegalStateException("termWeightStrategy cannot be null.");
        }
        this.termWeightStrategy = termWeightStrategy;
    }

    public void setTreatMissingUserModelEntriesAsZero(boolean treatMissingUserModelEntriesAsZero) {
        assertCanSet();
        this.treatMissingUserModelEntriesAsZero = treatMissingUserModelEntriesAsZero;
    }

    public void setUserModelType(String userModelType) {
        if (userModelType == null) {
            throw new IllegalArgumentException("userModelType cannot be null.");
        }
        assertCanSet();
        this.userModelType = userModelType;
    }

    @Override
    public String toString() {
        return "RankerConfiguration [flags=" + flags + ", minUserSimilarity=" + minUserSimilarity
                + ", minContentMessageScore=" + minContentMessageScore + ", messageRankThreshold="
                + messageRankThreshold + ", nonParticipationFactor=" + nonParticipationFactor
                + ", minimumCleanTextLengthForInvokingLearner="
                + minimumCleanTextLengthForInvokingLearner + ", termWeightStrategy="
                + termWeightStrategy + ", termVectorSimilarityStrategy="
                + termVectorSimilarityStrategy + ", immutable=" + immutable
                + ", interestTermTreshold=" + interestTermTreshold
                + ", treatMissingUserModelEntriesAsZero=" + treatMissingUserModelEntriesAsZero
                + ", termUniquenessLogfile=" + termUniquenessLogfile + ", userModelType="
                + userModelType + ", informationExtractionConfiguration="
                + informationExtractionConfiguration + ", informationExtractionCommand="
                + informationExtractionCommand + ", userModelAdapterConfiguration="
                + userModelAdapterConfiguration + "]";
    }

}
{'fraction_non_alphanumeric': 0.06349271160834592, 'fraction_numerical': 0.0019599036380711283, 'mean_word_length': 3.0752079866888518, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2407924', 'n_tokens_mistral': 6076, 'n_tokens_neox': 5565, 'n_words': 1169}