package examples.utils;

import com.tzld.piaoquan.recommend.similarity.word2vec.Segment;
import com.tzld.piaoquan.recommend.similarity.word2vec.Word2Vec;
import lombok.extern.slf4j.Slf4j;
import org.ansj.library.AmbiguityLibrary;
import org.ansj.library.DicLibrary;
import org.ansj.library.SynonymsLibrary;

import java.io.IOException;
import java.util.List;

/**
 * @author dyp
 * 从recommend-server复制过来的 @zhangbo 20241211
 */
@Slf4j
public final class SimilarityUtils {

    private static Word2Vec vec;

    public static void init() {
        initLibrary();
        Segment.getWords("1");
        try {
            long start = System.currentTimeMillis();
            String endpoint = "oss-cn-hangzhou-internal.aliyuncs.com";
            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();
            log.info("Model loaded successfully cost {}. Scheduled tasks cancelled.", end - start);
        } catch (IOException e) {
            log.error("loadGoogleModelFromOss error", e);
        }
    }

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

    public static void initLibrary() {
        AmbiguityLibrary.put(AmbiguityLibrary.DEFAULT, "jar://library/ambiguity.dic");
        AmbiguityLibrary.reload(AmbiguityLibrary.DEFAULT);
        DicLibrary.put(DicLibrary.DEFAULT, "jar://library/default.dic");
        DicLibrary.reload(DicLibrary.DEFAULT);
        SynonymsLibrary.put(SynonymsLibrary.DEFAULT, "jar://library/synonyms.dic");
        SynonymsLibrary.reload(SynonymsLibrary.DEFAULT);
    }
}
