forked from krille-chan/fluffychat
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added language detection controller, edited README, and removed some …
…old code
- Loading branch information
William Jordan-Cooley
authored and
William Jordan-Cooley
committed
May 21, 2024
1 parent
7e7c881
commit aea0c9c
Showing
6 changed files
with
233 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
lib/pangea/choreographer/controllers/analytics_sender.dart
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
lib/pangea/controllers/language_detection_controller.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:fluffychat/pangea/config/environment.dart'; | ||
import 'package:fluffychat/pangea/controllers/pangea_controller.dart'; | ||
import 'package:fluffychat/pangea/network/urls.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
import '../network/requests.dart'; | ||
|
||
class LanguageDetectionRequest { | ||
String fullText; | ||
String userL1; | ||
String userL2; | ||
|
||
LanguageDetectionRequest({ | ||
required this.fullText, | ||
this.userL1 = "", | ||
required this.userL2, | ||
}); | ||
|
||
Map<String, dynamic> toJson() => { | ||
'full_text': fullText, | ||
'user_l1': userL1, | ||
'user_l2': userL2, | ||
}; | ||
|
||
@override | ||
bool operator ==(Object other) { | ||
if (identical(this, other)) return true; | ||
return other is LanguageDetectionRequest && | ||
other.fullText == fullText && | ||
other.userL1 == userL1 && | ||
other.userL2 == userL2; | ||
} | ||
|
||
@override | ||
int get hashCode => fullText.hashCode ^ userL1.hashCode ^ userL2.hashCode; | ||
} | ||
|
||
class LanguageDetectionResponse { | ||
List<Map<String, dynamic>> detections; | ||
String fullText; | ||
|
||
LanguageDetectionResponse({ | ||
required this.detections, | ||
required this.fullText, | ||
}); | ||
|
||
factory LanguageDetectionResponse.fromJson(Map<String, dynamic> json) { | ||
return LanguageDetectionResponse( | ||
detections: List<Map<String, dynamic>>.from(json['detections']), | ||
fullText: json['full_text'], | ||
); | ||
} | ||
} | ||
|
||
class _LanguageDetectionCacheItem { | ||
Future<LanguageDetectionResponse> data; | ||
|
||
_LanguageDetectionCacheItem({ | ||
required this.data, | ||
}); | ||
} | ||
|
||
class LanguageDetectionController { | ||
static final Map<LanguageDetectionRequest, _LanguageDetectionCacheItem> | ||
_cache = {}; | ||
late final PangeaController _pangeaController; | ||
Timer? _cacheClearTimer; | ||
|
||
LanguageDetectionController(PangeaController pangeaController) { | ||
_pangeaController = pangeaController; | ||
_initializeCacheClearing(); | ||
} | ||
|
||
void _initializeCacheClearing() { | ||
const duration = Duration(minutes: 15); // Adjust the duration as needed | ||
_cacheClearTimer = Timer.periodic(duration, (Timer t) => _clearCache()); | ||
} | ||
|
||
void _clearCache() { | ||
_cache.clear(); | ||
} | ||
|
||
void dispose() { | ||
_cacheClearTimer?.cancel(); | ||
} | ||
|
||
Future<LanguageDetectionResponse> get( | ||
LanguageDetectionRequest params, | ||
) async { | ||
if (_cache.containsKey(params)) { | ||
return _cache[params]!.data; | ||
} else { | ||
final Future<LanguageDetectionResponse> response = _fetchResponse( | ||
await _pangeaController.userController.accessToken, | ||
params, | ||
); | ||
_cache[params] = _LanguageDetectionCacheItem(data: response); | ||
return response; | ||
} | ||
} | ||
|
||
static Future<LanguageDetectionResponse> _fetchResponse( | ||
String accessToken, | ||
LanguageDetectionRequest params, | ||
) async { | ||
final Requests request = Requests( | ||
choreoApiKey: Environment.choreoApi, | ||
accessToken: accessToken, | ||
); | ||
|
||
final http.Response res = await request.post( | ||
url: PApiUrls.languageDetection, | ||
body: params.toJson(), | ||
); | ||
|
||
final Map<String, dynamic> json = jsonDecode(res.body); | ||
return LanguageDetectionResponse.fromJson(json); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.