betto_lang_detector 0.1.0-dev.1
betto_lang_detector: ^0.1.0-dev.1 copied to clipboard
A pure-Dart language detection library for Dart and Flutter applications.
// Copyright 2026 The Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ignore_for_file: avoid_print
import 'package:betto_lang_detector/betto_lang_detector.dart';
void main() {
final detector = LanguageDetector.pureDart();
// Example 1: a clear-cut match.
switch (detector.detect('Bonjour tout le monde, comment allez-vous ?')) {
case Detected(best: final guess):
print('Detected: ${guess.code} (confidence ${guess.confidence})'); // fr
case Undetermined():
print('Could not determine a language');
}
// Example 2: restrictTo narrows (and sharpens) the n-gram comparison —
// useful when the caller already knows the plausible language set, e.g.
// from a vault's known document languages.
final restricted = LanguageDetector.pureDart(restrictTo: {'en', 'de', 'nl'});
final result = restricted.detect('Guten Morgen, wie geht es dir heute?');
print(result); // Detected(LanguageGuess(de, ...), ...)
// Example 3: cheap script-only classification, without running the
// (comparatively more expensive) n-gram stage — useful for routing
// decisions like picking a tokenizer.
print(detector.dominantScript('こんにちは')); // Hira
print(detector.dominantScript('中文')); // Hani
print(detector.dominantScript('123 !?')); // null — no scripted letters
// Example 4: Undetermined for input with no language signal at all.
print(detector.detect('')); // Undetermined(ranked: [])
}