<commit_before>/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2016 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.lenskit.data.ratings;

import it.unimi.dsi.fastutil.longs.Long2IntMap;
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongSortedSet;
import org.grouplens.grapht.annotation.DefaultProvider;
import org.lenskit.data.dao.DataAccessObject;
import org.lenskit.data.entities.CommonAttributes;
import org.lenskit.data.entities.CommonTypes;
import org.lenskit.data.entities.Entity;
import org.lenskit.data.entities.EntityType;
import org.lenskit.inject.Shareable;
import org.lenskit.inject.Transient;
import org.lenskit.util.io.ObjectStream;
import org.lenskit.util.keys.SortedKeyIndex;

import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import javax.inject.Provider;
import java.io.Serializable;

/**
 * Interaction statistics, counting the number of times an item has been interacted with.
 * The interaction entity must have {@link CommonAttributes#ITEM_ID} attributes.
 */
@Shareable
@Immutable
@DefaultProvider(InteractionStatistics.ISProvider.class)
public class InteractionStatistics implements Serializable {
    private static final long serialVersionUID = 1L;

    private final EntityType entityType;
    private final SortedKeyIndex items;
    private final int[] interactionCounts;

    /**
     * Construct a new interaction statistics object.
     *
     * @param type The counted entity type.
     * @param counts A map of item interaction counts.
     */
    public InteractionStatistics(EntityType type, Long2IntMap counts) {
        entityType = type;
        items = SortedKeyIndex.fromCollection(counts.keySet());
        int n = items.size();
        interactionCounts = new int[n];
        for (int i = 0; i < n; i++) {
            interactionCounts[i] = counts.get(items.getKey(i));
        }
    }

    /**
     * Construct a new interaction statistics object.
     * @param dao The DAO.
     * @param type The entity type to count.
     * @return The statistics.
     */
    public static InteractionStatistics create(DataAccessObject dao, EntityType type) {
        return new ISProvider(type, dao).get();
    }

    /**
     * Construct a new interaction statistics object that counts ratings.
     * @param dao The DAO.
     * @return The rating statistics.
     */
    public static InteractionStatistics create(DataAccessObject dao) {
        return create(dao, CommonTypes.RATING);
    }

    /**
     * Get the interaction entity type.
     * @return The type of entities counted for this statistics object.
     */
    public EntityType getEntityType() {
        return entityType;
    }

    /**
     * Get the number of interactions for an item.
     * @param item The item.
     * @return The number of interactions for `item`.
     */
    public int getInteractionCount(long item) {
        int idx = items.tryGetIndex(item);
        if (idx >= 0) {
            return interactionCounts[idx];
        } else {
            return 0;
        }
    }

    /**
     * Get the set of known items.
     * @return The set of known items.
     */
    public LongSortedSet getKnownItems() {
        return items.keySet();
    }

    /**
     * Provider that counts item interactions.
     */
    public static class ISProvider implements Provider<InteractionStatistics> {
        private final EntityType entityType;
        private final DataAccessObject dao;

        /**
         * Construct the provider.
         * @param type The entity type. It should have {@link CommonAttributes#ITEM_ID} attributes.
         * @param dao The data access object.
         */
        @Inject
        public ISProvider(@InteractionEntityType EntityType type,
                          @Transient DataAccessObject dao) {
            entityType = type;
            this.dao = dao;
        }

        @Override
        public InteractionStatistics get() {
            Long2IntOpenHashMap counts = new Long2IntOpenHashMap();

            try (ObjectStream<Entity> stream = dao.query(entityType).stream()) {
                for (Entity e : stream) {
                    long item = e.getLong(CommonAttributes.ITEM_ID);
                    counts.addTo(item, 1);
                }
            }

            return new InteractionStatistics(entityType, counts);
        }
    }
}<commit_msg>Add a count-sum interaction statistic provider<commit_after>/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2016 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.lenskit.data.ratings;

import it.unimi.dsi.fastutil.longs.Long2IntMap;
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongSortedSet;
import org.grouplens.grapht.annotation.DefaultProvider;
import org.lenskit.data.dao.DataAccessObject;
import org.lenskit.data.entities.CommonAttributes;
import org.lenskit.data.entities.CommonTypes;
import org.lenskit.data.entities.Entity;
import org.lenskit.data.entities.EntityType;
import org.lenskit.inject.Shareable;
import org.lenskit.inject.Transient;
import org.lenskit.util.io.ObjectStream;
import org.lenskit.util.keys.SortedKeyIndex;

