Struct LanguageDetector

Source
pub struct LanguageDetector { /* private fields */ }
Expand description

A thread-safe struct for detecting the language of a given text.

Implementations§

Source§

impl LanguageDetector

Source

pub fn new() -> Self

Creates a new instance of LanguageDetector.

This constructor initializes the detector with predefined language detection patterns for multiple languages, allowing for quick detection based on common words and script patterns.

§Returns
  • LanguageDetector - A new instance of the LanguageDetector struct.
§Examples
use langweave::language_detector::LanguageDetector;
use whatlang::Lang;

let detector = LanguageDetector::new();
assert_eq!(detector.convert_lang_code(Lang::Eng), "en");
assert_eq!(detector.convert_lang_code(Lang::Fra), "fr");
assert_eq!(detector.convert_lang_code(Lang::Deu), "de");
Source

pub fn convert_lang_code(&self, lang: Lang) -> String

Converts whatlang’s language codes to the desired format.

This function maps whatlang’s internal Lang enum values to their ISO 639-1 equivalents or other common language codes used by the application.

§Arguments
  • lang - The Lang enum from whatlang.
§Returns
  • String - The standardized language code (e.g., “en”, “fr”).
§Examples
use langweave::language_detector::LanguageDetector;
use whatlang::Lang;

let detector = LanguageDetector::new();
assert_eq!(detector.convert_lang_code(Lang::Eng), "en");
assert_eq!(detector.convert_lang_code(Lang::Fra), "fr");
assert_eq!(detector.convert_lang_code(Lang::Deu), "de");

Trait Implementations§

Source§

impl Clone for LanguageDetector

Source§

fn clone(&self) -> LanguageDetector

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for LanguageDetector

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for LanguageDetector

Source§

fn default() -> Self

Provides a default instance of LanguageDetector.

§Returns
  • LanguageDetector - A default instance using predefined patterns for common languages.

This method allows the LanguageDetector to be initialized easily with default patterns.

§Examples
use langweave::language_detector::LanguageDetector;
use langweave::language_detector_trait::LanguageDetectorTrait;

let detector = LanguageDetector::default();
let result = detector.detect("Hello, world!");
assert_eq!(result.unwrap(), "en");
Source§

impl LanguageDetectorTrait for LanguageDetector

Source§

fn detect(&self, text: &str) -> Result<String, I18nError>

Detects the language of the given text synchronously.

This method first attempts to detect the language using custom regular expression patterns for common words. If no match is found, it falls back to whatlang for statistical detection.

§Arguments
  • text - A string slice that holds the text to analyze.
§Returns
  • Result<String, I18nError> - The detected language code if successful, or an error if detection fails.
§Examples
use langweave::language_detector::LanguageDetector;
use langweave::language_detector_trait::LanguageDetectorTrait;

let detector = LanguageDetector::new();

// Detect English
let result = detector.detect("The quick brown fox");
assert_eq!(result.unwrap(), "en");

// Detect French
let result = detector.detect("Le chat noir");
assert_eq!(result.unwrap(), "fr");

// Detect German
let result = detector.detect("Der schnelle braune Fuchs");
assert_eq!(result.unwrap(), "de");
§Errors

This function will return an I18nError::LanguageDetectionFailed if:

  • The input text is empty or contains only non-alphabetic characters.
  • The language detection process fails to identify a language with sufficient confidence.
Source§

fn detect_async<'life0, 'life1, 'async_trait>( &'life0 self, text: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<String, I18nError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Detects the language of the given text asynchronously.

This method provides the same functionality as detect, but operates asynchronously, allowing for non-blocking language detection in concurrent contexts.

§Arguments
  • text - A string slice that holds the text to analyze.
§Returns
  • Result<String, I18nError> - The detected language code if successful, or an error if detection fails.
§Examples
use langweave::language_detector::LanguageDetector;
use langweave::language_detector_trait::LanguageDetectorTrait;

#[tokio::main]
async fn main() {
    let detector = LanguageDetector::new();

    // Detect Spanish asynchronously
    let result = detector.detect_async("El gato negro").await;
    assert_eq!(result.unwrap(), "es");

    // Detect Portuguese asynchronously
    let result = detector.detect_async("O gato preto").await;
    assert_eq!(result.unwrap(), "pt");
}
§Errors

This function will return an I18nError::LanguageDetectionFailed if:

  • The input text is empty or contains only non-alphabetic characters.
  • The language detection process fails to identify a language with sufficient confidence.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.