|
1 | 1 | """Diffbot NLP API: entity identification, resolution, and sentiment.""" |
2 | 2 |
|
3 | | -from typing import TYPE_CHECKING, Any, Dict |
| 3 | +from typing import TYPE_CHECKING, Any, Dict, Optional, Sequence |
4 | 4 |
|
5 | 5 | from ._constants import NLP_HOST_BASE |
6 | | -from .types import EntitiesResponse |
| 6 | +from .types import EntitiesResponse, NlpCustomSummary, NlpField, NlpFormat, NlpResponse |
7 | 7 |
|
8 | 8 | if TYPE_CHECKING: |
9 | 9 | from .client import Diffbot, DiffbotAsync |
@@ -36,3 +36,57 @@ async def entities_async( |
36 | 36 | response = await client.request("POST", client.nlp_url, params=params, json=payload) |
37 | 37 | data = response.json() |
38 | 38 | return data[0] if isinstance(data, list) else data |
| 39 | + |
| 40 | + |
| 41 | +def _build_nlp_request( |
| 42 | + client: Any, |
| 43 | + text: str, |
| 44 | + fields: Optional[Sequence[NlpField]], |
| 45 | + lang: str, |
| 46 | + format: NlpFormat, |
| 47 | + document_type: Optional[str], |
| 48 | + custom_summary: Optional[NlpCustomSummary], |
| 49 | +) -> Any: |
| 50 | + params: Dict[str, Any] = {"token": client.token} |
| 51 | + if fields is not None: |
| 52 | + params["fields"] = fields if isinstance(fields, str) else ",".join(fields) |
| 53 | + document: Dict[str, Any] = {"lang": lang, "format": format, "content": text} |
| 54 | + if document_type is not None: |
| 55 | + document["documentType"] = document_type |
| 56 | + if custom_summary is not None: |
| 57 | + max_sentences = custom_summary.get("max_number_of_sentences") |
| 58 | + if max_sentences is not None: |
| 59 | + document["customSummary"] = {"maxNumberOfSentences": max_sentences} |
| 60 | + return params, [document] |
| 61 | + |
| 62 | + |
| 63 | +def nlp( |
| 64 | + client: "Diffbot", |
| 65 | + text: str, |
| 66 | + *, |
| 67 | + fields: Optional[Sequence[NlpField]] = None, |
| 68 | + lang: str = "auto", |
| 69 | + format: NlpFormat = "plain text", |
| 70 | + document_type: Optional[str] = None, |
| 71 | + custom_summary: Optional[NlpCustomSummary] = None, |
| 72 | +) -> NlpResponse: |
| 73 | + params, payload = _build_nlp_request(client, text, fields, lang, format, document_type, custom_summary) |
| 74 | + response = client.request("POST", client.nlp_url, params=params, json=payload) |
| 75 | + data = response.json() |
| 76 | + return data[0] if isinstance(data, list) else data |
| 77 | + |
| 78 | + |
| 79 | +async def nlp_async( |
| 80 | + client: "DiffbotAsync", |
| 81 | + text: str, |
| 82 | + *, |
| 83 | + fields: Optional[Sequence[NlpField]] = None, |
| 84 | + lang: str = "auto", |
| 85 | + format: NlpFormat = "plain text", |
| 86 | + document_type: Optional[str] = None, |
| 87 | + custom_summary: Optional[NlpCustomSummary] = None, |
| 88 | +) -> NlpResponse: |
| 89 | + params, payload = _build_nlp_request(client, text, fields, lang, format, document_type, custom_summary) |
| 90 | + response = await client.request("POST", client.nlp_url, params=params, json=payload) |
| 91 | + data = response.json() |
| 92 | + return data[0] if isinstance(data, list) else data |
0 commit comments