|
| 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() |
0 commit comments