|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google, Inc |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Analyzes text using the Google Cloud Natural Language API.""" |
| 18 | + |
| 19 | +import argparse |
| 20 | +import json |
| 21 | +import sys |
| 22 | + |
| 23 | +import googleapiclient.discovery |
| 24 | + |
| 25 | + |
| 26 | +def get_native_encoding_type(): |
| 27 | + """Returns the encoding type that matches Python's native strings.""" |
| 28 | + if sys.maxunicode == 65535: |
| 29 | + return "UTF16" |
| 30 | + else: |
| 31 | + return "UTF32" |
| 32 | + |
| 33 | + |
| 34 | +def analyze_entities(text, encoding="UTF32"): |
| 35 | + body = { |
| 36 | + "document": {"type": "PLAIN_TEXT", "content": text}, |
| 37 | + "encoding_type": encoding, |
| 38 | + } |
| 39 | + |
| 40 | + service = googleapiclient.discovery.build("language", "v1") |
| 41 | + |
| 42 | + request = service.documents().analyzeEntities(body=body) |
| 43 | + response = request.execute() |
| 44 | + |
| 45 | + return response |
| 46 | + |
| 47 | + |
| 48 | +def analyze_sentiment(text, encoding="UTF32"): |
| 49 | + body = { |
| 50 | + "document": {"type": "PLAIN_TEXT", "content": text}, |
| 51 | + "encoding_type": encoding, |
| 52 | + } |
| 53 | + |
| 54 | + service = googleapiclient.discovery.build("language", "v1") |
| 55 | + |
| 56 | + request = service.documents().analyzeSentiment(body=body) |
| 57 | + response = request.execute() |
| 58 | + |
| 59 | + return response |
| 60 | + |
| 61 | + |
| 62 | +def analyze_syntax(text, encoding="UTF32"): |
| 63 | + body = { |
| 64 | + "document": {"type": "PLAIN_TEXT", "content": text}, |
| 65 | + "encoding_type": encoding, |
| 66 | + } |
| 67 | + |
| 68 | + service = googleapiclient.discovery.build("language", "v1") |
| 69 | + |
| 70 | + request = service.documents().analyzeSyntax(body=body) |
| 71 | + response = request.execute() |
| 72 | + |
| 73 | + return response |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + parser = argparse.ArgumentParser( |
| 78 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 79 | + ) |
| 80 | + parser.add_argument("command", choices=["entities", "sentiment", "syntax"]) |
| 81 | + parser.add_argument("text") |
| 82 | + |
| 83 | + args = parser.parse_args() |
| 84 | + |
| 85 | + if args.command == "entities": |
| 86 | + result = analyze_entities(args.text, get_native_encoding_type()) |
| 87 | + elif args.command == "sentiment": |
| 88 | + result = analyze_sentiment(args.text, get_native_encoding_type()) |
| 89 | + elif args.command == "syntax": |
| 90 | + result = analyze_syntax(args.text, get_native_encoding_type()) |
| 91 | + |
| 92 | + print(json.dumps(result, indent=2)) |
0 commit comments