Skip to content

Instantly share code, notes, and snippets.

@suragch
Last active January 21, 2023 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suragch/f7c661bb4e78c28129e9972134686882 to your computer and use it in GitHub Desktop.
Save suragch/f7c661bb4e78c28129e9972134686882 to your computer and use it in GitHub Desktop.
Find unique words in text
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
List<String> findUniqueWords(String text) {
final uniqueWords = <String>{};
final wordMatch = RegExp(
r'([\p{Letter}\u202f\u180B\u180C\u180D\u180E-]+)',
unicode: true,
);
final matches = wordMatch.allMatches(text);
for (final match in matches) {
final word = match.group(1);
if (word != null) {
uniqueWords.add(word);
}
}
return uniqueWords.toList()..sort();
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final controller = TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Column(children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
textAlignVertical: TextAlignVertical.top,
controller: controller,
autofocus: true,
maxLines: null,
expands: true,
decoration: const InputDecoration(border: OutlineInputBorder()),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: ElevatedButton(
onPressed: () {
final words = findUniqueWords(controller.text);
controller.text = words.toString();
print(words);
},
child: const Text('Find unique words'),
),
)
]),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment