TextLanguageRouter
Route text strings to different pipeline branches based on their detected language.
Key Features
- Detects the language of a text string using
langdetect. - Routes text to a named output matching the detected language.
- Supports multiple languages configured at initialization.
- Routes text that doesn't match any configured language to an
unmatchedoutput.
Configuration
- Drag the
TextLanguageRoutercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set Languages to the list of ISO 639-1 language codes you want to route (for example,
["en", "de", "fr"]). If not specified, defaults to["en"]. See the supported languages in thelangdetectdocumentation.
- Set Languages to the list of ISO 639-1 language codes you want to route (for example,
Connections
TextLanguageRouter receives a single text string. For each language you configure, it creates a named output connection (for example, en or de). Text that doesn't match any configured language is routed to the unmatched output. Connect each language output to the appropriate downstream component.
To route documents (not text strings) by language, use DocumentLanguageClassifier followed by MetadataRouter.
Source Code
To check this component's source code, open text_language_router.py in the Haystack repository.
Usage Examples
Basic Configuration
TextLanguageRouter:
type: components.routers.text_language_router.TextLanguageRouter
init_parameters: {}
To route documents by language instead of raw text, use DocumentLanguageClassifier followed by MetadataRouter.
components:
TextLanguageRouter:
type: components.routers.text_language_router.TextLanguageRouter
init_parameters:
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
text | str | A text string to route. |
Outputs
| Parameter | Type | Description |
|---|---|---|
| (per language code) | str | The text routed to the output matching the detected language. |
unmatched | str | The text when its language doesn't match any configured language. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
languages | Optional[List[str]] | None | A list of ISO 639-1 language codes. See the supported languages in the langdetect documentation. If not specified, defaults to ["en"]. |
Run Method Parameters
These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.
| Parameter | Type | Default | Description |
|---|---|---|---|
text | str | A text string to route. |
Was this page helpful?