Search in sources :

Example 1 with VectorHandler

use of com.graphaware.nlp.vector.VectorHandler in project neo4j-nlp by graphaware.

the class TagPersister method storeExtraProperties.

private void storeExtraProperties(Tag tag, Node tagNode) {
    for (Map.Entry<String, Object> entry : tag.getExtraProperties().entrySet()) {
        if (entry.getValue() instanceof VectorHandler) {
            VectorPersister persister = NLPManager.getInstance().getPersister(VectorContainer.class);
            VectorHandler vectorHandler = (VectorHandler) entry.getValue();
            persister.storeVector(tagNode, entry.getKey(), vectorHandler.getType(), vectorHandler.getArray(), Optional.empty());
        } else {
            tagNode.setProperty(entry.getKey(), entry.getValue());
        }
    }
}
Also used : VectorHandler(com.graphaware.nlp.vector.VectorHandler)

Example 2 with VectorHandler

use of com.graphaware.nlp.vector.VectorHandler in project neo4j-nlp by graphaware.

the class NLPManager method computeVectorAndPersist.

public Node computeVectorAndPersist(ComputeVectorRequest request) {
    try {
        VectorComputation vectorComputation = vectorComputationProcesses.get(request.getType());
        if (vectorComputation == null) {
            throw new RuntimeException("Cannot find the VectorComputation instance with type: " + request.getType());
        }
        VectorHandler vector = vectorComputation.computeSparseVector(request.getInput().getId(), request.getParameters());
        if (vector != null) {
            VectorContainer vectorNode = new VectorContainer(request.getInput().getId(), request.getPropertyName(), vector);
            getPersister(vectorNode.getClass()).persist(vectorNode, request.getLabel(), null);
        }
        return request.getInput();
    } catch (Exception ex) {
        LOG.error("Error in computeVectorAndPersist", ex);
        throw ex;
    }
}
Also used : VectorComputation(com.graphaware.nlp.vector.VectorComputation) NotSupportedException(javax.ws.rs.NotSupportedException) MethodNotSupportedException(org.apache.http.MethodNotSupportedException) VectorHandler(com.graphaware.nlp.vector.VectorHandler) VectorContainer(com.graphaware.nlp.domain.VectorContainer)

Example 3 with VectorHandler

use of com.graphaware.nlp.vector.VectorHandler in project neo4j-nlp by graphaware.

the class Word2VecProcessor method attach.

public int attach(Word2VecRequest request) {
    try {
        Iterator<Node> tagsIterator;
        if (request.getAnnotatedNode() != null) {
            tagsIterator = getAnnotatedTextTags(request.getAnnotatedNode());
        } else if (request.getTagNode() != null) {
            List<Node> proc = new ArrayList<>();
            proc.add(request.getTagNode());
            tagsIterator = proc.iterator();
        } else if (request.getQuery() != null) {
            tagsIterator = getByQuery(request.getQuery());
        } else {
            throw new RuntimeException("You need to specify or an annotated text " + "or a tag " + "or a query");
        }
        TextProcessor processor = getProcessor(request.getProcessor());
        List<Tag> tags = new ArrayList<>();
        while (tagsIterator.hasNext()) {
            Tag tag = (Tag) getPersister(Tag.class).fromNode(tagsIterator.next());
            if (request.getSplitTags()) {
                List<Tag> annotateTags = processor.annotateTags(tag.getLemma(), request.getLang());
                if (annotateTags.size() == 1 && annotateTags.get(0).getLemma().equalsIgnoreCase(tag.getLemma())) {
                    tags.add(tag);
                } else {
                    annotateTags.forEach((newTag) -> {
                        tags.add(newTag);
                        tag.addParent(RELATIONSHIP_IS_RELATED_TO_SUB_TAG, newTag, 0.0f);
                    });
                }
            } else {
                tags.add(tag);
            }
        }
        List<Tag> extendedTags = new ArrayList<>();
        tags.stream().forEach((tag) -> {
            LOG.info("Searching for: " + tag.getLemma().toLowerCase());
            float[] vector = word2VecModel.getWordToVec(tag.getLemma().toLowerCase(), request.getModelName());
            if (vector != null) {
                VectorHandler vectorHandler = new VectorHandler(new DenseVector(vector));
                tag.addProperties(request.getPropertyName(), vectorHandler);
                extendedTags.add(tag);
            }
        });
        AtomicInteger affectedTag = new AtomicInteger(0);
        extendedTags.stream().forEach((newTag) -> {
            if (newTag != null) {
                getPersister(Tag.class).getOrCreate(newTag, newTag.getId(), String.valueOf(System.currentTimeMillis()));
                affectedTag.incrementAndGet();
            }
        });
        return affectedTag.get();
    } catch (Exception ex) {
        LOG.error("Error!!!! ", ex);
        throw new RuntimeException("Error", ex);
    }
}
Also used : TextProcessor(com.graphaware.nlp.processor.TextProcessor) Node(org.neo4j.graphdb.Node) IOException(java.io.IOException) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) VectorHandler(com.graphaware.nlp.vector.VectorHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tag(com.graphaware.nlp.domain.Tag) DenseVector(com.graphaware.nlp.vector.DenseVector)

Example 4 with VectorHandler

use of com.graphaware.nlp.vector.VectorHandler in project neo4j-nlp by graphaware.

the class VectorPersister method fromNode.

@Override
public VectorContainer fromNode(Node node, Object... properties) {
    String basePropertyname = (String) properties[0];
    String type = (String) node.getProperty(getTypePropertyName(basePropertyname));
    float[] vector = (float[]) node.getProperty(getArrayPropertyName(basePropertyname));
    VectorHandler createVector = VectorFactory.createVector(type, vector);
    return new VectorContainer(node.getId(), basePropertyname, createVector);
}
Also used : VectorHandler(com.graphaware.nlp.vector.VectorHandler) VectorContainer(com.graphaware.nlp.domain.VectorContainer)

Aggregations

VectorHandler (com.graphaware.nlp.vector.VectorHandler)4 VectorContainer (com.graphaware.nlp.domain.VectorContainer)2 Tag (com.graphaware.nlp.domain.Tag)1 TextProcessor (com.graphaware.nlp.processor.TextProcessor)1 DenseVector (com.graphaware.nlp.vector.DenseVector)1 VectorComputation (com.graphaware.nlp.vector.VectorComputation)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NotSupportedException (javax.ws.rs.NotSupportedException)1 MethodNotSupportedException (org.apache.http.MethodNotSupportedException)1 Node (org.neo4j.graphdb.Node)1 QueryExecutionException (org.neo4j.graphdb.QueryExecutionException)1