Search in sources :

Example 46 with Description

use of org.neo4j.procedure.Description in project neo4j-nlp by graphaware.

the class SimilarityProcedure method similarity.

// @Procedure(name = "ga.nlp.ml.similarity.cosine", mode = Mode.WRITE)
// @Description("Compute similarity between Annotated Text")
// public Stream<SingleResult> similarityOld(@Name("input") List<Node> input,
// @Name("depth") Long depth,
// @Name("query") String query,
// @Name("relationshipType") String relationshipType) {
// SimilarityProcessor similarityProcessor = (SimilarityProcessor) getNLPManager().getExtension(SimilarityProcessor.class);
// int processed = similarityProcessor.compute(input, query, relationshipType, depth);
// return Stream.of(new SingleResult(processed));
// }
@Procedure(name = "ga.nlp.ml.similarity.cosine", mode = Mode.WRITE)
@Description("Compute similarity between Annotated Text")
public Stream<SingleResult> similarity(@Name("similarityRequest") Map<String, Object> similarityRequest) {
    try {
        SimilarityRequest request = SimilarityRequest.fromMap(similarityRequest);
        SimilarityProcessor similarityProcessor = (SimilarityProcessor) getNLPManager().getExtension(SimilarityProcessor.class);
        int processed = similarityProcessor.compute(request);
        return Stream.of(new SingleResult(processed));
    } catch (Exception ex) {
        LOG.error("Error while computing similarity", ex);
        throw new RuntimeException(ex);
    }
}
Also used : SingleResult(com.graphaware.nlp.dsl.result.SingleResult) SimilarityRequest(com.graphaware.nlp.dsl.request.SimilarityRequest) SimilarityProcessor(com.graphaware.nlp.ml.similarity.SimilarityProcessor) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 47 with Description

use of org.neo4j.procedure.Description in project neo4j-nlp by graphaware.

the class TextProcessorsProcedure method setDefaultPipeline.

@Procedure(value = "ga.nlp.processor.pipeline.default", mode = Mode.WRITE)
@Description("Specify the pipeline to be used by default")
public Stream<SingleResult> setDefaultPipeline(@Name("name") String name) {
    PipelineSpecification pipelineSpecification = getConfiguration().loadPipeline(name);
    if (null == pipelineSpecification) {
        throw new RuntimeException("Pipeline " + name + " does not exist");
    }
    getConfiguration().updateInternalSetting(SettingsConstants.DEFAULT_PIPELINE, name);
    return Stream.of(SingleResult.success());
}
Also used : PipelineSpecification(com.graphaware.nlp.dsl.request.PipelineSpecification) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 48 with Description

use of org.neo4j.procedure.Description in project neo4j-nlp by graphaware.

the class TextProcessorsProcedure method train.

@Procedure(name = "ga.nlp.processor.train", mode = Mode.WRITE)
@Description("Procedure for training custom models.")
public Stream<SingleResult> train(@Name("customModelsRequest") Map<String, Object> customModelsRequest) {
    try {
        CustomModelsRequest request = CustomModelsRequest.fromMap(customModelsRequest);
        Object result = getNLPManager().train(request);
        return Stream.of(new SingleResult(result));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SingleResult(com.graphaware.nlp.dsl.result.SingleResult) CustomModelsRequest(com.graphaware.nlp.dsl.request.CustomModelsRequest) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 49 with Description

use of org.neo4j.procedure.Description in project neo4j-nlp by graphaware.

the class AnnotateFunction method getAnnotation.

@UserFunction("ga.nlp.processor.annotate")
@Description("Perform the annotation on the given text, returns the produced annotation domain")
public Map<String, Object> getAnnotation(@Name("text") String text, @Name("pipelineSpecification") Map<String, Object> specificationInput) {
    PipelineSpecification pipelineSpecification = PipelineSpecification.fromMap(specificationInput);
    String pipeline = getNLPManager().getPipeline(pipelineSpecification.getName());
    PipelineSpecification spec = getConfiguration().loadPipeline(pipeline);
    TextProcessor processor = getNLPManager().getTextProcessorsManager().getTextProcessor(spec.getTextProcessor());
    AnnotatedText annotatedText = processor.annotateText(text, "en", pipelineSpecification);
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
    return mapper.convertValue(annotatedText, Map.class);
}
Also used : PipelineSpecification(com.graphaware.nlp.dsl.request.PipelineSpecification) TextProcessor(com.graphaware.nlp.processor.TextProcessor) AnnotatedText(com.graphaware.nlp.domain.AnnotatedText) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Description(org.neo4j.procedure.Description) UserFunction(org.neo4j.procedure.UserFunction)

Example 50 with Description

use of org.neo4j.procedure.Description in project neo4j-nlp by graphaware.

the class SentenceFunctions method nextTags.

@UserFunction("ga.nlp.sentence.nextTags")
@Description("Returns a list of Tag nodes that appear just after the given Tag in a sentence along with the frequency")
public List<Map<String, Object>> nextTags(@Name("from") Node from) {
    Map<Long, Integer> freqMap = new HashMap<>();
    Map<Long, Node> references = new HashMap<>();
    for (Relationship rel : from.getRelationships(Relationships.TAG_OCCURRENCE_TAG, Direction.INCOMING)) {
        Node tagOccurence = rel.getStartNode();
        int minPosition = (int) tagOccurence.getProperty(Properties.END_POSITION);
        Node sentence = tagOccurence.getSingleRelationship(Relationships.SENTENCE_TAG_OCCURRENCE, Direction.INCOMING).getStartNode();
        for (Relationship rel2 : sentence.getRelationships(Relationships.SENTENCE_TAG_OCCURRENCE, Direction.OUTGOING)) {
            Node occurence = rel2.getEndNode();
            int occPosition = (int) occurence.getProperty(Properties.START_POSITION);
            if (occPosition == (minPosition + 1)) {
                Node tag = occurence.getSingleRelationship(Relationships.TAG_OCCURRENCE_TAG, Direction.OUTGOING).getEndNode();
                Integer freq = freqMap.containsKey(tag.getId()) ? freqMap.get(tag.getId()) + 1 : 1;
                freqMap.put(tag.getId(), freq);
            }
        }
    }
    List<Map<String, Object>> response = new ArrayList<>();
    for (Long l : references.keySet()) {
        Map<String, Object> m = new HashMap<>();
        m.put("node", references.get(l));
        m.put("frequency", freqMap.get(l));
        response.add(m);
    }
    return response;
}
Also used : HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) Relationship(org.neo4j.graphdb.Relationship) Map(java.util.Map) HashMap(java.util.HashMap) Description(org.neo4j.procedure.Description) UserFunction(org.neo4j.procedure.UserFunction)

Aggregations

Description (org.neo4j.procedure.Description)65 Procedure (org.neo4j.procedure.Procedure)58 SystemProcedure (org.neo4j.kernel.api.procedure.SystemProcedure)25 ArrayList (java.util.ArrayList)19 Node (org.neo4j.graphdb.Node)15 HashMap (java.util.HashMap)14 Stream (java.util.stream.Stream)14 Map (java.util.Map)13 Context (org.neo4j.procedure.Context)13 Name (org.neo4j.procedure.Name)13 Collectors (java.util.stream.Collectors)12 Relationship (org.neo4j.graphdb.Relationship)12 ProcedureException (org.neo4j.internal.kernel.api.exceptions.ProcedureException)12 ZoneId (java.time.ZoneId)11 Comparator (java.util.Comparator)11 List (java.util.List)11 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)11 Status (org.neo4j.kernel.api.exceptions.Status)11 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)11 Admin (org.neo4j.procedure.Admin)11