Further enhance metadata matching precision
This commit is contained in:
committed by
Aditya Chandel
parent
204a155582
commit
b0735fe0b9
+27
-5
@@ -51,7 +51,13 @@ public class AmazonBookParser implements BookParser {
|
||||
}
|
||||
List<BookMetadata> fetchedBookMetadata = new ArrayList<>();
|
||||
for (String amazonBookId : amazonBookIds) {
|
||||
fetchedBookMetadata.add(getBookMetadata(amazonBookId));
|
||||
BookMetadata metadata = getBookMetadata(amazonBookId);
|
||||
if (metadata.getTitle() == null || metadata.getTitle().isBlank() ||
|
||||
metadata.getAuthors() == null || metadata.getAuthors().isEmpty()) {
|
||||
log.debug("Skipping metadata with missing title or author for ID: {}", amazonBookId);
|
||||
continue;
|
||||
}
|
||||
fetchedBookMetadata.add(metadata);
|
||||
}
|
||||
return fetchedBookMetadata;
|
||||
}
|
||||
@@ -81,10 +87,26 @@ public class AmazonBookParser implements BookParser {
|
||||
continue;
|
||||
}
|
||||
Element titleDiv = item.selectFirst("div[data-cy=title-recipe]");
|
||||
if (titleDiv != null) {
|
||||
String titleText = titleDiv.text();
|
||||
if (titleText.contains("Books Set")) {
|
||||
log.debug("Skipping box set item (Books Set in title): {}", extractAmazonBookId(item));
|
||||
if (titleDiv == null) {
|
||||
log.debug("Skipping item with missing title div: {}", extractAmazonBookId(item));
|
||||
continue;
|
||||
}
|
||||
|
||||
String titleText = titleDiv.text().trim();
|
||||
if (titleText.isEmpty()) {
|
||||
log.debug("Skipping item with empty title: {}", extractAmazonBookId(item));
|
||||
continue;
|
||||
}
|
||||
|
||||
String lowerTitle = titleText.toLowerCase();
|
||||
if (lowerTitle.contains("books set") || lowerTitle.contains("box set") || lowerTitle.contains("collection set") || lowerTitle.contains("summary & study guide")) {
|
||||
log.debug("Skipping box set item (matched filtered phrase) in title: {}", extractAmazonBookId(item));
|
||||
continue;
|
||||
}
|
||||
if (request.getAuthor() != null && !request.getAuthor().isBlank()) {
|
||||
String itemText = item.text().toLowerCase();
|
||||
if (!itemText.contains(request.getAuthor().toLowerCase())) {
|
||||
log.debug("Skipping item as author '{}' not found in item text: {}", request.getAuthor(), extractAmazonBookId(item));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
+11
-4
@@ -341,10 +341,17 @@ public class GoodReadsParser implements BookParser {
|
||||
String queryAuthor = request.getAuthor();
|
||||
for (Element previewBook : previewBooks) {
|
||||
List<String> authors = extractAuthorsPreview(previewBook);
|
||||
if (queryAuthor != null && !queryAuthor.isEmpty()) {
|
||||
boolean matches = authors.stream().anyMatch(author ->
|
||||
(double) fuzzyScore.fuzzyScore(author, queryAuthor) / Math.max(author.length(), queryAuthor.length()) >= 0.8
|
||||
);
|
||||
if (queryAuthor != null && !queryAuthor.isBlank()) {
|
||||
List<String> queryAuthorTokens = List.of(queryAuthor.toLowerCase().split("\\s+"));
|
||||
boolean matches = authors.stream().flatMap(a -> Arrays.stream(a.toLowerCase().split("\\s+"))).anyMatch(actual -> {
|
||||
for (String query : queryAuthorTokens) {
|
||||
int score = fuzzyScore.fuzzyScore(actual, query);
|
||||
int maxScore = Math.max(fuzzyScore.fuzzyScore(query, query), fuzzyScore.fuzzyScore(actual, actual));
|
||||
double similarity = maxScore > 0 ? (double) score / maxScore : 0;
|
||||
if (similarity >= 0.5) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (!matches) {
|
||||
continue;
|
||||
}
|
||||
|
||||
+16
-7
@@ -34,13 +34,22 @@ public class HardcoverParser implements BookParser {
|
||||
String searchAuthor = fetchMetadataRequest.getAuthor() != null ? fetchMetadataRequest.getAuthor() : "";
|
||||
|
||||
return hits.stream()
|
||||
.filter(hit -> {
|
||||
String author = String.join(" ", hit.getDocument().getAuthorNames());
|
||||
int score = fuzzyScore.fuzzyScore(author, searchAuthor);
|
||||
int maxScore = Math.max(fuzzyScore.fuzzyScore(searchAuthor, searchAuthor), fuzzyScore.fuzzyScore(author, author));
|
||||
double similarity = maxScore > 0 ? (double) score / maxScore : 0;
|
||||
return similarity >= 0.8;
|
||||
})
|
||||
.filter(hit -> {
|
||||
if (searchAuthor.isBlank()) return true;
|
||||
List<String> actualAuthorTokens = hit.getDocument().getAuthorNames().stream()
|
||||
.flatMap(name -> List.of(name.toLowerCase().split("\\s+")).stream())
|
||||
.toList();
|
||||
List<String> searchAuthorTokens = List.of(searchAuthor.toLowerCase().split("\\s+"));
|
||||
for (String actual : actualAuthorTokens) {
|
||||
for (String query : searchAuthorTokens) {
|
||||
int score = fuzzyScore.fuzzyScore(actual, query);
|
||||
int maxScore = Math.max(fuzzyScore.fuzzyScore(query, query), fuzzyScore.fuzzyScore(actual, actual));
|
||||
double similarity = maxScore > 0 ? (double) score / maxScore : 0;
|
||||
if (similarity >= 0.5) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
})
|
||||
.map(hit -> {
|
||||
GraphQLResponse.Document doc = hit.getDocument();
|
||||
BookMetadata metadata = new BookMetadata();
|
||||
|
||||
Reference in New Issue
Block a user