Skip to content

Instantly share code, notes, and snippets.

@marshyon
Last active October 2, 2023 17:21
Show Gist options
  • Save marshyon/87a5f4ede8fe3930d5b8a38dc6125a6a to your computer and use it in GitHub Desktop.
Save marshyon/87a5f4ede8fe3930d5b8a38dc6125a6a to your computer and use it in GitHub Desktop.
fuzzy string search of list of objects in dart using fuzzywuzzy
import 'package:fuzzywuzzy/fuzzywuzzy.dart';
class Book {
final String title;
final String author;
final String genre;
final String summary;
Book(this.title, this.author, this.genre, this.summary);
}
class SearchRank {
final Book book;
final int rank;
SearchRank(this.book, this.rank);
}
final books = [
Book('The Hobbit', 'J.R.R. Tolkien', 'Fantasy',
'The classic fantasy novel about Bilbo Baggins and his adventures with dwarves and a dragon.'),
Book('Nineteen Eighty-Four', 'George Orwell', 'Dystopian',
'The dystopian novel set in a totalitarian society where Big Brother is always watching and history is constantly rewritten.'),
Book('To Kill a Mockingbird', 'Harper Lee', 'Classic',
'A story of racial injustice and moral growth in the American South.'),
Book('Pride and Prejudice', 'Jane Austen', 'Romance',
'The classic novel about love, class, and society in 19th-century England.'),
Book('The Catcher in the Rye', 'J.D. Salinger', 'Coming-of-age',
'Follow the adventures of Holden Caulfield as he navigates the challenges of adolescence.'),
Book('The Great Gatsby', 'F. Scott Fitzgerald', 'Classic',
'A tale of love, wealth, and the American Dream during the Roaring Twenties.'),
Book('The Lord of the Rings', 'J.R.R. Tolkien', 'Fantasy',
'Epic fantasy trilogy following the quest to destroy the One Ring and defeat Sauron.'),
Book('Brave New World', 'Aldous Huxley', 'Dystopian',
'A futuristic society controlled by technology, where individuality is suppressed.'),
Book('Crime and Punishment', 'Fyodor Dostoevsky', 'Psychological Thriller',
'Exploration of morality, guilt, and redemption in the mind of a tormented student.'),
Book('1984', 'George Orwell', 'Dystopian',
'Winston Smith\'s struggle against the oppressive regime of Oceania and the omnipresent Big Brother.'),
Book('The Da Vinci Code', 'Dan Brown', 'Mystery',
'A gripping mystery involving art, religion, and secret societies.'),
Book('The Hunger Games', 'Suzanne Collins', 'Dystopian',
'A young girl\'s journey in a dystopian world where she must fight to survive in a brutal game.')
];
void findBooks(String term) {
var searchRanks = <SearchRank>[];
for (var book in books) {
var searchText =
('${book.title} ${book.author} ${book.genre} ${book.summary}')
.toLowerCase();
int ranking = partialRatio(searchText, term.toLowerCase());
searchRanks.add(SearchRank(book, ranking));
}
searchRanks.sort((a, b) => b.rank.compareTo(a.rank));
var maxResults = 5;
var resultCount = 0;
for (var rank in searchRanks) {
print('>> book: ${rank.book.title}');
print('>> rank: ${rank.rank}');
resultCount++;
if (resultCount >= maxResults) {
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment