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