Recursive AI

Detect the language of text in C#

OneNLP is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Identify a string’s language locally with the model included in OneNLP.

Terminal
dotnet add package RecursiveAI.OneNlp

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneNlp;
 
Console.WriteLine(Nlp.DetectLanguage(
    "Bonjour, comment allez-vous ?").Code);

The detector returns a language; .Code prints its language code.

Text
fr

Unzip the example, then dotnet run.

The full program

The download’s Program.cs uses the same visible input as the snippet at the top. No input files are needed.

The download’s Program.cs is the snippet above.

dotnet run prints:

Text
fr

Null input raises ArgumentNullException; input beyond Nlp.MaxLength raises ArgumentOutOfRangeException.

Variations

Test text with no letters. Digits and punctuation alone return Language.Unknown, not a guessed language. This is a standalone replacement for Program.cs.

C#
using RecursiveAI.OneNlp;
 
Console.WriteLine(Nlp.DetectLanguage("123?!") == Language.Unknown);
Text
True

The short answer

Call Nlp.DetectLanguage(text) and read the returned Language.Code. The French greeting in the snippet at the top prints fr. You don’t need a language-specific model package for detection: the identifier is embedded in RecursiveAI.OneNlp. It identifies natural languages, not programming languages, and it doesn’t translate the input.

How to detect language from text?

Pass the original string to the detector rather than guessing from a few accented characters. The language identification documentation describes a classifier using character sequences and script information. Its supported set contains 75 languages. That is the detector’s set, not a promise that every language has an entity or lemmatization model.

Nlp.DetectLanguage returns the most likely supported language. It isn’t a general “is this valid French?” test. In particular, related languages share words, and names or isolated words may not provide the context your application needs. Test the kinds of strings you actually receive before using the answer to route important work. Preserve the original text alongside its detected code so a later processing stage can reconsider the choice.

Digits, whitespace and punctuation are ignored. When there are no letters of a supported script, the answer is Language.Unknown; the variation above shows that case explicitly. Treat it as a separate routing outcome rather than interpreting it as English. Calling other high-level NLP verbs can have different fallback behavior, so keep identification separate when an unknown result matters.

The lower-level LanguageDetector in RecursiveAI.OneNlp.LanguageId also exposes Confidences, Restrict and DetectSpans. Confidence candidates are restricted to languages that use the text’s script, and their probabilities sum to one. These are model probabilities over its candidates, not an assurance that an arbitrary input belongs to a supported language. Restrict is appropriate when your application genuinely knows the allowed languages, not simply to force an answer you prefer.

For the most likely candidates and their probabilities, call LanguageDetector.Default.Confidences. This standalone program prints the first three candidates for the same greeting:

C#
using RecursiveAI.OneNlp.LanguageId;
using System.Globalization;
 
var candidates = LanguageDetector.Default.Confidences(
    "Bonjour, comment allez-vous ?");
foreach (var candidate in candidates.Take(3))
    Console.WriteLine($"{candidate.Language.Code}: " +
        candidate.Confidence.ToString("F4", CultureInfo.InvariantCulture));
Text
fr: 0.9986
en: 0.0005
nl: 0.0002

How do I detect the language of a string in C#?

The complete program is the same small call shown at the top. Its only input is the visible greeting, so you can replace that literal with a string from your application without preparing a file or obtaining a service key. The published package contains the detection model; running the program does not fetch weights.

For a document containing more than one language, one whole-string result cannot describe every passage. The documented DetectSpans method returns language runs with offsets covering the text. Its switching penalty controls how readily it changes language between words. That is a different decision from choosing a single label for an entire document.

Detection does not automatically install downstream language models. If you next need entities or lemmas, add the appropriate package described in model packages. Keep these deployment decisions explicit: a successful language ID result alone does not mean a model-dependent operation will succeed.

All onenlp recipes