package com.tzld.piaoquan.recommend.server.util;

import com.tzld.piaoquan.recommend.similarity.word2vec.Segment;
import com.tzld.piaoquan.recommend.similarity.word2vec.Word2Vec;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author dyp
 */
@Slf4j
public final class SimilarityUtils {

    private static Word2Vec vec = new Word2Vec();

    private static final AtomicBoolean modelLoaded = new AtomicBoolean(false);
    private static final AtomicBoolean init = new AtomicBoolean(false);

    private static final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    public static void init() {
        if (init.compareAndSet(false, true)) {
            Segment.getWords("1");
            scheduler.scheduleAtFixedRate(() -> {
                try {
                    long start = System.currentTimeMillis();
                    String endpoint = PropertiesUtil.getString("oss.endpoint");
                    String bucketName = "art-recommend";
                    String path = "similarity/word2vec/Google_word2vec_zhwiki210720_300d.bin";
                    String accessKeyId = "LTAIP6x1l3DXfSxm";
                    String accessKetSecret = "KbTaM9ars4OX3PMS6Xm7rtxGr1FLon";
                    Word2Vec temp = new Word2Vec();
                    temp.loadGoogleModelFromOss(endpoint, bucketName, path, accessKeyId, accessKetSecret);
                    vec = temp;
                    long end = System.currentTimeMillis();

                    if (modelLoaded.compareAndSet(false, true)) {
                        scheduler.shutdown();
                        log.info("Model loaded successfully cost {}. Scheduled tasks cancelled.", end - start);
                    }

                } catch (IOException e) {
                    log.error("loadGoogleModelFromOss error", e);
                }
            }, 0, 5, TimeUnit.MINUTES);
        }
    }


    public static float word2VecSimilarity(String str1, String str2) {
        List<String> words1 = Segment.getWords(str1);
        List<String> words2 = Segment.getWords(str2);
        return vec.sentenceSimilarity(words1, words2);
    }


}
