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());
}
}
}
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;
}
}
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);
}
}
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);
}
Aggregations