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

com.fnklabs.draenei.analytics.CacheableTextUtils Maven / Gradle / Ivy

There is a newer version: 0.8.3
Show newest version
package com.fnklabs.draenei.analytics;

import com.codahale.metrics.Timer;
import com.fnklabs.draenei.MetricsFactory;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Set;

/**
 * Cacheable version of {@link TextUtils}
 */
class CacheableTextUtils extends TextUtils {

    private final MetricsFactory metricsFactory;

    private final LoadingCache IS_NORMAL_WORD_CACHE = CacheBuilder.newBuilder()
                                                                                   .maximumSize(50000)
            .build(new CacheLoader() {
                @Override
                public Boolean load(@NotNull String key) throws Exception {
                    return CacheableTextUtils.super.isNormalWord(key, MorphologyFactory.Language.RU);
                }
            });

    private final LoadingCache> NORMAL_FORMS_OF_WORD_CACHE = CacheBuilder.newBuilder()
                                                                                             .maximumSize(50000)
            .>build(new CacheLoader>() {
                @Override
                public Set load(@NotNull String key) throws Exception {
                    return CacheableTextUtils.super.getNormalForms(key, MorphologyFactory.Language.RU);
                }
            });

    private final LoadingCache IS_STOP_WORD = CacheBuilder.newBuilder()
                                                                           .maximumSize(10000)
            .build(new CacheLoader() {
                @Override
                public Boolean load(@NotNull String key) throws Exception {
                    return CacheableTextUtils.super.isStopWord(key);
                }
            });

    public CacheableTextUtils(StopWordsDao stopWordsDao, MetricsFactory metricsFactory, MorphologyFactory morphologyFactory) throws IOException {
        super(morphologyFactory, stopWordsDao, metricsFactory);
        this.metricsFactory = metricsFactory;
    }

    @Override
    public Set getNormalForms(String word, MorphologyFactory.Language language) {
        Timer.Context timer = getMetricsFactory().getTimer(MetricsType.CACHEABLE_TEXT_UTILS_GET_NORMAL_FORMS).time();
        Set normalFormsOfWord = NORMAL_FORMS_OF_WORD_CACHE.getUnchecked(word);
        timer.stop();

        return normalFormsOfWord;
    }

    @Override
    protected boolean isStopWord(@NotNull String token) {
        Timer.Context timer = getMetricsFactory().getTimer(MetricsType.CACHEABLE_TEXT_UTILS_IS_STOP_WORD).time();

        boolean isStopWord = IS_STOP_WORD.getUnchecked(token);

        timer.stop();

        return isStopWord;
    }

    @Override
    protected boolean isNormalWord(String word, MorphologyFactory.Language language) {
        Timer.Context timer = getMetricsFactory().getTimer(MetricsType.CACHEABLE_TEXT_UTILS_IS_NORMAL_WORD).time();

        Boolean result = IS_NORMAL_WORD_CACHE.getUnchecked(word);
        timer.stop();

        return result;
    }

    private MetricsFactory getMetricsFactory() {
        return metricsFactory;
    }

    private enum MetricsType implements MetricsFactory.Type {
        CACHEABLE_TEXT_UTILS_GET_NORMAL_FORMS,
        CACHEABLE_TEXT_UTILS_IS_STOP_WORD,
        CACHEABLE_TEXT_UTILS_IS_NORMAL_WORD
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy