google_nlp_api.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:tokenquery 作者: ramtinms 项目源码 文件源码
def call_google_nlp(text):
    """Use the NL API to analyze the given text string, and returns the
    response from the API.  Requests an encodingType that matches
    the encoding used natively by Python.  Raises an
    errors.HTTPError if there is a connection problem.
    """

    # TODO check cred exists ....

    # check GOOGLE_APPLICATION_CREDENTIALS
    credentials = GoogleCredentials.get_application_default()
    scoped_credentials = credentials.create_scoped(
        ['https://www.googleapis.com/auth/cloud-platform'])
    http = httplib2.Http()
    scoped_credentials.authorize(http)
    service = discovery.build(
        'language', 'v1beta1', http=http)
    body = {
        'document': {
            'type': 'PLAIN_TEXT',
            'content': text
        },
        'features': {
            # 'extract_syntax': True,
            'extractEntities': True
        },
        'encodingType': get_native_encoding_type(),
    }
    request = service.documents().annotateText(body=body)
    return request.execute()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号