AINLP04 – Detect language
11 mins read

AINLP04 – Detect language

(Source: https://learn.microsoft.com/en-us/training/modules/analyze-text-ai-language/3-detect-language)

The Azure AI Language detection API evaluates text input and, for each document submitted, returns language identifiers with a score indicating the strength of the analysis.

This capability is useful for content stores that collect arbitrary text, where language is unknown. Another scenario could involve a chat bot. If a user starts a session with the chat bot, language detection can be used to determine which language they are using and allow you to configure your bot responses in the appropriate language.

You can parse the results of this analysis to determine which language is used in the input document. The response also returns a score, which reflects the confidence of the model (a value between 0 and 1).

Language detection can work with documents or single phrases. It’s important to note that the document size must be under 5,120 characters. The size limit is per document and each collection is restricted to 1,000 items (IDs).

A sample of a properly formatted JSON payload that you might submit to the service in the request body is shown here, including a collection of documents, each containing a unique id and the text to be analyzed. Optionally, you can provide a countryHint to improve prediction performance.


API Nhận diện Ngôn ngữ của Azure AI (The Azure AI Language detection API) đánh giá đầu vào văn bản (evaluates text input) và, đối với mỗi tài liệu được gửi, trả về mã định danh ngôn ngữ (language identifiers) cùng với một điểm số cho biết độ mạnh của phân tích (indicating the strength of the analysis).

Khả năng này hữu ích cho các kho lưu trữ nội dung (content stores) dùng để thu thập văn bản bất kỳ (arbitrary text), nơi ngôn ngữ không được biết trước (is unknown).

Một kịch bản khác (Another scenario) đó là liên quan (involve) đến chatbot. Nếu người dùng bắt đầu phiên làm việc (starts a session) với chatbot, việc nhận diện ngôn ngữ (language detection) có thể được sử dụng để xác định ngôn ngữ họ đang sử dụng và cho phép bạn cấu hình phản hồi của bot (configure your bot responses) bằng ngôn ngữ phù hợp (in the appropriate language).

Bạn có thể phân tích cú pháp (parse) kết quả của phân tích này để xác định ngôn ngữ nào (determine which language) được sử dụng trong tài liệu đầu vào (the input document). Phản hồi cũng trả về một điểm số (a score), phản ánh mức độ tin cậy của mô hình (reflects the confidence of the model) (giá trị từ 0 đến 1).

Nhận diện ngôn ngữ (Language detection) có thể hoạt động với các tài liệu (documents) hoặc cụm từ đơn lẻ (single phrases). Điều quan trọng cần lưu ý là kích thước tài liệu phải dưới 5,120 ký tự (5,120 characters). Giới hạn kích thước là trên mỗi tài liệu và mỗi bộ sưu tập bị giới hạn (is restricted to) ở 1,000 mục (IDs) – 1,000 items (IDs).

Một ví dụ về mẫu payload JSON được định dạng đúng (a properly formatted JSON payload) mà bạn có thể gửi tới dịch vụ (submit to the service) trong phần thân yêu cầu (the request body) được hiển thị dưới đây, bao gồm một bộ sưu tập các tài liệu (a collection of documents), mỗi tài liệu chứa một id duy nhất (a unique id) và văn bản cần phân tích (the text to be analyzed). Ngoài ra, bạn có thể cung cấp countryHint để cải thiện hiệu suất dự đoán (improve prediction performance).

{
    "kind": "LanguageDetection",
    "parameters": {
        "modelVersion": "latest"
    },
    "analysisInput":{
        "documents":[
              {
                "id": "1",
                "text": "Hello world",
                "countryHint": "US"
              },
              {
                "id": "2",
                "text": "Bonjour tout le monde"
              }
        ]
    }
}

The service will return a JSON response that contains a result for each document in the request body, including the predicted language and a value indicating the confidence level of the prediction. The confidence level is a value ranging from 0 to 1 with values closer to 1 being a higher confidence level. Here’s an example of a standard JSON response that maps to the above request JSON.


Dịch vụ sẽ trả về phản hồi JSON (a JSON response) chứa kết quả cho mỗi tài liệu (a result for each document) trong phần thân yêu cầu (the request body), bao gồm ngôn ngữ dự đoán (the predicted language) và một giá trị cho biết mức độ tin cậy (indicating the confidence level of) của dự đoán (the prediction). Mức độ tin cậy là một giá trị từ 0 đến 1, với các giá trị càng gần 1 thì mức độ tin cậy càng cao (a higher confidence level). Dưới đây là một ví dụ về phản hồi JSON chuẩn tương ứng (a standard JSON response) với yêu cầu JSON trên.

{   "kind": "LanguageDetectionResults",
    "results": {
        "documents": [
          {
            "detectedLanguage": {
              "confidenceScore": 1,
              "iso6391Name": "en",
              "name": "English"
            },
            "id": "1",
            "warnings": []
          },
          {
            "detectedLanguage": {
              "confidenceScore": 1,
              "iso6391Name": "fr",
              "name": "French"
            },
            "id": "2",
            "warnings": []
          }
        ],
        "errors": [],
        "modelVersion": "2022-10-01"
    }
}

In our sample, all of the languages show a confidence of 1, mostly because the text is relatively simple and easy to identify the language for.

If you pass in a document that has multilingual content, the service will behave a bit differently. Mixed language content within the same document returns the language with the largest representation in the content, but with a lower positive rating, reflecting the marginal strength of that assessment. In the following example, the input is a blend of English, Spanish, and French. The analyzer uses statistical analysis of the text to determine the predominant language.


Trong ví dụ của chúng ta, tất cả các ngôn ngữ đều hiển thị độ tin cậy là 1 (a confidence of 1), chủ yếu vì văn bản khá đơn giản (is relatively simple) và dễ dàng xác định (easy to identify) ngôn ngữ.

Nếu bạn gửi một tài liệu có nội dung đa ngôn ngữ (multilingual content), dịch vụ sẽ hoạt động hơi khác (will behave a bit differently). Nội dung ngôn ngữ hỗn hợp (Mixed language content) trong cùng một tài liệu sẽ trả về ngôn ngữ xuất hiện nhiều nhất (with the largest representation) trong nội dung, nhưng với mức độ tin cậy thấp hơn (a lower positive rating), phản ánh độ mạnh yếu (reflecting the marginal strength of) của đánh giá đó (that assessment).

Trong ví dụ sau, đầu vào là sự pha trộn giữa tiếng Anh, tiếng Tây Ban Nha và tiếng Pháp. Trình phân tích sử dụng phân tích thống kê của văn bản (uses statistical analysis of the text) để xác định ngôn ngữ chiếm ưu thế (to determine the predominant language).

{
  "documents": [
    {
      "id": "1",
      "text": "Hello, I would like to take a class at your University. ¿Se ofrecen clases en español? Es mi primera lengua y más fácil para escribir. Que diriez-vous des cours en français?"
    }
  ]
}

The following sample shows a response for this multi-language example.

{
    "documents": [
        {
            "id": "1",
            "detectedLanguage": {
                "name": "Spanish",
                "iso6391Name": "es",
                "confidenceScore": 0.9375
            },
            "warnings": []
        }
    ],
    "errors": [],
    "modelVersion": "2022-10-01"
}

The last condition to consider is when there is ambiguity as to the language content. The scenario might happen if you submit textual content that the analyzer is not able to parse, for example because of character encoding issues when converting the text to a string variable. As a result, the response for the language name and ISO code will indicate (unknown) and the score value will be returned as 0. The following example shows how the response would look.


Điều kiện cuối cùng cần xem xét là khi có sự mơ hồ (ambiguity) về nội dung ngôn ngữ. Kịch bản này có thể xảy ra nếu (The scenario might happen if)bạn gửi nội dung văn bản mà trình phân tích không thể phân tích được (is not able to parse), ví dụ do các vấn đề về mã hóa ký tự (character encoding issues) khi chuyển đổi văn bản thành biến chuỗi (converting the text to a string variable). Kết quả là phản hồi cho tên ngôn ngữ và mã ISO sẽ chỉ ra (không xác định) (indicate (unknown))và giá trị điểm số sẽ được trả về là 0. Ví dụ sau đây cho thấy cách phản hồi sẽ trông như thế nào.

{
    "documents": [
        {
            "id": "1",
            "detectedLanguage": {
                "name": "(Unknown)",
                "iso6391Name": "(Unknown)",
                "confidenceScore": 0.0
            },
            "warnings": []
        }
    ],
    "errors": [],
    "modelVersion": "2022-10-01"
}

Leave a Reply

Your email address will not be published. Required fields are marked *