Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor 003 #491

Merged
merged 7 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/violet-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ jobs:
- uses: actions/setup-java@v1
with:
java-version: "12.x"

- uses: subosito/flutter-action@v2
with:
channel: "stable"
flutter-version: 3.19.6

- uses: actions/setup-python@v2
with:
Expand Down
41 changes: 4 additions & 37 deletions violet/lib/component/hitomi/displayed_tag.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,18 @@
import 'package:violet/component/hitomi/tag_translate.dart';

class DisplayedTag {
// artist, group, tag, series, character, uploader, type, class, language, prefix, page
// artist, group, tag, series, character, uploader, type, class, language, prefix, page, female, male
String? group;
// <tag>, female:<tag>, male:<tag>
// <tag>
String? name;
// <translated-tag>
String? translated;

// tag := <group>:<name>
DisplayedTag({String? tag, this.group, this.name, this.translated}) {
if (tag != null) {
final maybeGroup = tag.split(':').first;
if ([
'artist',
'group',
'tag',
'series',
'character',
'uploader',
'type',
'class',
'language',
'prefix',
'page',
].contains(maybeGroup)) {
group = maybeGroup;
name = tag.substring(tag.indexOf(':') + 1);
} else if (['female', 'male'].contains(maybeGroup)) {
group = 'tag';
name = tag;
}
}

if (group != null) {
if (['female', 'male'].contains(group)) {
if (!name!.startsWith('$group:')) name = '$group:$name';
group = 'tag';
}
group = tag.split(':').first;
name = tag.substring(tag.indexOf(':') + 1);
}
}

Expand All @@ -61,12 +36,4 @@ class DisplayedTag {
String toString() {
return getTag();
}

// TODO: https://github.com/project-violet/violet/issues/440 로 삭제
bool groupEqualTo(String otherGroup) {
if (otherGroup == 'female' || otherGroup == 'male') {
return group == 'tag' && name!.startsWith(otherGroup);
}
return group == otherGroup;
}
}
136 changes: 42 additions & 94 deletions violet/lib/component/hitomi/hitomi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ class HitomiManager {

static String normalizeTagPrefix(String pp) {
switch (pp) {
case 'female':
case 'male':
case 'tags':
return 'tag';

Expand Down Expand Up @@ -86,6 +84,24 @@ class HitomiManager {
final text = path.readAsStringSync();
tagmap = jsonDecode(text);
}

// split `tag:female:` and `tag:male:` to `female:` and `male:`
if (tagmap!.containsKey('tag')) {
final tags = tagmap!['tag'] as Map<String, dynamic>;
final femaleTags = tags.entries
.where((e) => e.key.startsWith('female:'))
.map((e) => MapEntry(e.key.split(':')[1], e.value))
.toList();
final maleTags = tags.entries
.where((e) => e.key.startsWith('male:'))
.map((e) => MapEntry(e.key.split(':')[1], e.value))
.toList();
tagmap!['female'] = Map.fromEntries(femaleTags);
tagmap!['male'] = Map.fromEntries(maleTags);

tags.removeWhere(
(tag, _) => tag.startsWith('female:') || tag.startsWith('male:'));
}
}
}

Expand All @@ -108,44 +124,20 @@ class HitomiManager {
String prefix, bool useTranslated) {
final groupOrig = prefix.split(':')[0];
final group = normalizeTagPrefix(groupOrig);
final name = prefix.split(':').last;

final results = <Tuple2<DisplayedTag, int>>[];
if (!tagmap!.containsKey(group)) return results;

final nameCountsMap = tagmap![group] as Map<dynamic, dynamic>;
if (!useTranslated) {
final tagContains = () {
switch (groupOrig) {
case 'female':
case 'male':
return (key) =>
key.toLowerCase().startsWith('$groupOrig:') &&
key.toLowerCase().contains(prefix);

case 'tag':
final po = prefix.split(':')[1];
return (key) =>
!key.toLowerCase().startsWith('female:') &&
!key.toLowerCase().startsWith('male:') &&
key.toLowerCase().contains(po);

default:
final po = prefix.split(':')[1];
return (key) => key.toLowerCase().contains(po) as bool;
}
}();

nameCountsMap.entries
.where((element) => tagContains(element.key))
.forEach((element) {
results.add(Tuple2<DisplayedTag, int>(
DisplayedTag(group: group, name: element.key), element.value));
});
results.addAll(nameCountsMap.entries
.where((e) => e.key.toString().toLowerCase().contains(name))
.map((e) => Tuple2<DisplayedTag, int>(
DisplayedTag(group: group, name: e.key), e.value)));
} else {
final name = prefix.split(':').last;
results.addAll(TagTranslate.containsTotal(name)
.where((e) =>
e.groupEqualTo(groupOrig) && nameCountsMap.containsKey(e.name))
.where((e) => e.group! == group && nameCountsMap.containsKey(e.name))
.map((e) => Tuple2<DisplayedTag, int>(e, nameCountsMap[e.name])));
}
results.sort((a, b) => b.item2.compareTo(a.item2));
Expand Down Expand Up @@ -203,53 +195,28 @@ class HitomiManager {
if (prefix.contains(':')) {
final groupOrig = prefix.split(':')[0];
final group = normalizeTagPrefix(groupOrig);
final name = prefix.split(':').last;

// <Tag, Similarity, Count>
final results = <Tuple3<DisplayedTag, int, int>>[];
if (!tagmap!.containsKey(group)) return <Tuple2<DisplayedTag, int>>[];

final nameCountsMap = tagmap![group];
if (!useTranslated) {
if (groupOrig == 'female' || groupOrig == 'male') {
nameCountsMap.forEach((key, value) {
if (key.toLowerCase().startsWith('$groupOrig:')) {
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: groupOrig, name: key),
Distance.levenshteinDistance(
prefix.runes.toList(), key.runes.toList()),
value));
}
});
} else if (groupOrig == 'tag') {
// CHECK: tag:female:~, tag:artist:~도 허용해야하는지?
final name = prefix.split(':').last;
nameCountsMap.forEach((key, value) {
if (!key.toLowerCase().startsWith('female:') &&
!key.toLowerCase().startsWith('male:')) {
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: groupOrig, name: key),
Distance.levenshteinDistance(
name.runes.toList(), key.runes.toList()),
value));
}
});
} else {
final name = prefix.split(':').last;
nameCountsMap.forEach((key, value) {
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: group, name: key),
Distance.levenshteinDistance(
name.runes.toList(), key.runes.toList()),
value));
});
}
nameCountsMap.forEach((key, value) {
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: group, name: key),
Distance.levenshteinDistance(
name.runes.toList(), key.runes.toList()),
value));
});
} else {
final name = prefix.split(':').last;
results.addAll(TagTranslate.containsFuzzingTotal(name)
.where((e) =>
e.item1.groupEqualTo(groupOrig) &&
e.item1.group! == group &&
nameCountsMap.containsKey(e.item1.name))
.map((e) => Tuple3<DisplayedTag, int, int>(
e.item1, nameCountsMap[e.item1.name], e.item2)));
e.item1, e.item2, nameCountsMap[e.item1.name])));
}
results.sort((a, b) => a.item2.compareTo(b.item2));
return results
Expand All @@ -259,32 +226,13 @@ class HitomiManager {
if (!useTranslated) {
final results = <Tuple3<DisplayedTag, int, int>>[];
tagmap!.forEach((group, value) {
if (group == 'tag') {
value.forEach((name, count) {
if (name.contains(':')) {
final split = name.split(':');
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: split[0], name: name),
Distance.levenshteinDistance(
prefix.runes.toList(), split[1].runes.toList()),
count));
} else {
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: 'tag', name: name),
Distance.levenshteinDistance(
prefix.runes.toList(), name.runes.toList()),
count));
}
});
} else {
value.forEach((name, count) {
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: group, name: name),
Distance.levenshteinDistance(
prefix.runes.toList(), name.runes.toList()),
count));
});
}
value.forEach((name, count) {
results.add(Tuple3<DisplayedTag, int, int>(
DisplayedTag(group: group, name: name),
Distance.levenshteinDistance(
prefix.runes.toList(), name.runes.toList()),
count));
});
});
results.sort((a, b) => a.item2.compareTo(b.item2));
return results
Expand Down
26 changes: 17 additions & 9 deletions violet/lib/component/hitomi/tag_translate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,22 @@ class TagTranslate {
return null;
}

