|
24 | 24 | import argparse
|
25 | 25 |
|
26 | 26 | from google.cloud import language
|
| 27 | +from google.cloud.language import enums |
| 28 | +from google.cloud.language import types |
27 | 29 | import six
|
28 | 30 |
|
29 | 31 |
|
| 32 | +# [START def_sentiment_text] |
30 | 33 | def sentiment_text(text):
|
31 | 34 | """Detects sentiment in the text."""
|
32 |
| - language_client = language.Client() |
| 35 | + client = language.LanguageServiceClient() |
33 | 36 |
|
34 | 37 | if isinstance(text, six.binary_type):
|
35 | 38 | text = text.decode('utf-8')
|
36 | 39 |
|
37 | 40 | # Instantiates a plain text document.
|
38 |
| - document = language_client.document_from_text(text) |
| 41 | + # [START migration_document_text] |
| 42 | + # [START migration_analyze_sentiment] |
| 43 | + document = types.Document( |
| 44 | + content=text, |
| 45 | + type=enums.Document.Type.PLAIN_TEXT) |
| 46 | + # [END migration_document_text] |
39 | 47 |
|
40 | 48 | # Detects sentiment in the document. You can also analyze HTML with:
|
41 |
| - # document.doc_type == language.Document.HTML |
42 |
| - sentiment = document.analyze_sentiment().sentiment |
| 49 | + # document.type == enums.Document.Type.HTML |
| 50 | + sentiment = client.analyze_sentiment(document).document_sentiment |
43 | 51 |
|
44 | 52 | print('Score: {}'.format(sentiment.score))
|
45 | 53 | print('Magnitude: {}'.format(sentiment.magnitude))
|
| 54 | + # [END migration_analyze_sentiment] |
| 55 | +# [END def_sentiment_text] |
46 | 56 |
|
47 | 57 |
|
| 58 | +# [START def_sentiment_file] |
48 | 59 | def sentiment_file(gcs_uri):
|
49 | 60 | """Detects sentiment in the file located in Google Cloud Storage."""
|
50 |
| - language_client = language.Client() |
| 61 | + client = language.LanguageServiceClient() |
51 | 62 |
|
52 | 63 | # Instantiates a plain text document.
|
53 |
| - document = language_client.document_from_url(gcs_uri) |
| 64 | + # [START migration_document_gcs_uri] |
| 65 | + document = types.Document( |
| 66 | + gcs_content_uri=gcs_uri, |
| 67 | + type=enums.Document.Type.PLAIN_TEXT) |
| 68 | + # [END migration_document_gcs_uri] |
54 | 69 |
|
55 | 70 | # Detects sentiment in the document. You can also analyze HTML with:
|
56 |
| - # document.doc_type == language.Document.HTML |
57 |
| - sentiment = document.analyze_sentiment().sentiment |
| 71 | + # document.type == enums.Document.Type.HTML |
| 72 | + sentiment = client.analyze_sentiment(document).document_sentiment |
58 | 73 |
|
59 | 74 | print('Score: {}'.format(sentiment.score))
|
60 | 75 | print('Magnitude: {}'.format(sentiment.magnitude))
|
| 76 | +# [END def_sentiment_file] |
61 | 77 |
|
62 | 78 |
|
| 79 | +# [START def_entities_text] |
63 | 80 | def entities_text(text):
|
64 | 81 | """Detects entities in the text."""
|
65 |
| - language_client = language.Client() |
| 82 | + client = language.LanguageServiceClient() |
66 | 83 |
|
67 | 84 | if isinstance(text, six.binary_type):
|
68 | 85 | text = text.decode('utf-8')
|
69 | 86 |
|
70 | 87 | # Instantiates a plain text document.
|
71 |
| - document = language_client.document_from_text(text) |
| 88 | + # [START migration_analyze_entities] |
| 89 | + document = types.Document( |
| 90 | + content=text, |
| 91 | + type=enums.Document.Type.PLAIN_TEXT) |
72 | 92 |
|
73 | 93 | # Detects entities in the document. You can also analyze HTML with:
|
74 |
| - # document.doc_type == language.Document.HTML |
75 |
| - entities = document.analyze_entities().entities |
| 94 | + # document.type == enums.Document.Type.HTML |
| 95 | + entities = client.analyze_entities(document).entities |
76 | 96 |
|
77 | 97 | for entity in entities:
|
78 | 98 | print('=' * 20)
|
79 | 99 | print(u'{:<16}: {}'.format('name', entity.name))
|
80 |
| - print(u'{:<16}: {}'.format('type', entity.entity_type)) |
| 100 | + print(u'{:<16}: {}'.format('type', entity.type)) |
81 | 101 | print(u'{:<16}: {}'.format('metadata', entity.metadata))
|
82 | 102 | print(u'{:<16}: {}'.format('salience', entity.salience))
|
83 | 103 | print(u'{:<16}: {}'.format('wikipedia_url',
|
84 | 104 | entity.metadata.get('wikipedia_url', '-')))
|
| 105 | + # [END migration_analyze_entities] |
| 106 | +# [END def_entities_text] |
85 | 107 |
|
86 | 108 |
|
| 109 | +# [START def_entities_file] |
87 | 110 | def entities_file(gcs_uri):
|
88 | 111 | """Detects entities in the file located in Google Cloud Storage."""
|
89 |
| - language_client = language.Client() |
| 112 | + client = language.LanguageServiceClient() |
90 | 113 |
|
91 | 114 | # Instantiates a plain text document.
|
92 |
| - document = language_client.document_from_url(gcs_uri) |
| 115 | + document = types.Document( |
| 116 | + gcs_content_uri=gcs_uri, |
| 117 | + type=enums.Document.Type.PLAIN_TEXT) |
93 | 118 |
|
94 | 119 | # Detects sentiment in the document. You can also analyze HTML with:
|
95 |
| - # document.doc_type == language.Document.HTML |
96 |
| - entities = document.analyze_entities().entities |
| 120 | + # document.type == enums.Document.Type.HTML |
| 121 | + entities = client.analyze_entities(document).entities |
97 | 122 |
|
98 | 123 | for entity in entities:
|
99 | 124 | print('=' * 20)
|
100 | 125 | print(u'{:<16}: {}'.format('name', entity.name))
|
101 |
| - print(u'{:<16}: {}'.format('type', entity.entity_type)) |
| 126 | + print(u'{:<16}: {}'.format('type', entity.type)) |
102 | 127 | print(u'{:<16}: {}'.format('metadata', entity.metadata))
|
103 | 128 | print(u'{:<16}: {}'.format('salience', entity.salience))
|
104 | 129 | print(u'{:<16}: {}'.format('wikipedia_url',
|
105 | 130 | entity.metadata.get('wikipedia_url', '-')))
|
| 131 | +# [END def_entities_file] |
106 | 132 |
|
107 | 133 |
|
| 134 | +# [START def_syntax_text] |
108 | 135 | def syntax_text(text):
|
109 | 136 | """Detects syntax in the text."""
|
110 |
| - language_client = language.Client() |
| 137 | + client = language.LanguageServiceClient() |
111 | 138 |
|
112 | 139 | if isinstance(text, six.binary_type):
|
113 | 140 | text = text.decode('utf-8')
|
114 | 141 |
|
115 | 142 | # Instantiates a plain text document.
|
116 |
| - document = language_client.document_from_text(text) |
| 143 | + # [START migration_analyze_syntax] |
| 144 | + document = types.Document( |
| 145 | + content=text, |
| 146 | + type=enums.Document.Type.PLAIN_TEXT) |
117 | 147 |
|
118 | 148 | # Detects syntax in the document. You can also analyze HTML with:
|
119 |
| - # document.doc_type == language.Document.HTML |
120 |
| - tokens = document.analyze_syntax().tokens |
| 149 | + # document.type == enums.Document.Type.HTML |
| 150 | + tokens = client.analyze_syntax(document).tokens |
| 151 | + |
| 152 | + # part-of-speech tags from enums.PartOfSpeech.Tag |
| 153 | + pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', |
| 154 | + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') |
121 | 155 |
|
122 | 156 | for token in tokens:
|
123 |
| - print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content)) |
| 157 | + print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], |
| 158 | + token.text.content)) |
| 159 | + # [END migration_analyze_syntax] |
| 160 | +# [END def_syntax_text] |
124 | 161 |
|
125 | 162 |
|
| 163 | +# [START def_syntax_file] |
126 | 164 | def syntax_file(gcs_uri):
|
127 | 165 | """Detects syntax in the file located in Google Cloud Storage."""
|
128 |
| - language_client = language.Client() |
| 166 | + client = language.LanguageServiceClient() |
129 | 167 |
|
130 | 168 | # Instantiates a plain text document.
|
131 |
| - document = language_client.document_from_url(gcs_uri) |
| 169 | + document = types.Document( |
| 170 | + gcs_content_uri=gcs_uri, |
| 171 | + type=enums.Document.Type.PLAIN_TEXT) |
132 | 172 |
|
133 | 173 | # Detects syntax in the document. You can also analyze HTML with:
|
134 |
| - # document.doc_type == language.Document.HTML |
135 |
| - tokens = document.analyze_syntax().tokens |
| 174 | + # document.type == enums.Document.Type.HTML |
| 175 | + tokens = client.analyze_syntax(document).tokens |
| 176 | + |
| 177 | + # part-of-speech tags from enums.PartOfSpeech.Tag |
| 178 | + pos_tag = ('UNKNOWN', 'ADJ', 'ADP', 'ADV', 'CONJ', 'DET', 'NOUN', 'NUM', |
| 179 | + 'PRON', 'PRT', 'PUNCT', 'VERB', 'X', 'AFFIX') |
136 | 180 |
|
137 | 181 | for token in tokens:
|
138 |
| - print(u'{}: {}'.format(token.part_of_speech.tag, token.text_content)) |
| 182 | + print(u'{}: {}'.format(pos_tag[token.part_of_speech.tag], |
| 183 | + token.text.content)) |
| 184 | +# [END def_syntax_file] |
139 | 185 |
|
140 | 186 |
|
141 | 187 | if __name__ == '__main__':
|
|
0 commit comments