stanfordcorenlp

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2022 License: MIT Imports: 8 Imported by: 0

README

go-stanfordcorenlp

stanfordcorenlp is a simple Golang client for Stanford CoreNLP server.

Install

go get -u github.com/shota3506/go-stanfordcorenlp

Usage

Run Stanford CoreNLP server

Please run the Stanford CoreNLP server following the official documentation. Or you can run the server under docker.

Make sure you use version 4.0.0 or above.

Tokenize

package main

import (
	"context"
	"fmt"
	"log"

	corenlp "github.com/shota3506/go-stanfordcorenlp"
)

func main() {
	ctx := context.Background()

	// create client for Stanford CoreNLP
	client := corenlp.NewClient(ctx, "http://localhost:9000")

	// sample text
	text := "The quick brown fox jumped over the lazy dog."
	resp, err := client.Do(ctx, text, corenlp.AnnotatorTokenize)
	if err != nil {
		log.Fatal(err)
	}

	s, err := corenlp.UnmarshalSentence(resp)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(s) // The quick brown fox jumped over the lazy dog .

	fmt.Println(s.Tokens[1].Word) // quick
	fmt.Println(s.Tokens[3].Word) // fox
}

Pos Tagging

package main

import (
	"context"
	"fmt"
	"log"

	corenlp "github.com/shota3506/go-stanfordcorenlp"
)

func main() {
	ctx := context.Background()

	// create client for Stanford CoreNLP
	client := corenlp.NewClient(ctx, "http://localhost:9000")

	// sample text
	text := "The quick brown fox jumped over the lazy dog."
	resp, err := client.Do(ctx, text,
		corenlp.AnnotatorTokenize|corenlp.AnnotatorSsplit|corenlp.AnnotatorPos)
	if err != nil {
		log.Fatal(err)
	}

	d, err := corenlp.UnmarshalDocument(resp)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(d) // The quick brown fox jumped over the lazy dog .

	fmt.Println(d.Sentences[0].Tokens[1].Word) // quick
	fmt.Println(d.Sentences[0].Tokens[1].Pos)  // JJ

	fmt.Println(d.Sentences[0].Tokens[3].Word) // fox
	fmt.Println(d.Sentences[0].Tokens[3].Pos)  // NN
}

Parsing

package main

import (
	"context"
	"fmt"
	"log"

	corenlp "github.com/shota3506/go-stanfordcorenlp"
)

func main() {
	ctx := context.Background()

	// create client for Stanford CoreNLP
	client := corenlp.NewClient(ctx, "http://localhost:9000")

	// sample text
	text := "The quick brown fox jumped over the lazy dog."
	resp, err := client.Do(ctx, text,
		corenlp.AnnotatorTokenize|corenlp.AnnotatorSsplit|corenlp.AnnotatorPos|corenlp.AnnotatorParse)
	if err != nil {
		log.Fatal(err)
	}

	d, err := corenlp.UnmarshalDocument(resp)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(d.Sentences[0].Parse)
	/*
	  (ROOT
	    (S
	      (NP (DT The) (JJ quick) (JJ brown) (NN fox))
	      (VP (VBD jumped)
	        (PP (IN over)
	          (NP (DT the) (JJ lazy) (NN dog))))
	      (. .)))
	*/
}

Documentation

Overview

Package stanfordcorenlp provides client for Stanford CoreNLP server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(ctx context.Context, url string) *client

NewClient is a factory method to create a client for Stanford CoreNLP server.

Types

type AnnotatorType

type AnnotatorType int
const (
	AnnotatorTokenize AnnotatorType = 1 << iota
	AnnotatorCleanxml
	AnnotatorSsplit
	AnnotatorDocdate
	AnnotatorPos
	AnnotatorLemma
	AnnotatorNer
	AnnotatorRegexner
	AnnotatorSentiment
	AnnotatorParse
	AnnotatorDepparse
	AnnotatorDcoref
	AnnotatorRelation
	AnnotatorNatlog
	AnnotatorEntitylink
	AnnotatorKbp
	AnnotatorQuote
)

func (AnnotatorType) MarshalJSON

func (a AnnotatorType) MarshalJSON() ([]byte, error)

func (AnnotatorType) String

func (a AnnotatorType) String() string

type Client

type Client interface {
	Do(ctx context.Context, text string, annotators AnnotatorType) ([]byte, error)
}

type DependencyNode

type DependencyNode struct {
	Dep            string `json:"dep"`
	Governor       int    `json:"governor"`
	GovernorGloss  string `json:"governorGloss"`
	Dependent      int    `json:"dependent"`
	DependentGloss string `json:"dependentGloss"`
}

type Document

type Document struct {
	Sentences []*Sentence `json:"sentences"`
}

func UnmarshalDocument

func UnmarshalDocument(data []byte) (*Document, error)

func (*Document) String

func (d *Document) String() string

type Sentence

type Sentence struct {
	Index                        int               `json:"index"`
	Tokens                       []*Token          `json:"tokens"`
	Parse                        string            `json:"parse,omitempty"`
	BasicDependencies            []*DependencyNode `json:"basicDependencies,omitempty"`
	EnhancedDependencies         []*DependencyNode `json:"enhancedDependencies,omitempty"`
	EnhancedPlusPlusDependencies []*DependencyNode `json:"enhancedPlusPlusDependencies,omitempty"`
}

func UnmarshalSentence

func UnmarshalSentence(data []byte) (*Sentence, error)

func (*Sentence) String

func (s *Sentence) String() string

type Token

type Token struct {
	Index                int    `json:"index"`
	Word                 string `json:"word"`
	OriginalText         string `json:"originalText,omitempty"`
	Lemma                string `json:"lemma,omitempty"`
	CharacterOffsetBegin int    `json:"characterOffsetBegin,omitempty"`
	CharacterOffsetEnd   int    `json:"characterOffsetEnd,omitempty"`
	Pos                  string `json:"pos,omitempty"`
	Before               string `json:"before,omitempty"`
	After                string `json:"after,omitempty"`
}

func (*Token) String

func (t *Token) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL