|
26 | 26 | def get_native_encoding_type():
|
27 | 27 | """Returns the encoding type that matches Python's native strings."""
|
28 | 28 | if sys.maxunicode == 65535:
|
29 |
| - return 'UTF16' |
| 29 | + return "UTF16" |
30 | 30 | else:
|
31 |
| - return 'UTF32' |
| 31 | + return "UTF32" |
32 | 32 |
|
33 | 33 |
|
34 |
| -def analyze_entities(text, encoding='UTF32'): |
| 34 | +def analyze_entities(text, encoding="UTF32"): |
35 | 35 | body = {
|
36 |
| - 'document': { |
37 |
| - 'type': 'PLAIN_TEXT', |
38 |
| - 'content': text, |
39 |
| - }, |
40 |
| - 'encoding_type': encoding, |
| 36 | + "document": {"type": "PLAIN_TEXT", "content": text}, |
| 37 | + "encoding_type": encoding, |
41 | 38 | }
|
42 | 39 |
|
43 |
| - service = googleapiclient.discovery.build('language', 'v1') |
| 40 | + service = googleapiclient.discovery.build("language", "v1") |
44 | 41 |
|
45 | 42 | request = service.documents().analyzeEntities(body=body)
|
46 | 43 | response = request.execute()
|
47 | 44 |
|
48 | 45 | return response
|
49 | 46 |
|
50 | 47 |
|
51 |
| -def analyze_sentiment(text, encoding='UTF32'): |
| 48 | +def analyze_sentiment(text, encoding="UTF32"): |
52 | 49 | body = {
|
53 |
| - 'document': { |
54 |
| - 'type': 'PLAIN_TEXT', |
55 |
| - 'content': text, |
56 |
| - }, |
57 |
| - 'encoding_type': encoding |
| 50 | + "document": {"type": "PLAIN_TEXT", "content": text}, |
| 51 | + "encoding_type": encoding, |
58 | 52 | }
|
59 | 53 |
|
60 |
| - service = googleapiclient.discovery.build('language', 'v1') |
| 54 | + service = googleapiclient.discovery.build("language", "v1") |
61 | 55 |
|
62 | 56 | request = service.documents().analyzeSentiment(body=body)
|
63 | 57 | response = request.execute()
|
64 | 58 |
|
65 | 59 | return response
|
66 | 60 |
|
67 | 61 |
|
68 |
| -def analyze_syntax(text, encoding='UTF32'): |
| 62 | +def analyze_syntax(text, encoding="UTF32"): |
69 | 63 | body = {
|
70 |
| - 'document': { |
71 |
| - 'type': 'PLAIN_TEXT', |
72 |
| - 'content': text, |
73 |
| - }, |
74 |
| - 'encoding_type': encoding |
| 64 | + "document": {"type": "PLAIN_TEXT", "content": text}, |
| 65 | + "encoding_type": encoding, |
75 | 66 | }
|
76 | 67 |
|
77 |
| - service = googleapiclient.discovery.build('language', 'v1') |
| 68 | + service = googleapiclient.discovery.build("language", "v1") |
78 | 69 |
|
79 | 70 | request = service.documents().analyzeSyntax(body=body)
|
80 | 71 | response = request.execute()
|
81 | 72 |
|
82 | 73 | return response
|
83 | 74 |
|
84 | 75 |
|
85 |
| -if __name__ == '__main__': |
| 76 | +if __name__ == "__main__": |
86 | 77 | parser = argparse.ArgumentParser(
|
87 |
| - description=__doc__, |
88 |
| - formatter_class=argparse.RawDescriptionHelpFormatter) |
89 |
| - parser.add_argument('command', choices=[ |
90 |
| - 'entities', 'sentiment', 'syntax']) |
91 |
| - parser.add_argument('text') |
| 78 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 79 | + ) |
| 80 | + parser.add_argument("command", choices=["entities", "sentiment", "syntax"]) |
| 81 | + parser.add_argument("text") |
92 | 82 |
|
93 | 83 | args = parser.parse_args()
|
94 | 84 |
|
95 |
| - if args.command == 'entities': |
| 85 | + if args.command == "entities": |
96 | 86 | result = analyze_entities(args.text, get_native_encoding_type())
|
97 |
| - elif args.command == 'sentiment': |
| 87 | + elif args.command == "sentiment": |
98 | 88 | result = analyze_sentiment(args.text, get_native_encoding_type())
|
99 |
| - elif args.command == 'syntax': |
| 89 | + elif args.command == "syntax": |
100 | 90 | result = analyze_syntax(args.text, get_native_encoding_type())
|
101 | 91 |
|
102 | 92 | print(json.dumps(result, indent=2))
|
0 commit comments