### Batch Process Documents with Whichlang Source: https://context7.com/quickwit-oss/whichlang/llms.txt Process multiple text documents efficiently and count them by language. This example demonstrates filtering and grouping documents based on their detected language. ```rust use whichlang::{detect_language, Lang}; use std::collections::HashMap; fn main() { // Process a batch of documents and count by language let documents = vec![ "Hello world, this is an English document", "Bonjour, ceci est un document français", "Hallo, dies ist ein deutsches Dokument", "Another English text here", "Ciao, questo è un documento italiano", "Hola, este es un documento en español", "More English content follows", "日本語のドキュメントです", "这是一个中文文档", "한국어 문서입니다", ]; // Count documents per language let mut lang_counts: HashMap = HashMap::new(); for doc in &documents { let lang = detect_language(doc); *lang_counts.entry(lang).or_insert(0) += 1; } // Print statistics println!("Language distribution:"); for (lang, count) in &lang_counts { println!(" {{}}: {{}} documents", lang.three_letter_code(), count); } // Output: // Language distribution: // eng: 3 documents // fra: 1 documents // deu: 1 documents // ita: 1 documents // spa: 1 documents // jpn: 1 documents // cmn: 1 documents // kor: 1 documents // Filter documents by language let english_docs: Vec<&&str> = documents.iter() .filter(|doc| detect_language(doc) == Lang::Eng) .collect(); println!("\nEnglish documents: {{}}", english_docs.len()); // Output: 3 // Group documents by language let mut grouped: HashMap> = HashMap::new(); for doc in &documents { let lang = detect_language(doc); grouped.entry(lang).or_default().push(doc); } for (lang, docs) in &grouped { println!("\n{{}} documents:", lang.three_letter_code().to_uppercase()); for doc in docs { println!(" - {{}}", &doc[..doc.len().min(50)]); } } } ``` -------------------------------- ### Detect Language in Rust Project Source: https://context7.com/quickwit-oss/whichlang/llms.txt Example usage in your project to detect the language of multiple text documents. It maps each text to its detected language using `detect_language` and returns a vector of tuples. ```rust use whichlang::{detect_language, Lang}; fn process_multilingual_content(texts: &[&str]) -> Vec<(String, Lang)> { texts.iter() .map(|text| (text.to_string(), detect_language(text))) .collect() } fn main() { let documents = vec![ "The quick brown fox jumps over the lazy dog", "Le renard brun rapide saute par-dessus le chien paresseux", "Der schnelle braune Fuchs springt über den faulen Hund", "日本語のテキストです", "Привет мир", ]; for (text, lang) in process_multilingual_content(&documents) { println!("[{{}}] {{}}", lang.three_letter_code(), &text[..text.len().min(40)]); } // Output: // [eng] The quick brown fox jumps over the lazy // [fra] Le renard brun rapide saute par-dessus // [deu] Der schnelle braune Fuchs springt über // [jpn] 日本語のテキストです // [rus] Привет мир } ``` -------------------------------- ### Lang Enum and Supported Languages Source: https://context7.com/quickwit-oss/whichlang/llms.txt The `Lang` enum represents all 16 supported languages with ISO 639-3 codes. Use the `three_letter_code()` method to get the string representation. The `LANGUAGES` constant provides a convenient way to iterate over all supported languages. ```rust use whichlang::{Lang, LANGUAGES}; fn main() { // All supported language variants let languages = [ Lang::Ara, // Arabic Lang::Cmn, // Mandarin Chinese Lang::Deu, // German Lang::Eng, // English Lang::Fra, // French Lang::Hin, // Hindi Lang::Ita, // Italian Lang::Jpn, // Japanese Lang::Kor, // Korean Lang::Nld, // Dutch Lang::Por, // Portuguese Lang::Rus, // Russian Lang::Spa, // Spanish Lang::Swe, // Swedish Lang::Tur, // Turkish Lang::Vie, // Vietnamese ]; // Convert to three-letter code strings for lang in languages { let code = lang.three_letter_code(); println!("{:?} -> {}", lang, code); } // Output: // Ara -> ara // Cmn -> cmn // Deu -> deu // Eng -> eng // Fra -> fra // Hin -> hin // Ita -> ita // Jpn -> jpn // Kor -> kor // Nld -> nld // Por -> por // Rus -> rus // Spa -> spa // Swe -> swe // Tur -> tur // Vie -> vie // Use LANGUAGES constant to iterate all supported languages println!("Total supported languages: {}", LANGUAGES.len()); // Output: 16 // Lang implements Copy, Clone, Debug, Eq, PartialEq let lang1 = Lang::Eng; let lang2 = lang1; // Copy assert_eq!(lang1, lang2); // Use in match expressions let detected = whichlang::detect_language("Bonjour le monde"); match detected { Lang::Fra => println!("French detected!"), Lang::Eng => println!("English detected!"), _ => println!("Other language: {}", detected.three_letter_code()), } // Output: French detected! } ``` -------------------------------- ### Generate Rust Weights File Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This script generates a Rust file ('src/weights.rs') containing language enum, constants for languages, weights, and intercepts from a trained model. Ensure 'model', 'np', and 'coef' are properly defined. ```python from sklearn import metrics print(model.classes_) print(sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['deu', 'eng', 'fra', 'ita','nld', 'por', 'rus', 'spa'])) ``` ```python sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['kor', 'jpn', 'cmn']) ``` ```python from sklearn import metrics print(model.classes_) print(sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['deu', 'eng', 'fra', 'ita','nld', 'por', 'rus', 'spa'])) ``` ```python sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['kor', 'jpn', 'cmn']) ``` ```python from sklearn import metrics print(model.classes_) print(sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['deu', 'eng', 'fra', 'ita','nld', 'por', 'rus', 'spa'])) ``` ```python sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['kor', 'jpn', 'cmn']) ``` ```python (LANG, DIM) = model.coef_.shape print(model.coef_.shape) coef = np.float32(model.coef_) print(coef[0,0]) print(model.coef_[:,0]) f = open("src/weights.rs", "w") f.write("#[derive(Clone, Copy, Debug, Eq, PartialEq)]\n") f.write("pub enum Lang { ") for lang in model.classes_: f.write(" %s,\n" % lang.capitalize()) f.write("} ") f.write("\nimpl Lang {\n pub fn three_letter_code(self)-> &'static str {\n match self {\n") for lang in model.classes_: f.write(" Lang::%s => \"%s\",\n" % (lang.capitalize(), lang)) f.write(" } } } ") f.write("pub const LANGUAGES: [Lang; %d] = [\n\t" % LANG) for lang in model.classes_: f.write("Lang::%s, " % lang.capitalize()) f.write("];\n\n") f.write("pub const WEIGHTS: [f32; %d] = [\n" % (LANG * DIM)) for i in range(DIM): f.write(" ") for val in coef[:, i]: f.write("%f, " % val) f.write("\n") f.write("];\n\n") f.write("pub const INTERCEPTS: [f32; %d] = [\n\t" % LANG) for val in model.intercept_: f.write("%f, " % val) f.write("];\n\n") f.flush() f.close() ``` -------------------------------- ### Logistic Regression Model Training with Scikit-learn Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This Python snippet demonstrates training a logistic regression model using scikit-learn. It configures the model with specific parameters like max_iter, C, and class_weight, and then fits it to the training data. The output shows the convergence status and accuracy on training and testing sets. ```python for C in [64]: model = sklearn.linear_model.LogisticRegression(max_iter=500, penalty='l2', multi_class='multinomial', C=C, verbose=1, class_weight='balanced') #, l1_ratio=0.1,) # penalty='elasticnet', solver='saga') model.fit(X_train, y_train) print("\n\n\n\n\n----------") print(C) print((model.predict(X_train) == y_train).mean()) print((model.predict(X_test) == y_test).mean()) ``` -------------------------------- ### Split Data into Training and Testing Sets Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb Splits the preprocessed feature matrix (X) and label vector (y) into training and testing sets without shuffling. This is often used when the order of data is important, such as in time-series analysis. ```python (X_train, X_test, y_train, y_test) = train_test_split(X, y, shuffle=False) del X del y n_train = X_train.shape[0] print(X_train.shape, X_test.shape) ``` -------------------------------- ### Generate Confusion Matrix with Specific Labels Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb Use this snippet to generate a confusion matrix for specific language labels. Ensure 'y_test', 'model', and 'X_test' are defined. ```python from sklearn import metrics print(model.classes_) print(sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['deu', 'eng', 'fra', 'ita','nld', 'por', 'rus', 'spa'])) ``` ```python sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['kor', 'jpn', 'cmn']) ``` -------------------------------- ### Import Libraries for Data Processing Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb Imports necessary libraries including NumPy, Scikit-learn, Polars, and SciPy for data manipulation and machine learning tasks. ```python import numpy as np import sklearn from sklearn import linear_model import polars from sklearn.model_selection import train_test_split from scipy import sparse ``` -------------------------------- ### Load and Preprocess CSV Data in Batches Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb Reads a CSV file in batches using Polars, extracts features and labels, converts them to NumPy arrays and sparse matrices, normalizes features, and concatenates batches. This is useful for handling large datasets that do not fit into memory. ```python df_it = polars.read_csv_batched("train.csv", has_header=True) ys = [] Xs = [] idxs = [] for i in range(12): df = df_it.next_batches(1)[0] df[0, 0:10] print(df.shape) y = df[:, 1].to_numpy() ys.append(y) X = sparse.csr_matrix(np.float32(df[:, 2:].to_numpy())) X = sklearn.preprocessing.normalize(X) Xs.append(X) idx = df[:, 0] idxs.append(idx) X = sparse.vstack(Xs) y = np.hstack(ys) del Xs del ys print(X.shape) print(y.shape) ``` -------------------------------- ### Scikit-learn Logistic Regression Convergence Warning Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This output indicates a ConvergenceWarning from scikit-learn's Logistic Regression, suggesting that the optimization algorithm (L-BFGS-B) did not converge within the specified number of iterations. It recommends increasing max_iter or scaling the data. ```text /home/fulmicoton/miniconda3/lib/python3.9/site-packages/sklearn/linear_model/_logistic.py:458: ConvergenceWarning: lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT. Increase the number of iterations (max_iter) or scale the data as shown in: https://scikit-learn.org/stable/modules/preprocessing.html Please also refer to the documentation for alternative solver options: https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression n_iter_i = _check_optimize_result( [Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 8.0min finished ``` -------------------------------- ### Add Whichlang Dependency to Cargo.toml Source: https://context7.com/quickwit-oss/whichlang/llms.txt Add whichlang as a dependency in your Rust project's Cargo.toml file. The library has no external dependencies and compiles quickly. ```toml [dependencies] whichlang = "0.1" ``` -------------------------------- ### Re-evaluation of Confusion Matrix with Specific Labels Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This Python code snippet re-calculates and prints the confusion matrix using scikit-learn's metrics module. It specifies the labels for the matrix, ensuring the output corresponds to the desired language order. ```python from sklearn import metrics print(model.classes_) print(sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['deu', 'eng', 'fra', 'ita','nld', 'por', 'rus', 'spa'])) ``` -------------------------------- ### Language Identification Model Evaluation with Confusion Matrix Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This Python code snippet uses scikit-learn's metrics module to print the predicted classes and generate a confusion matrix for a text classification task. It evaluates the model's performance by comparing true labels (y_test) against predicted labels, with specific language labels provided for the matrix. ```python import time time.sleep(100) print("start")from sklearn import metrics print(model.classes_) print(sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['deu', 'eng', 'fra', 'ita','nld', 'por', 'rus', 'spa'])) sklearn.metrics.confusion_matrix(y_test, model.predict(X_test), labels=['kor', 'jpn', 'cmn']) ``` -------------------------------- ### Character Frequency Analysis for Language Identification Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This Python script analyzes character frequencies in a dataset of sentences to identify common non-ASCII characters across several languages. It's useful for understanding character distribution and potential features for language identification models. ```python from collections import Counter counter = Counter() c = 0 for line in open("dataset/archive/sentences.prepared.csv"): (rid, lang, sentence) = line.strip().split("\t", 2) if lang not in {"fra","eng", "ita", "deu", "esp", "por"}: continue c += 1 if c > 100_000: break for chr in sentence: if ord(chr) > 128: counter[chr] += 1 letters = sorted(ord(letter) for (letter, count) in counter.most_common(100) if count >= 10) print(", ".join(map(str, letters))) #print(letters) ``` -------------------------------- ### Analyze Misclassified Samples Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This code snippet helps identify and display details of misclassified samples. It prints the shape of misclassified samples and iterates through the first 10 to show true label, predicted label, and an index. ```python y_predict = model.predict(X_test) print(np.where((y_predict == y_test) == False)[0].shape) print(y_predict.shape) i = 0 for row in list(np.where((y_predict == y_test) == False))[0]: i += 1 print(y_test[row], y_predict[row], idx[int(n_train + row)]) if i == 10: break ``` -------------------------------- ### Detect Language of Text String Source: https://context7.com/quickwit-oss/whichlang/llms.txt Use the `detect_language` function to identify the language of a given text. Empty strings default to English. This function returns a `Lang` enum variant. ```rust use whichlang::{detect_language, Lang}; fn main() { // English detection let text = "Hello, happy tax payer"; let lang = detect_language(text); assert_eq!(lang, Lang::Eng); println!("Detected language: {:?}", lang); // Output: Detected language: Eng // French detection let french_text = "Bonjour joyeux contribuable"; let french_lang = detect_language(french_text); assert_eq!(french_lang, Lang::Fra); println!("French: {:?}", french_lang); // Output: French: Fra // German detection let german_text = "Hallo glücklicher Steuerzahler"; let german_lang = detect_language(german_text); assert_eq!(german_lang, Lang::Deu); println!("German: {:?}", german_lang); // Output: German: Deu // Japanese detection let japanese_text = "こんにちは幸せな税金納め"; let japanese_lang = detect_language(japanese_text); assert_eq!(japanese_lang, Lang::Jpn); println!("Japanese: {:?}", japanese_lang); // Output: Japanese: Jpn // Mandarin Chinese detection let chinese_text = "你好幸福的纳税人"; let chinese_lang = detect_language(chinese_text); assert_eq!(chinese_lang, Lang::Cmn); println!("Mandarin: {:?}", chinese_lang); // Output: Mandarin: Cmn // Korean detection let korean_text = "안녕하세요 행복한 납세자입니다"; let korean_lang = detect_language(korean_text); assert_eq!(korean_lang, Lang::Kor); println!("Korean: {:?}", korean_lang); // Output: Korean: Kor // Spanish detection let spanish_text = "Hola feliz contribuyente"; let spanish_lang = detect_language(spanish_text); assert_eq!(spanish_lang, Lang::Spa); println!("Spanish: {:?}", spanish_lang); // Output: Spanish: Spa // Empty string defaults to English let empty_lang = detect_language(""); assert_eq!(empty_lang, Lang::Eng); } ``` -------------------------------- ### Confusion Matrix for Specific Languages Source: https://github.com/quickwit-oss/whichlang/blob/main/train.ipynb This output represents a confusion matrix generated by scikit-learn for a multi-class classification problem. The rows typically represent the true labels and columns represent the predicted labels for the specified languages (e.g., 'kor', 'jpn', 'cmn'). ```text Result: array([[ 209, 0, 0], [ 0, 5127, 3], [ 0, 1, 1704]]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.