Skip to content

Commit

Permalink
Remove Levenshtein distance algoritm, using Hamming Distance as default
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewquang512 committed Nov 19, 2023
1 parent 2121e56 commit 6dc9f3c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 62 deletions.
7 changes: 0 additions & 7 deletions src/Type_Definitions/Common_Common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,13 @@ const commonDefs = gql`
checkSimilarPosts(
post1Id: String
post2Id: String
algo: similarImageAlgorithm
): checkSimilarPostsDetails!
}
input hashImageWithPostIdsInput {
postIds: [String]
}
enum similarImageAlgorithm {
HAMMING
LEVENSHTEIN
ALL
}
type checkSimilarPostsDetails {
isSimilar: Boolean
post1Imageurl: String
Expand Down
7 changes: 0 additions & 7 deletions src/Type_Definitions/Post_Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,9 @@ const postDefs = gql`
data: SimilarPostsInput!
limit: Int
after: String
algo: similarImageAlgorithm
): PostPagination!
}
enum similarImageAlgorithm {
HAMMING
LEVENSHTEIN
ALL
}
type PostPagination {
edges: [PostEdge!]!
pageInfo: PageInfo!
Expand Down
6 changes: 0 additions & 6 deletions src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ export const VOTE_COMMENT_ACTION = {
CANCEL: 'CANCEL',
};

export const SIMILAR_IMAGE_ALGORITHM = {
HAMMING: 'HAMMING',
LEVENSHTEIN: 'LEVENSHTEIN',
ALL: 'ALL',
};

export const DEFAULT_DEFINED_SKILL_LIST = [
'Abstract',
'Animal',
Expand Down
41 changes: 6 additions & 35 deletions src/resolvers/Common/compareImages.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
import Jimp from 'jimp';
import leven from 'leven';
import { SIMILAR_IMAGE_ALGORITHM } from '../../constants.js';

export function compareImages(
image1hash,
image2hash,
algo = SIMILAR_IMAGE_ALGORITHM.ALL,
) {
export function compareImages(image1hash, image2hash) {
// Perceived distance
const hammingDistance = findHammingDistance(image1hash, image2hash);
const levenDistance = leven(image1hash, image2hash);
console.log(`compareImages: hamming distance: ${hammingDistance.toFixed(3)}`);
console.log(`compareImages: leven distance: ${levenDistance.toFixed(3)}`);

switch (algo) {
case SIMILAR_IMAGE_ALGORITHM.HAMMING:
if (hammingDistance <= 10) {
console.log('compareImages: Images match!');
return true;
}
console.log('compareImages: Images do NOT match!');
return false;
case SIMILAR_IMAGE_ALGORITHM.LEVENSHTEIN:
if (levenDistance <= 12) {
console.log('compareImages: Images match!');
return true;
}
console.log('compareImages: Images do NOT match!');
return false;
case SIMILAR_IMAGE_ALGORITHM.ALL:
if (hammingDistance <= 10 || levenDistance <= 12) {
console.log('compareImages: Images match!');
return true;
}
console.log('compareImages: Images do NOT match!');
return false;
default:
throw Error('Similar Image Algorithm is not specified');
if (hammingDistance <= 10) {
console.log('compareImages: Images match!');
return true;
}
console.log('compareImages: Images do NOT match!');
return false;
}

const findHammingDistance = (str1 = '', str2 = '') => {
Expand Down
6 changes: 3 additions & 3 deletions src/resolvers/Mutation/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ const utilityMutation = {
/**
*
* @param {*} parent
* @param {{post1Id: string, post2Id: string, algo: string}} args
* @param {{post1Id: string, post2Id: string}} args
* @param {*} info
* @returns
*/
checkSimilarPosts: async (parent, args, info) => {
const { post1Id, post2Id, algo } = args;
const { post1Id, post2Id } = args;
const allPosts = await prisma.post.findMany({
where: {
id: {
Expand All @@ -79,7 +79,7 @@ const utilityMutation = {
const hash1 = images[0].hash;
const hash2 = images[1].hash;

const result = compareImages(hash1, hash2, algo);
const result = compareImages(hash1, hash2);
return {
isSimilar: result,
post1Imageurl: images[0].url,
Expand Down
8 changes: 4 additions & 4 deletions src/resolvers/Query/POST.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,12 @@ const postQuery = {

/**
* @param {*} parent
* @param {{data: {postId: string}, limit: number, after: string, algo: string}} args
* @param {{data: {postId: string}, limit: number, after: string}} args
* @param {*} info
* @returns
*/
similarPosts: async (parent, args, info) => {
const { after, limit = DEFAULT_LIMIT, algo } = args;
const { after, limit = DEFAULT_LIMIT } = args;
const { postId } = args.data ? args.data : {};

if (!postId) {
Expand Down Expand Up @@ -393,7 +393,7 @@ const postQuery = {
console.log('Init Stage wit referenceImage URL: ', referenceImage.url);
const initImages = initPosts.map((each) => each.image);
const isSimilarImageMap = initImages.map((each) =>
compareImages(referenceImage.hash, each.hash, algo),
compareImages(referenceImage.hash, each.hash),
);
for (const imgIndex in initImages) {
if (isSimilarImageMap[imgIndex]) {
Expand Down Expand Up @@ -436,7 +436,7 @@ const postQuery = {

const nextImages = nextPosts.map((each) => each.image);
const isSimilarImageMap = nextImages.map((each) =>
compareImages(referenceImage.hash, each.hash, algo),
compareImages(referenceImage.hash, each.hash),
);
for (const imgIndex in nextImages) {
if (result.length === limit) {
Expand Down

0 comments on commit 6dc9f3c

Please sign in to comment.