Skip to content

Commit

Permalink
feat: add caching system
Browse files Browse the repository at this point in the history
  • Loading branch information
rIIh committed Nov 15, 2020
1 parent b476ce8 commit 023d267
Show file tree
Hide file tree
Showing 33 changed files with 587 additions and 55 deletions.
3 changes: 2 additions & 1 deletion build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ targets:
options:
explicit_to_json: true
include_if_null: false
nullable: true
nullable: true
any_map: true
3 changes: 3 additions & 0 deletions lib/oxford_dict.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export 'src/index.dart';
export 'src/entries/model/dictionary_entry.dart';
export 'src/entries/model/lexical_entries.dart';
export 'src/entries/model/domain_class.dart';
export 'src/entries/model/grammatical_feature.dart';
export 'src/entries/model/lexical_entry.dart';
export 'src/entries/model/example.dart';
export 'src/entries/model/lexical_category.dart';
Expand All @@ -13,3 +14,5 @@ export 'src/entries/model/pronunciation.dart';
export 'src/entries/model/result.dart';
export 'src/entries/model/semantic_classes.dart';
export 'src/entries/model/sense.dart';
export 'src/entities/word.dart';
export 'src/lemmas/entity/lemmas.dart';
108 changes: 108 additions & 0 deletions lib/src/entities/word.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import 'dart:collection';
import 'package:oxford_dictionary/oxford_dict.dart' as remote;
import 'package:oxford_dictionary/src/entries/model/cross_reference.dart';
import 'package:oxford_dictionary/src/entries/model/grammatical_feature.dart';

class Word {
final String id;
final String word;
final String language;
final Map<remote.LexicalCategory, List<Variant>> variants;
final Map<remote.LexicalCategory, List<String>> phrases;
final remote.DictionaryEntries rawResponse;

Word.fromEntry(remote.DictionaryEntries entry)
: id = entry.id,
word = entry.word,
language = entry.results?.first?.language,
variants = SplayTreeMap.of(
Map.fromEntries(
entry.results
.map(
(e) => e.lexicalEntries.map(
(e) => MapEntry(
e.lexicalCategory,
e.entries
.map(
(e) => Variant.fromLexicalEntry(e),
)
.toList(),
),
),
)
.expand((element) => element)
.fold<List<MapEntry<remote.LexicalCategory, List<Variant>>>>(
[],
(reduced, element) {
if (reduced.map((e) => e.key).contains(element.key)) {
final list = reduced.firstWhere((reducedElement) => reducedElement.key == element.key).value;
list.addAll([...element.value]);
list.sort(
(e1, e2) => e1.grammaticalFeatures == null ? -1 : 1,
);
} else {
reduced.add(MapEntry(element.key, element.value));
}
return reduced;
},
),
),
(k1, k2) => k1.compareTo(k2),
),
phrases = Map.fromEntries(
entry.results
?.where((element) => element != null)
?.map(
(e) => e.lexicalEntries?.where((element) => element != null)?.map(
(e) => MapEntry(
e.lexicalCategory,
e.phrases?.where((element) => element != null)?.map((e) => e.text)?.toList(),
),
),
)
?.expand((element) => element),
),
rawResponse = entry;

}

class Variant {
final List<String> etymologies;
final String homographNumber;
final List<GrammaticalFeature> grammaticalFeatures;
final List<remote.Pronunciation> pronunciations;
final List<Sense> senses;

Variant.fromLexicalEntry(remote.LexicalEntry entry)
: senses = entry.senses.map((e) => Sense.fromRemoteSense(e)).toList(),
grammaticalFeatures = entry.grammaticalFeatures,
homographNumber = entry.homographNumber,
pronunciations = entry.pronunciations,
etymologies = entry.etymologies;

}

class Sense {
final List<String> definitions;
final List<CrossReference> crossReferences;
final List<String> shortDefinitions;
final List<String> examples;
final List<String> synonyms;
final List<String> semanticClasses;
final List<String> domains;
final List<Sense> subSenses;
final List<String> constructions;
final bool isCrossReferenced;

Sense.fromRemoteSense(remote.SenseFromRemote remoteSense)
: definitions = remoteSense.definitions ?? remoteSense.crossReferenceMarkers,
crossReferences = remoteSense.crossReferences,
subSenses = remoteSense.subSenses?.map((e) => Sense.fromRemoteSense(e))?.toList(),
constructions = remoteSense.constructions?.map((e) => e.text)?.toList(),
synonyms = remoteSense.synonyms?.map((e) => e.text)?.toList(),
isCrossReferenced = remoteSense.crossReferenceMarkers != null && remoteSense.definitions == null,
shortDefinitions = remoteSense.shortDefinitions,
examples = remoteSense.examples?.map((e) => e.text)?.toList(),
semanticClasses = remoteSense.semanticClasses?.map((e) => e.text)?.toList(),
domains = remoteSense.domainClasses?.map((e) => e.text)?.toList();
}
16 changes: 16 additions & 0 deletions lib/src/entries/model/cross_reference.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:json_annotation/json_annotation.dart';

