Reputation: 1813
I'm trying to classify some text and have the following code:
from google.cloud import language
from google.cloud.language import enums
from google.cloud.language import types
def classify_text(text):
"""Classifies content categories of the provided text."""
client = language.LanguageServiceClient()
if isinstance(text, six.binary_type):
text = text.decode('utf-8')
document = types.Document(
content=text.encode('utf-8'),
type=enums.Document.Type.PLAIN_TEXT)
categories = client.classify_text(document).categories
for category in categories:
print(u'=' * 20)
print(u'{:<16}: {}'.format('name', category.name))
print(u'{:<16}: {}'.format('confidence', category.confidence))
But when I call: classify_text('Hello')
, I get:
AttributeError: 'LanguageServiceClient' object has no attribute 'classify_text'
I can't seem to find any questions here on SO about this error. Does anyone know what's happening here?
Upvotes: 1
Views: 981
Reputation: 1813
The version I was using, 0.29, was deprecated. The current version is 1.1 and the correct function is the following:
def classify(text, verbose=True):
"""Classify the input text into categories. """
language_client = language.LanguageServiceClient()
document = language.types.Document(
content=text,
type=language.enums.Document.Type.PLAIN_TEXT)
response = language_client.classify_text(document)
categories = response.categories
result = {}
for category in categories:
# Turn the categories into a dictionary of the form:
# {category.name: category.confidence}, so that they can
# be treated as a sparse vector.
result[category.name] = category.confidence
if verbose:
print(text)
for category in categories:
print(u'=' * 20)
print(u'{:<16}: {}'.format('category', category.name))
print(u'{:<16}: {}'.format('confidence', category.confidence))
return result
That function is found here, but the function I mistakenly used is found here
Upvotes: 1
Reputation: 305
Try:
categories = client.classify_text(document)
cat = categories.categories
That's basic debugging, I know.
You could also comment out the declaration for a document and do this:
document = {}
And check if it's throwing the same error.
Since it should return either way a ClassifyTextResponse
, maybe this will start dividing the problem in parts. Better to attack one by one in these cases.
Upvotes: 0