Search in sources :

Example 1 with Word2VecModel

use of hex.word2vec.Word2VecModel in project h2o-3 by h2oai.

the class Word2VecHandler method findSynonyms.

public Word2VecSynonymsV3 findSynonyms(int version, Word2VecSynonymsV3 args) {
    Word2VecModel model = DKV.getGet(args.model.key());
    if (model == null)
        throw new IllegalArgumentException("missing source model " + args.model);
    Map<String, Float> synonyms = model.findSynonyms(args.word, args.count);
    List<Map.Entry<String, Float>> result = new ArrayList<>(synonyms.entrySet());
    Collections.sort(result, new Comparator<Map.Entry<String, Float>>() {

        @Override
        public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) {
            // reverse sort
            return o2.getValue().compareTo(o1.getValue());
        }
    });
    args.synonyms = new String[result.size()];
    args.scores = new double[result.size()];
    int i = 0;
    for (Map.Entry<String, Float> entry : result) {
        args.synonyms[i] = entry.getKey();
        args.scores[i] = entry.getValue();
        i++;
    }
    return args;
}
Also used : Word2VecModel(hex.word2vec.Word2VecModel)

Example 2 with Word2VecModel

use of hex.word2vec.Word2VecModel in project h2o-3 by h2oai.

the class Word2VecHandler method transform.

public Word2VecTransformV3 transform(int version, Word2VecTransformV3 args) {
    Word2VecModel model = DKV.getGet(args.model.key());
    if (model == null)
        throw new IllegalArgumentException("missing source model " + args.model);
    Frame words = DKV.getGet(args.words_frame.key());
    if (words == null)
        throw new IllegalArgumentException("missing words frame " + args.words_frame);
    if (words.numCols() != 1) {
        throw new IllegalArgumentException("words frame is expected to have a single string column, got" + words.numCols());
    }
    if (args.aggregate_method == null)
        args.aggregate_method = Word2VecModel.AggregateMethod.NONE;
    Frame vectors = model.transform(words.vec(0), args.aggregate_method);
    args.vectors_frame = new KeyV3.FrameKeyV3(vectors._key);
    return args;
}
Also used : Frame(water.fvec.Frame) Word2VecModel(hex.word2vec.Word2VecModel) KeyV3(water.api.schemas3.KeyV3)

Aggregations

Word2VecModel (hex.word2vec.Word2VecModel)2 KeyV3 (water.api.schemas3.KeyV3)1 Frame (water.fvec.Frame)1