Skip to content

Instantly share code, notes, and snippets.

@sekky0905
Created May 31, 2017 07:54
Show Gist options
  • Save sekky0905/271356409e64cad36593e3e3b5946931 to your computer and use it in GitHub Desktop.
Save sekky0905/271356409e64cad36593e3e3b5946931 to your computer and use it in GitHub Desktop.
【自然言語処理】Google Natural Language API Client LibrariesをGoで実装してみる ref: http://qiita.com/Sekky0905/items/0a07ac74518b26816e04
go get -u cloud.google.com/go/language/apiv1
package main
import (
"cloud.google.com/go/language/apiv1"
"encoding/json"
"fmt"
"golang.org/x/net/context"
languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
"log"
)
func main() {
doAnalyze()
}
func doAnalyze() {
// コンテキスト生成
ctx := context.Background()
// クライアント生成
client, err := language.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// 分析するテキストを宣言
text := "Natural Language API Client LibrariesクライアントをGoで実装してみる。"
// AnalyzeSyntaxRequest構造体を生成
req := &languagepb.AnalyzeSyntaxRequest{
Document: &languagepb.Document{
// 指定なし 0
// Plain text 1
// HTML 2
Type: 1,
// 分析する内容
Source: &languagepb.Document_Content{
Content: text,
},
},
EncodingType: languagepb.EncodingType_UTF8,
}
// 構文解析を行う
resp, err := client.AnalyzeSyntax(ctx, req)
if err != nil {
log.Fatalf("Failed to AnalyzeSyntax: %v", err)
}
// jsonにする
rawJson, err := json.Marshal(resp)
if err != nil {
log.Fatalf("Failed to Marshal json: %v", err)
}
jsonStr := string(rawJson)
fmt.Println("構文解析の結果\n" + jsonStr)
}
{
"sentences": [
{
"text": {
"content": "Natural Language API Client LibrariesクライアントをGoで実装してみる。"
}
}
],
"tokens": [
{
"text": {
"content": "Natural"
},
"part_of_speech": {
"tag": 12,
"proper": 1
},
"dependency_edge": {
"head_token_index": 5,
"label": 26
},
"lemma": "Natural"
},
{
"text": {
"content": "Language",
"begin_offset": 8
},
"part_of_speech": {
"tag": 6,
"proper": 1
},
"dependency_edge": {
"head_token_index": 2,
"label": 26
},
"lemma": "Language"
},
{
"text": {
"content": "API",
"begin_offset": 17
},
"part_of_speech": {
"tag": 6,
"proper": 1
},
"dependency_edge": {
"head_token_index": 5,
"label": 26
},
"lemma": "API"
},
{
"text": {
"content": "Client",
"begin_offset": 21
},
"part_of_speech": {
"tag": 6,
"proper": 2
},
"dependency_edge": {
"head_token_index": 4,
"label": 26
},
"lemma": "Client"
},
{
"text": {
"content": "Libraries",
"begin_offset": 28
},
"part_of_speech": {
"tag": 6,
"proper": 2
},
"dependency_edge": {
"head_token_index": 5,
"label": 26
},
"lemma": "Libraries"
},
{
"text": {
"content": "クライアント",
"begin_offset": 37
},
"part_of_speech": {
"tag": 6,
"proper": 2
},
"dependency_edge": {
"head_token_index": 9,
"label": 18
},
"lemma": "クライアント"
},
{
"text": {
"content": "を",
"begin_offset": 55
},
"part_of_speech": {
"tag": 9,
"case": 1,
"proper": 2
},
"dependency_edge": {
"head_token_index": 5,
"label": 45
},
"lemma": "を"
},
{
"text": {
"content": "Go",
"begin_offset": 58
},
"part_of_speech": {
"tag": 12,
"proper": 2
},
"dependency_edge": {
"head_token_index": 9,
"label": 64
},
"lemma": "Go"
},
{
"text": {
"content": "で",
"begin_offset": 60
},
"part_of_speech": {
"tag": 9,
"case": 2,
"proper": 2
},
"dependency_edge": {
"head_token_index": 7,
"label": 45
},
"lemma": "で"
},
{
"text": {
"content": "実装",
"begin_offset": 63
},
"part_of_speech": {
"tag": 6,
"proper": 2
},
"dependency_edge": {
"head_token_index": 9,
"label": 54
},
"lemma": "実装"
},
{
"text": {
"content": "し",
"begin_offset": 69
},
"part_of_speech": {
"tag": 11,
"form": 5,
"proper": 2
},
"dependency_edge": {
"head_token_index": 9,
"label": 24
},
"lemma": "する"
},
{
"text": {
"content": "て",
"begin_offset": 72
},
"part_of_speech": {
"tag": 9,
"proper": 2
},
"dependency_edge": {
"head_token_index": 9,
"label": 45
},
"lemma": "て"
},
{
"text": {
"content": "みる",
"begin_offset": 75
},
"part_of_speech": {
"tag": 11,
"form": 4,
"proper": 2
},
"dependency_edge": {
"head_token_index": 9,
"label": 66
},
"lemma": "みる"
},
{
"text": {
"content": "。",
"begin_offset": 81
},
"part_of_speech": {
"tag": 10,
"proper": 2
},
"dependency_edge": {
"head_token_index": 9,
"label": 32
},
"lemma": "。"
}
],
"language": "ja"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment