-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathnlp.py
More file actions
37 lines (30 loc) · 1.13 KB
/
nlp.py
File metadata and controls
37 lines (30 loc) · 1.13 KB
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
37
"""Diffbot NLP API: entity identification, resolution, and sentiment."""
from typing import TYPE_CHECKING, Any, Dict
if TYPE_CHECKING:
from .client import Diffbot, DiffbotAsync
NLP_BASE = "https://nl.diffbot.com/v1/"
NLP_FIELDS = "entities,sentiment"
def entities(
client: "Diffbot",
text: str,
*,
lang: str = "auto",
) -> Dict[str, Any]:
params = {"token": client.token, "fields": NLP_FIELDS}
payload = [{"lang": lang, "format": "plain text", "content": text}]
response = client._http.post(client.nlp_url, params=params, json=payload)
client._raise_for_status(response)
data = response.json()
return data[0] if isinstance(data, list) else data
async def entities_async(
client: "DiffbotAsync",
text: str,
*,
lang: str = "auto",
) -> Dict[str, Any]:
params = {"token": client.token, "fields": NLP_FIELDS}
payload = [{"lang": lang, "format": "plain text", "content": text}]
response = await client._http.post(client.nlp_url, params=params, json=payload)
client._raise_for_status(response)
data = response.json()
return data[0] if isinstance(data, list) else data