In my previous posts, I have talked about building a small backend server to handle HTTP POST request. We also have tried out the Google NLP and we dived into the topic of NLTK. Well, now we know how to create a Go function to handle HTTP request and we understand the idea of Natural language Processing. Why not building your own Google NLP API?

Let’s now look at the key functions

// Now we need to create a function to parse and tag all the data 
// There is a pre-built lib you can use - "gopkg.in/jdkato/prose.v2"

import (
	"gopkg.in/jdkato/prose.v2"
)

func goProse(docs string) (tag [][]string, entities [][]string){
	doc, err := prose.NewDocument(docs)
    if err != nil {
        log.Fatal(err)
	}

    for _, tok := range doc.Tokens() {
		fmt.Println(tok.Text, tok.Tag)
		var t []string
		t = append(t, tok.Text, tok.Tag)
		tag = append(tag, t)
	}

	for _, ent := range doc.Entities() {
		fmt.Println(ent.Text, ent.Label)
		var e []string
		e = append(e, ent.Text, ent.Label)
		entities = append(entities, e)
	}
	
	return tag, entities

}

Let’s think about the API response data structure

type resultType struct {
	WhoamI    string
	Tag [][]string
	Ent [][]string
  }
// I think they look like a good data type :)

Okie, dokie. Looks good! Now you will need a Golang backend to handle the http request

func postHandler(w http.ResponseWriter, r *http.Request){
		if r.Method == "POST" {
			body, err := ioutil.ReadAll(r.Body)
			if err != nil {
				log.Println(err)
			}

			fmt.Println(string(body))

			var resultTag [][]string
			var resultEnt [][]string

		    resultTag,resultEnt  = goProse(string(body))

			result := resultType{"👋 from Pingzhou| ⛵", resultTag, resultEnt}

			fmt.Println(result)

			js, err := json.Marshal(result)
  			if err != nil {
    			http.Error(w, err.Error(), http.StatusInternalServerError)
    		return
  			}

			w.Header().Set("Content-Type", "application/json")
  			w.Write(js)
		}
		
	
  }

Oh! Yes! All set! Let’s start the http server!

func main() {

	mux := http.NewServeMux()

	mux.HandleFunc("/v1/go-nlp", postHandler)
	http.ListenAndServe(":"+os.Getenv("api_port"), mux)
 }

Yes! Here you go! Your own nlp-api is up :)

$ curl -v -s -X POST http://localhost:8080/v1/go-nlp \
> -d 'Google, headquartered in Mountain View (1600 Amphitheatre Pkwy, Mountain View, CA 940430), unveiled the new Android phone for $799 at the Consumer Electronic Show. Sundar Pichai said in his keynote that users love their new Android phones.'
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> POST /v1/go-nlp HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Length: 239
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 239 out of 239 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Wed, 13 Nov 2019 18:13:07 GMT
< Content-Length: 846
< 
* Connection #0 to host localhost left intact

{“WhoamI”:“👋 from Pingzhou| ⛵”,“Tag”:[[“Google”,“NNP”],[",",","],[“headquartered”,“VBD”],[“in”,“IN”],[“Mountain”,“NNP”],[“View”,“NNP”],["(","("],[“1600”,“CD”],[“Amphitheatre”,“NNP”],[“Pkwy”,“NNP”],[",",","],[“Mountain”,“NNP”],[“View”,“NNP”],[",",","],[“CA”,“NNP”],[“940430”,“CD”],[")",")"],[",",","],[“unveiled”,“VBD”],[“the”,“DT”],[“new”,“JJ”],[“Android”,“NNP”],[“phone”,“NN”],[“for”,“IN”],[“$”,“$”],[“799”,“CD”],[“at”,“IN”],[“the”,“DT”],[“Consumer”,“NNP”],[“Electronic”,“NNP”],[“Show.",“NNP”],[“Sundar”,“NNP”],[“Pichai”,“NNP”],[“said”,“VBD”],[“in”,“IN”],[“his”,“PRP$”],[“keynote”,“NN”],[“that”,“IN”],[“users”,“NNS”],[“love”,“VBP”],[“their”,“PRP$”],[“new”,“JJ”],[“Android”,“NNP”],[“phones”,“NNS”],[".","."]],“Ent”:[[“Google”,“GPE”],[“Mountain View”,“GPE”],[“Mountain View”,“GPE”],[“Consumer Electronic Show. Sundar Pichai”,“ORGANIZATION”]]}

$ curl -s -X POST http://localhost:8080/v1/go-nlp \
-d 'Google, headquartered in Mountain View (1600 Amphitheatre Pkwy, Mountain View, CA 940430), unveiled the new Android phone for $799 at the Consumer Electronic Show. Sundar Pichai said in his keynote that users love their new Android phones.' | jq .Ent[]

[ “Google”, “GPE” ] [ “Mountain View”, “GPE” ] [ “Mountain View”, “GPE” ] [ “Consumer Electronic Show. Sundar Pichai”, “ORGANIZATION” ]

$ curl -s -X POST http://localhost:8080/v1/go-nlp \
-d 'Google, headquartered in Mountain View (1600 Amphitheatre Pkwy, Mountain View, CA 940430), unveiled the new Android phone for $799 at the Consumer Electronic Show. Sundar Pichai said in his keynote that users love their new Android phones.' | jq .Tag[0]

[ “Google”, “NNP” ]

Buy me a coffeeBuy me a coffee