Java Code Examples for com.jstarcraft.core.utility.KeyValue#getValue()

The following examples show how to use com.jstarcraft.core.utility.KeyValue#getValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MovieService.java    From jstarcraft-example with Apache License 2.0 6 votes vote down vote up
/**
 * 个性化搜索
 * 
 * @param userIndex
 * @param searchKey
 * @return
 * @throws Exception
 */
@LockableMethod(strategy = HashLockableStrategy.class)
public Object2FloatMap<MovieItem> getSearchItems(@LockableParameter int userIndex, String searchKey) throws Exception {
    // 标识-得分映射
    Object2FloatMap<MovieItem> item2ScoreMap = new Object2FloatOpenHashMap<>();

    long current = System.currentTimeMillis();
    Query query = queryParser.parse(searchKey, MovieItem.TITLE);
    KeyValue<List<Document>, FloatList> search = engine.retrieveDocuments(query, null, 0, 1000);
    List<Document> documents = search.getKey();
    FloatList scores = search.getValue();
    for (int index = 0, size = documents.size(); index < size; index++) {
        Document document = documents.get(index);
        MovieItem item = items.get(document.getField(MovieItem.INDEX).numericValue().intValue());
        float score = scores.getFloat(index);
        item2ScoreMap.put(item, score);
    }
    String message = StringUtility.format("搜索数量:{},搜索耗时:{}", documents.size(), System.currentTimeMillis() - current);
    logger.info(message);

    return item2ScoreMap;
}
 
Example 2
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> void iterateUnion(StorageIterator<T> iterator, Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.SHOULD);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	engine.iterateDocuments((document) -> {
		iterator.iterate((T) metadata.decodeDocument(document));
	}, query, null, offset, size);
}
 
Example 3
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> void iterateIntersection(StorageIterator<T> iterator, Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.MUST);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	engine.iterateDocuments((document) -> {
		iterator.iterate((T) metadata.decodeDocument(document));
	}, query, null, offset, size);
}
 
Example 4
Source File: IRRGModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * Compute group-to-item AR and store them into map itemCorrsGAR
 */
private void computeAssociationRuleByGroup(int groupIndex, LinkedList<KeyValue<Integer, Integer>> itemList) {
    List<KeyValue<KeyValue<Integer, Integer>, Float>> coefficientList = new LinkedList<>();

    for (KeyValue<Integer, Integer> keyValue : itemList) {
        int leftIndex = keyValue.getKey();
        int rightIndex = keyValue.getValue();
        SparseVector groupVector = scoreMatrix.getColumnVector(groupIndex);
        int count = 0;
        for (VectorScalar term : groupVector) {
            int userIndex = term.getIndex();
            if (dataTable.contains(userIndex, leftIndex) && dataTable.contains(userIndex, rightIndex)) {
                count++;
            }
        }
        if (count > 0) {
            float shrink = count / (count + reliability);
            int co_bc = itemCount.get(leftIndex, rightIndex);
            float coefficient = shrink * (count + 0F) / co_bc;
            coefficientList.add(new KeyValue<>(keyValue, coefficient));
        }
    }
    itemCorrsGAR.put(groupIndex, new ArrayList<>(coefficientList));
}
 
Example 5
Source File: IRRGModel.java    From jstarcraft-rns with Apache License 2.0 6 votes vote down vote up
/**
 * Order group-to-item AR and store them into map itemCorrsGAR_Sorted
 */
private void sortAssociationRuleByGroup() {
    for (int groupIndex : itemCorrsGAR.keySet()) {
        List<KeyValue<KeyValue<Integer, Integer>, Float>> list = itemCorrsGAR.get(groupIndex);
        if (list.size() > neighborSize) {
            Collections.sort(list, (left, right) -> {
                return right.getValue().compareTo(left.getValue());
            });
            list = list.subList(0, neighborSize);
        }

        HashMatrix groupTable = new HashMatrix(true, itemSize, itemSize, new Long2FloatRBTreeMap());
        for (KeyValue<KeyValue<Integer, Integer>, Float> keyValue : list) {
            int leftItemIndex = keyValue.getKey().getKey();
            int rightItemIndex = keyValue.getKey().getValue();
            float correlation = keyValue.getValue();
            groupTable.setValue(leftItemIndex, rightItemIndex, correlation);
        }
        itemCorrsGAR_Sorted.put(groupIndex, SparseMatrix.valueOf(itemSize, itemSize, groupTable));
    }
}
 
