🔍 CV match scorer WIP
This commit is contained in:
@@ -15,7 +15,11 @@ import {
|
||||
CV_CLEANUP,
|
||||
} from "../constants/action-types";
|
||||
import { refineQuery } from "../shared/utils/filenameparser.utils";
|
||||
import { matchScorer } from "../shared/utils/searchmatchscorer.utils";
|
||||
import {
|
||||
matchScorer,
|
||||
compareCoverImageHashes,
|
||||
} from "../shared/utils/searchmatchscorer.utils";
|
||||
import { assign, each } from "lodash";
|
||||
|
||||
export async function walkFolder(path: string): Promise<Array<IFolderData>> {
|
||||
return axios
|
||||
@@ -108,7 +112,7 @@ export const getRecentlyImportedComicBooks = (options) => async (dispatch) => {
|
||||
|
||||
export const fetchComicVineMatches = (searchPayload) => (dispatch) => {
|
||||
try {
|
||||
const issueString = searchPayload.rawFileDetails.path.split("/").pop();
|
||||
const issueString = searchPayload.rawFileDetails.name;
|
||||
const issueSearchQuery: IComicVineSearchQuery = refineQuery(issueString);
|
||||
let seriesSearchQuery: IComicVineSearchQuery = {} as IComicVineSearchQuery;
|
||||
if (searchPayload.rawFileDetails.containedIn !== "comics") {
|
||||
@@ -134,20 +138,27 @@ export const fetchComicVineMatches = (searchPayload) => (dispatch) => {
|
||||
offset: "0",
|
||||
resources: "issue",
|
||||
},
|
||||
transformResponse: [
|
||||
(r) => {
|
||||
const searchMatches = JSON.parse(r);
|
||||
return matchScorer(searchMatches.results, {
|
||||
issue: issueSearchQuery,
|
||||
series: seriesSearchQuery,
|
||||
});
|
||||
},
|
||||
],
|
||||
transformResponse: (r) => JSON.parse(r),
|
||||
})
|
||||
.then((response) => {
|
||||
const searchMatches = response.data.results;
|
||||
each(searchMatches, (match) => assign(match, { score: 0 }));
|
||||
const results = matchScorer(
|
||||
searchMatches,
|
||||
{
|
||||
issue: issueSearchQuery,
|
||||
series: seriesSearchQuery,
|
||||
},
|
||||
searchPayload.rawFileDetails,
|
||||
);
|
||||
const scoredResults = compareCoverImageHashes(
|
||||
searchPayload.rawFileDetails,
|
||||
results,
|
||||
);
|
||||
|
||||
dispatch({
|
||||
type: CV_SEARCH_SUCCESS,
|
||||
searchResults: response.data,
|
||||
searchResults: scoredResults,
|
||||
searchQueryObject: {
|
||||
issue: issueSearchQuery,
|
||||
series: seriesSearchQuery,
|
||||
@@ -159,6 +170,7 @@ export const fetchComicVineMatches = (searchPayload) => (dispatch) => {
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: CV_CLEANUP,
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ import ComicVineSearchForm from "./ComicVineSearchForm";
|
||||
|
||||
import { css } from "@emotion/react";
|
||||
import PuffLoader from "react-spinners/PuffLoader";
|
||||
import { isEmpty, isUndefined } from "lodash";
|
||||
import { isEmpty, isUndefined, isEqual } from "lodash";
|
||||
import { IExtractedComicBookCoverFile, RootState } from "threetwo-ui-typings";
|
||||
import { fetchComicVineMatches } from "../actions/fileops.actions";
|
||||
import { Drawer, Divider } from "antd";
|
||||
@@ -27,6 +27,7 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => {
|
||||
|
||||
const comicVineSearchResults = useSelector(
|
||||
(state: RootState) => state.comicInfo.searchResults,
|
||||
|
||||
);
|
||||
const comicVineSearchQueryObject = useSelector(
|
||||
(state: RootState) => state.comicInfo.searchQuery,
|
||||
@@ -135,8 +136,8 @@ export const ComicDetail = ({}: ComicDetailProps): ReactElement => {
|
||||
|
||||
<div className="search-results-container">
|
||||
{!isEmpty(comicVineSearchResults) && (
|
||||
<MatchResult matchData={comicVineSearchResults} />
|
||||
)}
|
||||
<MatchResult matchData={comicVineSearchResults} />
|
||||
)}
|
||||
</div>
|
||||
</Drawer>
|
||||
</>
|
||||
|
||||
@@ -16,9 +16,12 @@ export const MatchResult = (props: MatchResultProps) => {
|
||||
</thead>
|
||||
<tbody>
|
||||
{map(props.matchData, (match, idx) => {
|
||||
|
||||
return (
|
||||
|
||||
<tr className="search-result" key={idx}>
|
||||
<td>
|
||||
{match.score}
|
||||
<img className="cover-image" src={match.image.thumb_url} />
|
||||
</td>
|
||||
<td className="search-result-details">
|
||||
|
||||
@@ -17,7 +17,7 @@ function comicinfoReducer(state = initialState, action) {
|
||||
inProgress: true,
|
||||
};
|
||||
case CV_SEARCH_SUCCESS:
|
||||
console.log("ACTION", action);
|
||||
console.log("ASDASD", action)
|
||||
return {
|
||||
...state,
|
||||
searchResults: action.searchResults,
|
||||
|
||||
@@ -158,7 +158,6 @@ export const extractNumerals = (inputString: string): MatchArray[string] => {
|
||||
|
||||
export const refineQuery = (inputString) => {
|
||||
const queryObj = tokenize(inputString);
|
||||
console.log("QWEQWEQWE", queryObj);
|
||||
const removedYears = xor(
|
||||
queryObj.sentence_tokens.normalized,
|
||||
queryObj.years.yearMatches,
|
||||
|
||||
@@ -1,17 +1,50 @@
|
||||
import { each, isUndefined, isNull } from "lodash";
|
||||
const stringSimilarity = require("string-similarity");
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2015 Rishi Ghan
|
||||
*
|
||||
The MIT License (MIT)
|
||||
|
||||
export const matchScorer = (searchMatches, searchQuery) => {
|
||||
Copyright (c) 2015 Rishi Ghan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Revision History:
|
||||
* Initial: 2021/07/29 Rishi Ghan
|
||||
*/
|
||||
|
||||
import { each, map, isUndefined, isNull, assign } from "lodash";
|
||||
const stringSimilarity = require("string-similarity");
|
||||
import axios from "axios";
|
||||
|
||||
export const matchScorer = (searchMatches, searchQuery, rawFileDetails) => {
|
||||
// 1. Check if it exists in the db (score: 0)
|
||||
// 2. Check if issue name matches strongly (score: ++)
|
||||
// 3. Check if issue number matches strongly (score: ++)
|
||||
// 4. Check if issue covers hash match strongly (score: +++)
|
||||
// 5. Check if issue year matches strongly (score: +)
|
||||
const score = 0;
|
||||
each(searchMatches, (match, idx) => {
|
||||
console.log("SEARCH QUERY IN SMS:", searchQuery);
|
||||
console.log("MATCH NAME:", match);
|
||||
match.score = 0;
|
||||
|
||||
each(searchMatches, (match, idx) => {
|
||||
// check for the issue name match
|
||||
|
||||
if (
|
||||
!isNull(searchQuery.issue.searchParams.searchTerms.name) &&
|
||||
!isNull(match.name)
|
||||
@@ -21,7 +54,6 @@ export const matchScorer = (searchMatches, searchQuery) => {
|
||||
match.name,
|
||||
);
|
||||
match.score = issueNameScore;
|
||||
console.log("name score" + idx + ":", issueNameScore);
|
||||
}
|
||||
|
||||
// issue number matches
|
||||
@@ -33,12 +65,37 @@ export const matchScorer = (searchMatches, searchQuery) => {
|
||||
parseInt(searchQuery.issue.searchParams.searchTerms.number, 10) ===
|
||||
parseInt(match.issue_number, 10)
|
||||
) {
|
||||
match.score += 2;
|
||||
console.log(match.score);
|
||||
match.score += 1;
|
||||
}
|
||||
}
|
||||
});
|
||||
return searchMatches;
|
||||
|
||||
// check for the issue name match
|
||||
return match;
|
||||
});
|
||||
|
||||
return searchMatches;
|
||||
};
|
||||
|
||||
export const compareCoverImageHashes = (original, matches) => {
|
||||
// cover matches
|
||||
// calculate the image hashes of the covers and compare the Levenshtein Distance
|
||||
each(matches, async (match) => {
|
||||
const result = await axios.request({
|
||||
url: "http://localhost:3000/api/imagetransformation/calculateLevenshteinDistance",
|
||||
method: "POST",
|
||||
data: {
|
||||
imagePath: original.path,
|
||||
imagePath2: match.image.small_url,
|
||||
options: {
|
||||
match_id: match.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (result.data.levenshteinDistance === 0) {
|
||||
match.score += 4;
|
||||
} else {
|
||||
match.score -= 4;
|
||||
}
|
||||
});
|
||||
return matches;
|
||||
};
|
||||
|
||||
@@ -26,7 +26,7 @@ const port: number = Number(process.env.PORT) || 8050; // set our port
|
||||
// Send index.html on root request
|
||||
app.use(express.static("dist"));
|
||||
|
||||
export function opdsRouter() {
|
||||
export const opdsRouter = () => {
|
||||
const path_of_books = "/Users/rishi/work/threetwo/src/server/comics";
|
||||
router.use("/opds", async (req, res, next) => {
|
||||
return buildAsync(
|
||||
@@ -87,7 +87,8 @@ export function opdsRouter() {
|
||||
});
|
||||
|
||||
return router;
|
||||
}
|
||||
};
|
||||
|
||||
app.get("/", (req: Request, res: Response) => {
|
||||
console.log("sending index.html");
|
||||
res.sendFile("/dist/index.html");
|
||||
|
||||
Reference in New Issue
Block a user