if (translateMap.containsKey(key)) {
late final String nomalizedKey;
if (key.startsWith('tag:female:') || key.startsWith('tag:male:')) {
nomalizedKey = key.substring('tag:'.length);
} else {
nomalizedKey = key;
}

if (translateMap.containsKey(nomalizedKey)) {
return null;
}

translateMap[key] = value;
reverseAndroMap[disassembly(value)
.replaceAll('female:', '')
.replaceAll('male:', '')] = key;
late final String nomalizedValue =
value.replaceAll('female:', '').replaceAll('male:', '');

translateMap[nomalizedKey] = nomalizedValue;
reverseAndroMap[disassembly(nomalizedValue)] = nomalizedKey;

return null;
},
Expand Down Expand Up @@ -87,11 +95,11 @@ class TagTranslate {
if (_translateMap.containsKey('character:$key')) {
return _translateMap['character:$key']!.split('|').first;
}
if (_translateMap.containsKey('tag:female:$key')) {
return _translateMap['tag:female:$key']!.split('|').first;
if (_translateMap.containsKey('female:$key')) {
return _translateMap['female:$key']!.split('|').first;
}
if (_translateMap.containsKey('tag:male:$key')) {
return _translateMap['tag:male:$key']!.split('|').first;
if (_translateMap.containsKey('male:$key')) {
return _translateMap['male:$key']!.split('|').first;
}
if (_translateMap.containsKey('tag:$key')) {
return _translateMap['tag:$key']!.split('|').first;
Expand Down
Loading
Loading