Skip to content

Commit c16fac7

Browse files
Feature: detect
1 parent 38c6321 commit c16fac7

4 files changed

Lines changed: 140 additions & 2 deletions

File tree

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,19 @@ cd examples
114114
python glossaries_management.py
115115
```
116116

117+
### Language Detection
118+
- **[language_detection.py](examples/language_detection.py)** - Language detection examples
119+
- Single string detection
120+
- Multiple strings detection
121+
- Detection with hint parameter
122+
- Detection with passlist to restrict languages
123+
- Combined hint and passlist
124+
125+
```bash
126+
cd examples
127+
python language_detection.py
128+
```
129+
117130
## 🔧 API Reference
118131

119132
### Core Components
@@ -295,6 +308,34 @@ csv_data = lara.glossaries.export("gls_1A2b3C4d5E6f7G8h9I0jKl", "csv/table-uni",
295308
counts = lara.glossaries.counts("gls_1A2b3C4d5E6f7G8h9I0jKl")
296309
```
297310

311+
### 🌐 Language Detection
312+
313+
```python
314+
# Basic language detection
315+
result = lara.detect("Hello, world!")
316+
print(f"Detected language: {result.language}")
317+
print(f"Content type: {result.content_type}")
318+
319+
# Detect multiple strings
320+
result = lara.detect(["Hello", "Bonjour", "Hola"])
321+
322+
# Detection with hint
323+
result = lara.detect("Hello", hint="en")
324+
325+
# Detection with passlist (restrict to specific languages)
326+
result = lara.detect(
327+
"Guten Tag",
328+
passlist=["de-DE", "en-US", "fr-FR"]
329+
)
330+
331+
# Combined hint and passlist
332+
result = lara.detect(
333+
"Buongiorno",
334+
hint="it",
335+
passlist=["it-IT", "es-ES", "pt-PT"]
336+
)
337+
```
338+
298339
### Translation Options
299340

300341
```python

examples/language_detection.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from lara_sdk import Credentials, Translator
2+
import os
3+
4+
"""
5+
Language detection examples for the Lara Python SDK
6+
7+
This example demonstrates:
8+
- Detecting language of a single string
9+
- Detecting language of multiple strings
10+
- Using hint parameter to improve detection
11+
- Using passlist to restrict detected languages
12+
"""
13+
14+
def main():
15+
# All examples can use environment variables for credentials:
16+
# export LARA_ACCESS_KEY_ID="your-access-key-id"
17+
# export LARA_ACCESS_KEY_SECRET="your-access-key-secret"
18+
# Falls back to placeholders if not set
19+
access_key_id = os.getenv("LARA_ACCESS_KEY_ID", "your-access-key-id")
20+
access_key_secret = os.getenv("LARA_ACCESS_KEY_SECRET", "your-access-key-secret")
21+
22+
credentials = Credentials(access_key_id, access_key_secret)
23+
lara = Translator(credentials)
24+
25+
try:
26+
# Example 1: Detect language of a single string
27+
print("=== Single String Detection ===")
28+
result1 = lara.detect("Bonjour, comment allez-vous?")
29+
print("Text: Bonjour, comment allez-vous?")
30+
print(f"Detected language: {result1.language}")
31+
print(f"Content type: {result1.content_type}\n")
32+
33+
# Example 2: Detect language of multiple strings
34+
print("=== Multiple Strings Detection ===")
35+
texts = [
36+
"Hello, how are you?",
37+
"Hola, ¿cómo estás?",
38+
"Ciao, come stai?"
39+
]
40+
result2 = lara.detect(texts)
41+
print(f"Texts: {texts}")
42+
print(f"Detected language: {result2.language}")
43+
print(f"Content type: {result2.content_type}\n")
44+
45+
# Example 3: Using hint parameter
46+
print("=== Detection with Hint ===")
47+
result3 = lara.detect("Hello", hint="en")
48+
print("Text: Hello")
49+
print(f"Hint: en")
50+
print(f"Detected language: {result3.language}")
51+
print(f"Content type: {result3.content_type}\n")
52+
53+
# Example 4: Using passlist to restrict detected languages
54+
print("=== Detection with Passlist ===")
55+
result4 = lara.detect(
56+
"Guten Tag",
57+
passlist=["de-DE", "en-US", "fr-FR"]
58+
)
59+
print("Text: Guten Tag")
60+
print(f"Passlist: ['de-DE', 'en-US', 'fr-FR']")
61+
print(f"Detected language: {result4.language}")
62+
print(f"Content type: {result4.content_type}\n")
63+
64+
# Example 5: Combined hint and passlist
65+
print("=== Detection with Hint and Passlist ===")
66+
result5 = lara.detect(
67+
"Buongiorno",
68+
hint="it",
69+
passlist=["it-IT", "es-ES", "pt-PT"]
70+
)
71+
print("Text: Buongiorno")
72+
print(f"Hint: it")
73+
print(f"Passlist: ['it-IT', 'es-ES', 'pt-PT']")
74+
print(f"Detected language: {result5.language}")
75+
print(f"Content type: {result5.content_type}\n")
76+
77+
except Exception as error:
78+
print(f"Error: {error}")
79+
80+
if __name__ == "__main__":
81+
main()

src/lara_sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from ._client import LaraObject
22
from ._credentials import Credentials
33
from ._errors import LaraApiError, LaraError
4-
from ._translator import Memory, MemoryImport, TextBlock, TextResult, Memories, Translator, TranslatePriority, UseCache, Documents, Document, DocumentStatus, DocxExtractionParams, DocumentExtractionParams
4+
from ._translator import Memory, MemoryImport, TextBlock, TextResult, DetectResult, Memories, Translator, TranslatePriority, UseCache, Documents, Document, DocumentStatus, DocxExtractionParams, DocumentExtractionParams
55

66
# This constant is auto-generated by the build script.
77
# Manual modifications will be overwritten and may cause unexpected behavior.

src/lara_sdk/_translator.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ def __init__(self, **kwargs):
141141
self.translation = [TextBlock(**e) for e in translation]
142142

143143

144+
class DetectResult(LaraObject):
145+
def __init__(self, **kwargs):
146+
self.language: str = kwargs.get('language')
147+
self.content_type: str = kwargs.get('content_type')
148+
149+
144150
# Translator SDK -------------------------------------------------------------------------------------------------------
145151

146152

@@ -455,4 +461,14 @@ def translate(self, text: Union[str, Iterable[str], Iterable[TextBlock]], *,
455461
if no_trace is True:
456462
request_headers['X-No-Trace'] = 'true'
457463

458-
return TextResult(**self._client.post('/translate', body, headers=request_headers))
464+
return TextResult(**self._client.post('/translate', body, headers=request_headers))
465+
466+
def detect(self, text: Union[str, List[str]], *, hint: Optional[str] = None,
467+
passlist: Optional[List[str]] = None) -> DetectResult:
468+
body = {
469+
'q': text,
470+
'hint': hint,
471+
'passlist': passlist
472+
}
473+
474+
return DetectResult(**self._client.post('/detect', body))

0 commit comments

Comments
 (0)