-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaidu.py
36 lines (30 loc) · 992 Bytes
/
baidu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- encoding: utf-8 -*-
from aip.nlp import AipNlp
from distutils.util import strtobool
import os
ENV_BCE_APP_ID = os.getenv("ENV_BCE_APP_ID", "")
ENV_BCE_API_KEY = os.getenv("ENV_BCE_API_KEY", "")
ENV_BCE_SECRET_KEY = os.getenv("ENV_BCE_SECRET_KEY", "")
class BaidubceProvider(object):
def __init__(
self,
app_id=ENV_BCE_APP_ID,
api_key=ENV_BCE_API_KEY,
secret_key=ENV_BCE_SECRET_KEY,
):
self._app_id = app_id
self._api_key = api_key
self._secret_key = secret_key
self._app = None
def open(self):
self._app = AipNlp(self._app_id, self._api_key, self._secret_key)
def address_recognition(self, text):
if self._app:
return self._app.address(text)
return None
def recognition(self, type, *args, **kargs):
name = f"{type}_recognition"
method = getattr(self, name, None)
if method:
return method(*args, **kargs)
return None