These examples demonstrate how to detect the language to classify the text of quotes selected by different employees stored in a TeradataML DataFrame. Refer to TextAnalyticsAI Example Setup for the prerequisite steps.
obj_aws.detect_language(column="quotes",data=data,volatile=True)
Output:
| employee_id | employee_name | employee_data | articles | reviews | quotes | Language |
|---|---|---|---|---|---|---|
| 1 | Parker Doe | Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | Climate change poses significant challenges globally affecting weather patterns ecosystems and human | Todays food delivery was quicker than yesterdays Appreciated it | El tiempo es oro | Spanish \n |
| 4 | Michael Brown | Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | The recent 2020 United States election have shown a shift in the political landscape With more youn | Both the food and the delivery service were topnotch | Geniet van de kleine dingen. | Dutch \n |
| 2 | Alex Smith | Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | The food was excellent but it arrived a bit late | Apres la pluie le beau temps | French \n |
| 3 | Emily Johnson | Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | The delivery was prompt but the food was spilled and the portion size was small | La vie est belle | French |
| 5 | Linda Taylor | Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | The food was average and the delivery person was rude | Wees de verandering die je in de wereld wil zien gebereuren | Dutch \n |
obj_azure.detect_language(column="quotes",data=data,volatile=True)
Output:
| employee_id | employee_name | employee_data | articles | reviews | quotes | Language |
|---|---|---|---|---|---|---|
| 1 | Parker Doe | Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | Climate change poses significant challenges globally affecting weather patterns ecosystems and human | Todays food delivery was quicker than yesterdays Appreciated it | El tiempo es oro | Spanish \n |
| 4 | Michael Brown | Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | The recent 2020 United States election have shown a shift in the political landscape With more youn | Both the food and the delivery service were topnotch | Geniet van de kleine dingen. | Dutch \n |
| 2 | Alex Smith | Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | The food was excellent but it arrived a bit late | Apres la pluie le beau temps | French \n |
| 3 | Emily Johnson | Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | The delivery was prompt but the food was spilled and the portion size was small | La vie est belle | French |
| 5 | Linda Taylor | Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | The food was average and the delivery person was rude | Wees de verandering die je in de wereld wil zien gebereuren | Dutch \n |
obj_gcp.detect_language(column="quotes",data=data,volatile=True)
Output:
| employee_id | employee_name | employee_data | articles | reviews | quotes | Language |
|---|---|---|---|---|---|---|
| 1 | Parker Doe | Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | Climate change poses significant challenges globally affecting weather patterns ecosystems and human | Todays food delivery was quicker than yesterdays Appreciated it | El tiempo es oro | Spanish \n |
| 4 | Michael Brown | Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | The recent 2020 United States election have shown a shift in the political landscape With more youn | Both the food and the delivery service were topnotch | Geniet van de kleine dingen. | Dutch \n |
| 2 | Alex Smith | Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | The food was excellent but it arrived a bit late | Apres la pluie le beau temps | French \n |
| 3 | Emily Johnson | Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | The delivery was prompt but the food was spilled and the portion size was small | La vie est belle | French |
| 5 | Linda Taylor | Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | The food was average and the delivery person was rude | Wees de verandering die je in de wereld wil zien gebereuren | Dutch \n |
Example setup: Import required packages and set up input data
>>> import os
>>> import teradatagenai
>>> from teradatagenai import TeradataAI, TextAnalyticsAI, load_data
>>> from teradataml import DataFrame
>>> load_data('employee', 'employee_data')
>>> data = DataFrame('employee_data')
>>> df_quotes = data.select(["employee_id", "employee_name", "quotes"])
Complete the example setup requirements.
Example 1: Detect the language of text in the 'quotes' column of a teradataml DataFrame using hugging face model 'xlm-roberta-base-language-detection'
The text for language detection is passed as a column name along with the teradataml DataFrame. A specific language is passed in the 'language' argument.
Create an LLM endpoint.
>>> model_name = 'papluca/xlm-roberta-base-language-detection'
>>> model_args = {'transformer_class': 'AutoModelForSequenceClassification',
'task' : 'text-classification'}
>>> ues_args = {'env_name' : 'demo_env'}
>>> llm = TeradataAI(api_type = "hugging_face",
model_name = model_name,
model_args = model_args,
ues_args = ues_args)
Create a TextAnalyticsAI object.
>>> obj = TextAnalyticsAI(llm = llm)
Detect the language of the 'quotes' column in the 'df_quotes' teradataml DataFrame.
>>> obj.detect_language(column = "quotes",
data = df_quotes,
delimiter = "#")
Output:
| text | language |
|---|---|
| Apres la pluie le beau temps | [{'label': 'fr', 'score': 0.9922717809677124}] |
| El tiempo es oro | [{'label': 'es', 'score': 0.9926148653030396}] |
| Wees de verandering die je in de wereld wil zien gebereuren | [{'label': 'nl', 'score': 0.9953699707984924}] |
| La vie est belle | [{'label': 'fr', 'score': 0.9304302930831909}] |
| Geniet van de kleine dingen. | [{'label': 'nl', 'score': 0.995890736579895}] |
Example 2: Extend Example 1 to use default script with 'output_labels' to format the output
>>> obj.detect_language(column = 'quotes',
data = df_quotes,
output_labels = {'label': str, 'score': float},
delimiter = "#")
Output:
| text | label | score |
|---|---|---|
| Apres la pluie le beau temps | fr | 0.9922717809677124 |
| Wees de verandering die je in de wereld wil zien gebereuren | nl | 0.9953699707984924 |
| La vie est belle | fr | 0.9304302930831909 |
| El tiempo es oro | es | 0.9926148653030396 |
| Geniet van de kleine dingen. | nl | 0.995890736579895 |
Example 3: Extend Example 2 to use user defined script for inference
>>> base_dir = os.path.dirname(teradatagenai.__file__)
>>> language_detection_script = os.path.join(base_dir, 'example-data',
'detect_language.py')
>>> obj.detect_language(column = 'quotes',
data = df_quotes,
script = language_detection_script,
delimiter = "#")
Output:
| text | language |
|---|---|
| Apres la pluie le beau temps | dutch |
| Geniet van de kleine dingen. | french |
| La vie est belle | spanish |
| El tiempo es oro | french |
| Wees de verandering die je in de wereld wil zien gebereuren | dutch |
obj_nim.detect_language(column="quotes",data=data,volatile=True)
Output:
| employee_id | employee_name | employee_data | articles | reviews | quotes | Language |
|---|---|---|---|---|---|---|
| 5 | Linda Taylor | Linda Taylor a German cleared all their loans by 2020 12 31 They can be reached at 555 555 5559 Thei | The 2019 Amazon rainforest wildfires were a severe environmental crisis The fires burned thousands o | The food was average and the delivery person was rude | Wees de verandering die je in de wereld wil zien gebereuren | Dutch |
| 1 | Parker Doe | Parker Doe originally from Brazil has successfully cleared all their loans by 2020 04 25 Reach them | Climate change poses significant challenges globally affecting weather patterns ecosystems and human | Todays food delivery was quicker than yesterdays Appreciated it | El tiempo es oro | Spanish |
| 3 | Emily Johnson | Emily Johnson from the UK cleared all their loans by 2021 01 15 They can be contacted at 555 555 555 | Renewable energy sources such as solar wind and hydroelectric power play a crucial role in reducing | The delivery was prompt but the food was spilled and the portion size was small | La vie est belle | French |
| 4 | Michael Brown | Michael Brown an Australian has a loan due on 2023 07 20 Contact them at 555 555 5558 Their SSN is 6 | The recent 2020 United States election have shown a shift in the political landscape With more youn | Both the food and the delivery service were topnotch | Geniet van de kleine dingen. | Dutch |
| 2 | Alex Smith | Alex Smith a Canadian has an outstanding loan due on 2022 05 30 Their contact number is 555 555 5556 | The 2020 Tokyo Olympics was postponed to 2021 due to the COVID-19 pandemic. This was the first time i | The food was excellent but it arrived a bit late | Apres la pluie le beau temps | French |