import javax.annotation.concurrent.Immutable;
import javax.inject.Inject;
import javax.inject.Provider;
import java.io.Serializable;

/**
 * Interaction statistics, counting the number of times an item has been interacted with.
 * The interaction entity must have {@link CommonAttributes#ITEM_ID} attributes.
 */
@Shareable
@Immutable
@DefaultProvider(InteractionStatistics.ISProvider.class)
public class InteractionStatistics implements Serializable {
    private static final long serialVersionUID = 1L;

    private final EntityType entityType;
    private final SortedKeyIndex items;
    private final int[] interactionCounts;

    /**
     * Construct a new interaction statistics object.
     *
     * @param type The counted entity type.
     * @param counts A map of item interaction counts.
     */
    public InteractionStatistics(EntityType type, Long2IntMap counts) {
        entityType = type;
        items = SortedKeyIndex.fromCollection(counts.keySet());
        int n = items.size();
        interactionCounts = new int[n];
        for (int i = 0; i < n; i++) {
            interactionCounts[i] = counts.get(items.getKey(i));
        }
    }

    /**
     * Construct a new interaction statistics object.
     * @param dao The DAO.
     * @param type The entity type to count.
     * @return The statistics.
     */
    public static InteractionStatistics create(DataAccessObject dao, EntityType type) {
        return new ISProvider(type, dao).get();
    }

    /**
     * Construct a new interaction statistics object that counts ratings.
     * @param dao The DAO.
     * @return The rating statistics.
     */
    public static InteractionStatistics create(DataAccessObject dao) {
        return create(dao, CommonTypes.RATING);
    }

    /**
     * Get the interaction entity type.
     * @return The type of entities counted for this statistics object.
     */
    public EntityType getEntityType() {
        return entityType;
    }

    /**
     * Get the number of interactions for an item.
     * @param item The item.
     * @return The number of interactions for `item`.
     */
    public int getInteractionCount(long item) {
        int idx = items.tryGetIndex(item);
        if (idx >= 0) {
            return interactionCounts[idx];
        } else {
            return 0;
        }
    }

    /**
     * Get the set of known items.
     * @return The set of known items.
     */
    public LongSortedSet getKnownItems() {
        return items.keySet();
    }

    /**
     * Provider that counts item interactions.
     */
    public static class ISProvider implements Provider<InteractionStatistics> {
        private final EntityType entityType;
        private final DataAccessObject dao;

        /**
         * Construct the provider.
         * @param type The entity type. It should have {@link CommonAttributes#ITEM_ID} attributes.
         * @param dao The data access object.
         */
        @Inject
        public ISProvider(@InteractionEntityType EntityType type,
                          @Transient DataAccessObject dao) {
            entityType = type;
            this.dao = dao;
        }

        @Override
        public InteractionStatistics get() {
            Long2IntOpenHashMap counts = new Long2IntOpenHashMap();

            try (ObjectStream<Entity> stream = dao.query(entityType).stream()) {
                for (Entity e : stream) {
                    long item = e.getLong(CommonAttributes.ITEM_ID);
                    counts.addTo(item, 1);
                }
            }

            return new InteractionStatistics(entityType, counts);
        }
    }

    /**
     * Provider that counts item interactions.
     */
    public static class CountSumISProvider implements Provider<InteractionStatistics> {
        private final EntityType entityType;
        private final DataAccessObject dao;

        /**
         * Construct the provider.
         * @param type The entity type. It should have {@link CommonAttributes#ITEM_ID} attributes.
         * @param dao The data access object.
         */
        @Inject
        public CountSumISProvider(@InteractionEntityType EntityType type,
                                  @Transient DataAccessObject dao) {
            entityType = type;
            this.dao = dao;
        }

        @Override
        public InteractionStatistics get() {
            Long2IntOpenHashMap counts = new Long2IntOpenHashMap();

            try (ObjectStream<Entity> stream = dao.query(entityType).stream()) {
                for (Entity e : stream) {
                    long item = e.getLong(CommonAttributes.ITEM_ID);
                    counts.addTo(item, e.getInteger(CommonAttributes.COUNT));
                }
            }

            return new InteractionStatistics(entityType, counts);
        }
    }
}
{'fraction_non_alphanumeric': 0.08200474898236092, 'fraction_numerical': 0.0101763907734057, 'mean_word_length': 2.596523330283623, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4267134', 'n_tokens_mistral': 3219, 'n_tokens_neox': 3081, 'n_words': 1035}