part 'cross_reference.g.dart';

@JsonSerializable()
class CrossReference {
final String id;
final String text;
final String type;

CrossReference(this.id, this.text, this.type);

factory CrossReference.fromJson(Map json) => _$CrossReferenceFromJson(json);

Map toJson() => _$CrossReferenceToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/dictionary_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class DictionaryEntries {

DictionaryEntries({this.id, this.metadata, this.results, this.word});

factory DictionaryEntries.fromJson(Map<String, dynamic> json) => _$DictionaryEntriesFromJson(json);
factory DictionaryEntries.fromJson(Map json) => _$DictionaryEntriesFromJson(json);

Map<String, dynamic> toJson() => _$DictionaryEntriesToJson(this);
Map toJson() => _$DictionaryEntriesToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/domain_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DomainClass {

DomainClass(this.id, this.text);

factory DomainClass.fromJson(Map<String, dynamic> json) => _$DomainClassFromJson(json);
factory DomainClass.fromJson(Map json) => _$DomainClassFromJson(json);

Map<String, dynamic> toJson() => _$DomainClassToJson(this);
Map toJson() => _$DomainClassToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Example {

Example(this.text);

factory Example.fromJson(Map<String, dynamic> json) => _$ExampleFromJson(json);
factory Example.fromJson(Map json) => _$ExampleFromJson(json);

Map<String, dynamic> toJson() => _$ExampleToJson(this);
Map toJson() => _$ExampleToJson(this);
}
16 changes: 16 additions & 0 deletions lib/src/entries/model/grammatical_feature.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:json_annotation/json_annotation.dart';

part 'grammatical_feature.g.dart';

@JsonSerializable()
class GrammaticalFeature {
final String id;
final String text;
final String type;

GrammaticalFeature(this.id, this.text, this.type);

factory GrammaticalFeature.fromJson(Map json) => _$GrammaticalFeatureFromJson(json);

Map toJson() => _$GrammaticalFeatureToJson(this);
}
20 changes: 17 additions & 3 deletions lib/src/entries/model/lexical_category.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ import 'package:json_annotation/json_annotation.dart';
part 'lexical_category.g.dart';

@JsonSerializable()
class LexicalCategory {
class LexicalCategory implements Comparable<LexicalCategory> {
final String id;
final String text;

LexicalCategory(this.id, this.text);

factory LexicalCategory.fromJson(Map<String, dynamic> json) => _$LexicalCategoryFromJson(json);
factory LexicalCategory.fromJson(Map json) => _$LexicalCategoryFromJson(json);

Map<String, dynamic> toJson() => _$LexicalCategoryToJson(this);
Map toJson() => _$LexicalCategoryToJson(this);

@override
int compareTo(LexicalCategory other) {
return id.compareTo(other.id);
}

@override
bool operator ==(Object other) {
if (other is LexicalCategory) {
return id == other.id;
} else {
return false;
}
}
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/lexical_entries.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class LexicalEntries {

LexicalEntries(this.entries, this.language, this.lexicalCategory, this.phrases, this.text);

factory LexicalEntries.fromJson(Map<String, dynamic> json) => _$LexicalEntriesFromJson(json);
factory LexicalEntries.fromJson(Map json) => _$LexicalEntriesFromJson(json);

Map<String, dynamic> toJson() => _$LexicalEntriesToJson(this);
Map toJson() => _$LexicalEntriesToJson(this);
}
21 changes: 17 additions & 4 deletions lib/src/entries/model/lexical_entry.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:oxford_dictionary/src/entries/model/cross_reference.dart';

import 'grammatical_feature.dart';
import 'pronunciation.dart';
import 'sense.dart';

part 'lexical_entry.g.dart';

@JsonSerializable()
class LexicalEntry {
final List<String> crossReferenceMarkers;
final List<CrossReference> crossReferences;
final List<GrammaticalFeature> grammaticalFeatures;
final List<String> etymologies;
final String homographNumber;
final List<Pronunciation> pronunciations;
final List<Sense> senses;
final List<SenseFromRemote> senses;

LexicalEntry({this.etymologies, this.homographNumber, this.pronunciations, this.senses});
LexicalEntry({
this.grammaticalFeatures,
this.crossReferenceMarkers,
this.crossReferences,
this.etymologies,
this.homographNumber,
this.pronunciations,
this.senses,
});

factory LexicalEntry.fromJson(Map<String, dynamic> json) => _$EntryFromJson(json);
factory LexicalEntry.fromJson(Map<String, dynamic> json) => _$LexicalEntryFromJson(json);

Map<String, dynamic> toJson() => _$EntryToJson(this);
Map<String, dynamic> toJson() => _$LexicalEntryToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/metadata.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Metadata {

Metadata(this.operation, this.provider, this.schema);

factory Metadata.fromJson(Map<String, dynamic> json) => _$MetadataFromJson(json);
factory Metadata.fromJson(Map json) => _$MetadataFromJson(json);

Map<String, dynamic> toJson() => _$MetadataToJson(this);
Map toJson() => _$MetadataToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/phrase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Phrase {

Phrase(this.id, this.text);

factory Phrase.fromJson(Map<String, dynamic> json) => _$PhraseFromJson(json);
factory Phrase.fromJson(Map json) => _$PhraseFromJson(json);

Map<String, dynamic> toJson() => _$PhraseToJson(this);
Map toJson() => _$PhraseToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/pronunciation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Pronunciation {

Pronunciation(this.audioFile, this.dialects, this.phoneticNotation, this.phoneticSpelling);

factory Pronunciation.fromJson(Map<String, dynamic> json) => _$PronunciationFromJson(json);
factory Pronunciation.fromJson(Map json) => _$PronunciationFromJson(json);

Map<String, dynamic> toJson() => _$PronunciationToJson(this);
Map toJson() => _$PronunciationToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Result {

Result(this.id, this.language, this.lexicalEntries, this.type, this.word);

factory Result.fromJson(Map<String, dynamic> json) => _$ResultFromJson(json);
factory Result.fromJson(Map json) => _$ResultFromJson(json);

Map<String, dynamic> toJson() => _$ResultToJson(this);
Map toJson() => _$ResultToJson(this);
}
4 changes: 2 additions & 2 deletions lib/src/entries/model/semantic_classes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SemanticClass {

SemanticClass(this.id, this.text);

factory SemanticClass.fromJson(Map<String, dynamic> json) => _$SemanticClassFromJson(json);
factory SemanticClass.fromJson(Map json) => _$SemanticClassFromJson(json);

Map<String, dynamic> toJson() => _$SemanticClassToJson(this);
Map toJson() => _$SemanticClassToJson(this);
}
38 changes: 34 additions & 4 deletions lib/src/entries/model/sense.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:oxford_dictionary/src/entries/model/synonim.dart';

import 'cross_reference.dart';
import 'domain_class.dart';
import 'example.dart';
import 'semantic_classes.dart';

part 'sense.g.dart';

@JsonSerializable()
class Sense {
class Construction {
final String text;

Construction(this.text);

factory Construction.fromJson(Map json) => _$ConstructionFromJson(json);

Map toJson() => _$ConstructionToJson(this);
}

@JsonSerializable()
class SenseFromRemote {
final List<Construction> constructions;
final List<String> definitions;
final List<DomainClass> domainClasses;
final List<Example> examples;
final List<Synonym> synonyms;
final String id;
final List<SemanticClass> semanticClasses;
final List<String> shortDefinitions;
final List<String> crossReferenceMarkers;
final List<CrossReference> crossReferences;
@JsonKey(name: 'subsenses')
final List<SenseFromRemote> subSenses;

Sense(this.definitions, this.domainClasses, this.examples, this.id, this.semanticClasses, this.shortDefinitions);
SenseFromRemote(
this.definitions,
this.domainClasses,
this.examples,
this.id,
this.semanticClasses,
this.shortDefinitions,
this.crossReferenceMarkers,
this.crossReferences,
this.subSenses,
this.constructions, this.synonyms,
);

factory Sense.fromJson(Map<String, dynamic> json) => _$SenseFromJson(json);
factory SenseFromRemote.fromJson(Map json) => _$SenseFromRemoteFromJson(json);

Map<String, dynamic> toJson() => _$SenseToJson(this);
Map toJson() => _$SenseFromRemoteToJson(this);
}
15 changes: 15 additions & 0 deletions lib/src/entries/model/synonim.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:json_annotation/json_annotation.dart';

part 'synonim.g.dart';

@JsonSerializable()
class Synonym {
final String language;
final String text;

Synonym(this.language, this.text);

factory Synonym.fromJson(Map json) => _$SynonymFromJson(json);

Map toJson() => _$SynonymToJson(this);
}
Loading

0 comments on commit 023d267

Please sign in to comment.