From f803c59050cc49fb44e3dc378ea43f2d8e864037 Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Fri, 25 Jul 2025 22:41:44 +0200 Subject: [PATCH] refactor ml utils --- .../tabs/domain/repositories/gecko_inference.dart | 3 +-- packages/flutter_mozilla_components/lib/ml_utils.dart | 2 ++ .../lib/src/utils/ml}/cosine_similarity.dart | 6 ++++++ .../lib/src/utils/ml}/embedding_text_processing.dart | 6 ++++++ .../lib/src/utils/ml}/nearest_neighbor.dart | 8 +++++++- packages/flutter_mozilla_components/pubspec.yaml | 1 + .../test/text_processing_test.dart | 2 +- 7 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 packages/flutter_mozilla_components/lib/ml_utils.dart rename {app/lib/features/geckoview/features/tabs/utils => packages/flutter_mozilla_components/lib/src/utils/ml}/cosine_similarity.dart (75%) rename {app/lib/features/geckoview/features/tabs/utils => packages/flutter_mozilla_components/lib/src/utils/ml}/embedding_text_processing.dart (86%) rename {app/lib/features/geckoview/features/tabs/utils => packages/flutter_mozilla_components/lib/src/utils/ml}/nearest_neighbor.dart (85%) rename {app => packages/flutter_mozilla_components}/test/text_processing_test.dart (96%) diff --git a/app/lib/features/geckoview/features/tabs/domain/repositories/gecko_inference.dart b/app/lib/features/geckoview/features/tabs/domain/repositories/gecko_inference.dart index bd039561..6c184ab7 100644 --- a/app/lib/features/geckoview/features/tabs/domain/repositories/gecko_inference.dart +++ b/app/lib/features/geckoview/features/tabs/domain/repositories/gecko_inference.dart @@ -3,13 +3,12 @@ import 'dart:async'; import 'package:collection/collection.dart'; import 'package:fast_equatable/fast_equatable.dart'; import 'package:flutter_mozilla_components/flutter_mozilla_components.dart'; +import 'package:flutter_mozilla_components/ml_utils.dart'; import 'package:nullability/nullability.dart'; import 'package:riverpod/riverpod.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:synchronized/synchronized.dart'; import 'package:weblibre/features/geckoview/features/tabs/domain/providers.dart'; -import 'package:weblibre/features/geckoview/features/tabs/utils/embedding_text_processing.dart'; -import 'package:weblibre/features/geckoview/features/tabs/utils/nearest_neighbor.dart'; import 'package:weblibre/features/user/domain/repositories/general_settings.dart'; import 'package:weblibre/utils/lru_cache.dart'; diff --git a/packages/flutter_mozilla_components/lib/ml_utils.dart b/packages/flutter_mozilla_components/lib/ml_utils.dart new file mode 100644 index 00000000..30ed84fc --- /dev/null +++ b/packages/flutter_mozilla_components/lib/ml_utils.dart @@ -0,0 +1,2 @@ +export 'src/utils/ml/embedding_text_processing.dart'; +export 'src/utils/ml/nearest_neighbor.dart'; diff --git a/app/lib/features/geckoview/features/tabs/utils/cosine_similarity.dart b/packages/flutter_mozilla_components/lib/src/utils/ml/cosine_similarity.dart similarity index 75% rename from app/lib/features/geckoview/features/tabs/utils/cosine_similarity.dart rename to packages/flutter_mozilla_components/lib/src/utils/ml/cosine_similarity.dart index dd7efd46..21bba080 100644 --- a/app/lib/features/geckoview/features/tabs/utils/cosine_similarity.dart +++ b/packages/flutter_mozilla_components/lib/src/utils/ml/cosine_similarity.dart @@ -1,3 +1,9 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + import 'dart:math'; /// Calculates cosine similarity between two lists of floats diff --git a/app/lib/features/geckoview/features/tabs/utils/embedding_text_processing.dart b/packages/flutter_mozilla_components/lib/src/utils/ml/embedding_text_processing.dart similarity index 86% rename from app/lib/features/geckoview/features/tabs/utils/embedding_text_processing.dart rename to packages/flutter_mozilla_components/lib/src/utils/ml/embedding_text_processing.dart index 197beda6..15b7dd72 100644 --- a/app/lib/features/geckoview/features/tabs/utils/embedding_text_processing.dart +++ b/packages/flutter_mozilla_components/lib/src/utils/ml/embedding_text_processing.dart @@ -1,3 +1,9 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + /// Removes trailing domain-related text such as '... - Mail' or '... | News' /// If there's not enough information remaining after, we keep the text as is /// [text] tab title with potential domain information diff --git a/app/lib/features/geckoview/features/tabs/utils/nearest_neighbor.dart b/packages/flutter_mozilla_components/lib/src/utils/ml/nearest_neighbor.dart similarity index 85% rename from app/lib/features/geckoview/features/tabs/utils/nearest_neighbor.dart rename to packages/flutter_mozilla_components/lib/src/utils/ml/nearest_neighbor.dart index a858a725..6e6637a1 100644 --- a/app/lib/features/geckoview/features/tabs/utils/nearest_neighbor.dart +++ b/packages/flutter_mozilla_components/lib/src/utils/ml/nearest_neighbor.dart @@ -1,5 +1,11 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + import 'package:collection/collection.dart'; -import 'package:weblibre/features/geckoview/features/tabs/utils/cosine_similarity.dart'; +import 'package:flutter_mozilla_components/src/utils/ml/cosine_similarity.dart'; List findNearestNeighborsRecursive({ required Map> embeddings, diff --git a/packages/flutter_mozilla_components/pubspec.yaml b/packages/flutter_mozilla_components/pubspec.yaml index 066a8250..9dc7da33 100644 --- a/packages/flutter_mozilla_components/pubspec.yaml +++ b/packages/flutter_mozilla_components/pubspec.yaml @@ -8,6 +8,7 @@ environment: flutter: '>=3.3.0' dependencies: + collection: ^1.19.1 flutter: sdk: flutter rxdart: ^0.28.0 diff --git a/app/test/text_processing_test.dart b/packages/flutter_mozilla_components/test/text_processing_test.dart similarity index 96% rename from app/test/text_processing_test.dart rename to packages/flutter_mozilla_components/test/text_processing_test.dart index a81ded66..b659bc94 100644 --- a/app/test/text_processing_test.dart +++ b/packages/flutter_mozilla_components/test/text_processing_test.dart @@ -1,5 +1,5 @@ +import 'package:flutter_mozilla_components/src/utils/ml/embedding_text_processing.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:weblibre/features/geckoview/features/tabs/utils/embedding_text_processing.dart'; void main() { group('Text processing basic cases', () {