LoginSignup
0
0

More than 3 years have passed since last update.

COTOHA でキーワードの抽出 (Golang)

Posted at

COTOHA API Portal の使用例です。

フォルダー構造

$ tree -a
.
├── akai_rousoku.txt
├── .env
├── get_config.go
├── get_token.go
└── key_word.go
key_word.go
// ---------------------------------------------------------------
//
//  key_word.go
//
//                  Feb/25/2020
// ---------------------------------------------------------------
package main

import (
    "fmt"
    "os"
    "encoding/json"
    "net/http"
    "strings"
    "io/ioutil"
)

// ---------------------------------------------------------------
func key_word_proc(config map[string]interface{},doc string) {
    out_filename := "out01.json"
//  fmt.Printf("%s\n",doc)

    data := make (map[string]interface{})
    data["document"] = doc
    data["type"] = "default"
    str_json, _ := json.Marshal(data)
//  fmt.Printf("%s\n",str_json)

    url_base := config["url_base"]
    fmt.Printf("%s\n",url_base)
    url_target := url_base.(string) + "v1/keyword"
    fmt.Printf("%s\n",url_target)

    req, _ := http.NewRequest("POST", url_target, strings.NewReader(string(str_json)))

    req.Header.Set("Content-Type","application/json") 
    req.Header.Set("Authorization", "Bearer " + config["access_token"].(string))

    client := new(http.Client)
    resp, error := client.Do(req)
    if error != nil {
        fmt.Println("*** error *** client.Do ***")
        fmt.Println("Request error:", error)
        }

    bb, err := ioutil.ReadAll(resp.Body)
    if err == nil {
        ioutil.WriteFile (out_filename,bb,0666)

        var unit_aa map[string]interface{}
        json.Unmarshal ([]byte(string(bb)), &unit_aa)
        fmt.Printf("len(unit_aa) = %d\n", len(unit_aa))
        unit_bb := unit_aa["result"]
        fmt.Println(unit_bb)
        }   


}

// ---------------------------------------------------------------
func main() {

    fmt.Fprintf (os.Stderr,"*** 開始 ***\n")

    file_in := os.Args[1]
    fmt.Printf ("%s\n",file_in)
    buff,_ := ioutil.ReadFile (file_in)
    doc := string(buff)

    config := get_config_proc ()

    access_token := get_token_proc (config)

    config["access_token"] = access_token

    fmt.Printf("%s\n",config["access_token"])

//  sentence := "特急はくたかで富山に向かいます。それから、金沢に行って、兼六園に行きます。"

    key_word_proc(config,doc)

    fmt.Fprintf (os.Stderr,"*** 終了 ***\n")
}

// ---------------------------------------------------------------

get_config.go get_token.go はこちら
COTOHA API で構文解析 (Golang)

実行コマンド

go run key_word.go get_config.go get_token.go akai_rousoku.txt

次のJSONファイルが作成されます。

out01.json
{
  "result" : [ {
    "form" : "猿",
    "score" : 135.52966
  }, {
    "form" : "蝋燭",
    "score" : 83.9601
  }, {
    "form" : "花火",
    "score" : 78.08584
  }, {
    "form" : "亀",
    "score" : 43.078
  }, {
    "form" : "火",
    "score" : 42.81965
  } ],
  "status" : 0,
  "message" : ""
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0