Example 6
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryUnion(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.SHOULD);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example 7
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> List<T> queryIntersection(Class<T> clazz, Map<String, Object> condition, StoragePagination pagination) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.MUST);
	}
	query = buffer.build();
	int offset = pagination == null ? 0 : pagination.getFirst();
	int size = pagination == null ? Integer.MAX_VALUE : pagination.getSize();
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, offset, size);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example 8
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public <K extends Comparable, I, T extends IdentityObject<K>> List<T> queryInstances(Class<T> clazz, String name, StorageCondition<I> condition) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query;
	{
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(name);
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, metadata.getPrimaryName(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), condition.getType(), condition.getValues());
	}
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, Integer.MAX_VALUE);
	List<Document> documents = retrieve.getKey();
	List<T> list = new ArrayList<>(BATCH_SIZE);
	for (Document document : documents) {
		list.add((T) metadata.decodeDocument(document));
	}
	return list;
}
 
Example 9
Source File: AnsjSegmentFactory.java    From jstarcraft-nlp with Apache License 2.0 5 votes vote down vote up
@Override
protected NlpTokenizer<? extends NlpToken> getNlpTokenizer(Map<String, String> configurations) {
    KeyValue<Analysis, List<Recognition>> keyValue = build(configurations);
    Analysis analysis = keyValue.getKey();
    List<Recognition> recognitions = keyValue.getValue();

    AnsjTokenizer tokenizer = new AnsjTokenizer(analysis, recognitions);
    return tokenizer;
}
 
Example 10
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> long countUnion(Class<T> clazz, Map<String, Object> condition) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.SHOULD);
	}
	query = buffer.build();
	return engine.countDocuments(query);
}
 
Example 11
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> long countIntersection(Class<T> clazz, Map<String, Object> condition) {
	LuceneMetadata metadata = metadatas.get(clazz);
	Query query = null;
	BooleanQuery.Builder buffer = new BooleanQuery.Builder();
	for (Entry<String, Object> term : condition.entrySet()) {
		KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(term.getKey());
		Field key = keyValue.getKey();
		IndexConverter value = keyValue.getValue();
		query = value.query(context, term.getKey(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, term.getValue());
		buffer.add(query, Occur.MUST);
	}
	query = buffer.build();
	return engine.countDocuments(query);
}
 
Example 12
Source File: LuceneAccessor.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public <K extends Comparable, T extends IdentityObject<K>> T getInstance(Class<T> clazz, K id) {
	LuceneMetadata metadata = metadatas.get(clazz);
	KeyValue<Field, IndexConverter> keyValue = metadata.getIndexKeyValue(metadata.getPrimaryName());
	Field key = keyValue.getKey();
	IndexConverter value = keyValue.getValue();
	Query query = value.query(context, metadata.getPrimaryName(), key, key.getAnnotation(LuceneIndex.class), key.getGenericType(), ConditionType.Equal, id);
	KeyValue<List<Document>, FloatList> retrieve = engine.retrieveDocuments(query, null, 0, 100);
	List<Document> documents = retrieve.getKey();
	if (documents.size() > 0) {
		return (T) metadata.decodeDocument(documents.get(0));
	} else {
		return null;
	}
}
 
Example 13
Source File: PlusVertex.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public void doBackward() {
    MathMatrix innerError = outputKeyValue.getValue();
    for (KeyValue<MathMatrix, MathMatrix> keyValue : inputKeyValues) {
        MathMatrix outerError = keyValue.getValue();
        if (outerError != null) {
            // TODO 使用累计的方式计算
            // TODO 需要锁机制,否则并发计算会导致Bug
            synchronized (outerError) {
                outerError.addMatrix(innerError, false);
            }
        }
    }
}
 
Example 14
Source File: FMeasureLossFunction.java    From jstarcraft-ai with Apache License 2.0 5 votes vote down vote up
@Override
public float computeScore(MathMatrix tests, MathMatrix trains, MathMatrix masks) {
    KeyValue<Float, Float> keyValue = computeNumeratorWithDenominator(tests, trains, masks);
    float numerator = keyValue.getKey();
    float denominator = keyValue.getValue();
    if (numerator == 0F && denominator == 0F) {
        return 0F;
    }
    return 1F - numerator / denominator;
}
 
Example 15
Source File: AbstractModel.java    From jstarcraft-rns with Apache License 2.0 5 votes vote down vote up
@Override
public void prepare(Configurator configuration, DataModule model, DataSpace space) {
    userField = configuration.getString("data.model.fields.user", "user");
    itemField = configuration.getString("data.model.fields.item", "item");

    userDimension = model.getQualityInner(userField);
    itemDimension = model.getQualityInner(itemField);
    userSize = space.getQualityAttribute(userField).getSize();
    itemSize = space.getQualityAttribute(itemField).getSize();

    DataSplitter splitter = new QualityFeatureDataSplitter(userDimension);
    DataModule[] models = splitter.split(model, userSize);
    DataSorter sorter = new AllFeatureDataSorter();
    for (int index = 0; index < userSize; index++) {
        models[index] = sorter.sort(models[index]);
    }

    HashMatrix dataTable = new HashMatrix(true, userSize, itemSize, new Long2FloatRBTreeMap());
    for (DataInstance instance : model) {
        int rowIndex = instance.getQualityFeature(userDimension);
        int columnIndex = instance.getQualityFeature(itemDimension);
        dataTable.setValue(rowIndex, columnIndex, instance.getQuantityMark());
    }
    scoreMatrix = SparseMatrix.valueOf(userSize, itemSize, dataTable);
    actionSize = scoreMatrix.getElementSize();
    KeyValue<Float, Float> attribute = scoreMatrix.getBoundary(false);
    minimumScore = attribute.getKey();
    maximumScore = attribute.getValue();
    meanScore = scoreMatrix.getSum(false);
    meanScore /= actionSize;
}
 
Example 16
Source File: JiebaSegmentFactory.java    From jstarcraft-nlp with Apache License 2.0 5 votes vote down vote up
@Override
protected NlpTokenizer<? extends NlpToken> getNlpTokenizer(Map<String, String> configurations) {
    KeyValue<JiebaSegmenter, SegMode> keyValue = build(configurations);
    JiebaSegmenter segmenter = keyValue.getKey();
    SegMode mode = keyValue.getValue();

    JiebaTokenizer tokenizer = new JiebaTokenizer(segmenter, mode);
    return tokenizer;
}
 
Example 17
Source File: ShareVertex.java    From jstarcraft-ai with Apache License 2.0 4 votes vote down vote up
@Override
public void doCache(KeyValue<MathMatrix, MathMatrix>... samples) {
    // 检查样本
    if (samples.length == 0) {
        throw new IllegalArgumentException();
    }

    this.inputKeyValues = samples;
    this.outputKeyValue = new KeyValue<>(null, null);
    this.middleKeyValue = new KeyValue<>(null, null);

    inputLocalDatas = new MathMatrix[numberOfShares];
    middleLocalDatas = new MathMatrix[numberOfShares];
    outputLocalDatas = new MathMatrix[numberOfShares];
    outterLocalErrors = new MathMatrix[numberOfShares];
    middleLocalErrors = new MathMatrix[numberOfShares];
    innerLocalErrors = new MathMatrix[numberOfShares];

    for (int shareIndex = 0; shareIndex < numberOfShares; shareIndex++) {
        // 输入部分
        MathMatrix key = inputKeyValues[0].getKey();
        MathMatrix value = inputKeyValues[0].getValue();
        int from = shareIndex * key.getColumnSize() / numberOfShares;
        int to = from + key.getColumnSize() / numberOfShares;

        if (key instanceof ColumnGlobalMatrix) {
            key = ColumnGlobalMatrix.detachOf(ColumnGlobalMatrix.class.cast(key), from, to);
            if (value != null) {
                value = ColumnGlobalMatrix.detachOf(ColumnGlobalMatrix.class.cast(value), from, to);
            }
        } else {
            key = new LocalMatrix(key, from, to, 0, key.getRowSize());
            if (value != null) {
                value = new LocalMatrix(value, from, to, 0, key.getRowSize());
            }
        }

        KeyValue<MathMatrix, MathMatrix> keyValue = new KeyValue<>(key, value);
        layer.doCache(factory, keyValue);

        keyValue = layer.getInputKeyValue();
        inputLocalDatas[shareIndex] = keyValue.getKey();
        outterLocalErrors[shareIndex] = keyValue.getValue();
        keyValue = layer.getMiddleKeyValue();
        middleLocalDatas[shareIndex] = keyValue.getKey();
        middleLocalErrors[shareIndex] = keyValue.getValue();
        keyValue = layer.getOutputKeyValue();
        outputLocalDatas[shareIndex] = keyValue.getKey();
        innerLocalErrors[shareIndex] = keyValue.getValue();
    }

    inputGlobalData = ColumnGlobalMatrix.attachOf(inputLocalDatas);
    middleGlobalData = ColumnGlobalMatrix.attachOf(middleLocalDatas);
    outputGlobalData = ColumnGlobalMatrix.attachOf(outputLocalDatas);
    if (inputKeyValues[0].getValue() != null) {
        outterGlobalError = ColumnGlobalMatrix.attachOf(outterLocalErrors);
    }
    middleGlobalError = ColumnGlobalMatrix.attachOf(middleLocalErrors);
    innerGlobalError = ColumnGlobalMatrix.attachOf(innerLocalErrors);

    // 中间部分
    middleKeyValue.setKey(middleGlobalData);
    middleKeyValue.setValue(middleGlobalError);

    // 输出部分
    outputKeyValue.setKey(outputGlobalData);
    outputKeyValue.setValue(innerGlobalError);

    learner.doCache(layer.getGradients());
    epoch++;
    iteration = 0;
}
 
Example 18
Source File: WBPRModel.java    From jstarcraft-rns with Apache License 2.0 4 votes vote down vote up
@Override
protected void doPractice() {
    for (int epocheIndex = 0; epocheIndex < epocheSize; epocheIndex++) {
        totalError = 0F;
        for (int sampleIndex = 0, sampleTimes = userSize * 100; sampleIndex < sampleTimes; sampleIndex++) {
            // randomly draw (userIdx, posItemIdx, negItemIdx)
            int userIndex, positiveItemIndex, negativeItemIndex = 0;
            List<KeyValue<Integer, Double>> probabilities;
            while (true) {
                userIndex = RandomUtility.randomInteger(userSize);
                SparseVector userVector = scoreMatrix.getRowVector(userIndex);
                if (userVector.getElementSize() == 0) {
                    continue;
                }
                positiveItemIndex = userVector.getIndex(RandomUtility.randomInteger(userVector.getElementSize()));
                // sample j by popularity (probability)
                probabilities = itemProbabilities[userIndex];
                double random = RandomUtility.randomDouble(1D);
                for (KeyValue<Integer, Double> term : probabilities) {
                    if ((random -= term.getValue()) <= 0D) {
                        negativeItemIndex = term.getKey();
                        break;
                    }
                }
                break;
            }

            // update parameters
            float positiveScore = predict(userIndex, positiveItemIndex);
            float negativeScore = predict(userIndex, negativeItemIndex);
            float error = positiveScore - negativeScore;
            float value = (float) -Math.log(LogisticUtility.getValue(error));
            totalError += value;
            value = LogisticUtility.getValue(-error);

            // update bias
            float positiveBias = itemBiases.getValue(positiveItemIndex), negativeBias = itemBiases.getValue(negativeItemIndex);
            itemBiases.shiftValue(positiveItemIndex, learnRatio * (value - biasRegularization * positiveBias));
            itemBiases.shiftValue(negativeItemIndex, learnRatio * (-value - biasRegularization * negativeBias));
            totalError += biasRegularization * (positiveBias * positiveBias + negativeBias * negativeBias);

            // update user/item vectors
            for (int factorIndex = 0; factorIndex < factorSize; factorIndex++) {
                float userFactor = userFactors.getValue(userIndex, factorIndex);
                float positiveItemFactor = itemFactors.getValue(positiveItemIndex, factorIndex);
                float negativeItemFactor = itemFactors.getValue(negativeItemIndex, factorIndex);
                userFactors.shiftValue(userIndex, factorIndex, learnRatio * (value * (positiveItemFactor - negativeItemFactor) - userRegularization * userFactor));
                itemFactors.shiftValue(positiveItemIndex, factorIndex, learnRatio * (value * userFactor - itemRegularization * positiveItemFactor));
                itemFactors.shiftValue(negativeItemIndex, factorIndex, learnRatio * (value * (-userFactor) - itemRegularization * negativeItemFactor));
                totalError += userRegularization * userFactor * userFactor + itemRegularization * positiveItemFactor * positiveItemFactor + itemRegularization * negativeItemFactor * negativeItemFactor;
            }
        }
        if (isConverged(epocheIndex) && isConverged) {
            break;
        }
        isLearned(epocheIndex);
        currentError = totalError;
    }
}