Search in sources :

Example 1 with DenseVector

use of com.graphaware.nlp.vector.DenseVector 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)

Aggregations

Tag (com.graphaware.nlp.domain.Tag)1 TextProcessor (com.graphaware.nlp.processor.TextProcessor)1 DenseVector (com.graphaware.nlp.vector.DenseVector)1 VectorHandler (com.graphaware.nlp.vector.VectorHandler)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Node (org.neo4j.graphdb.Node)1 QueryExecutionException (org.neo4j.graphdb.QueryExecutionException)1