From c78c9af565082d535ec7339d2441963fc6f06719 Mon Sep 17 00:00:00 2001 From: ryanjung1998 Date: Mon, 26 Feb 2024 09:43:43 -0700 Subject: [PATCH 1/5] Score changes pt.1 --- .../src/abstract/scanner.abstract.ts | 4 +++ .../src/repository/scanner.repository.ts | 7 +++- .../src/service/scanner.service.ts | 32 ++++++++++++++++++- .../test/scanner.service.test.ts | 7 +++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/backend/scanner-service/src/abstract/scanner.abstract.ts b/backend/scanner-service/src/abstract/scanner.abstract.ts index 933bd6c..4dc24da 100644 --- a/backend/scanner-service/src/abstract/scanner.abstract.ts +++ b/backend/scanner-service/src/abstract/scanner.abstract.ts @@ -3,6 +3,10 @@ import { ScannerRequest, Tag } from "../types"; abstract class ScannerProvider { abstract getMaterials(text: string): Tag[]; + abstract getSustainability(arr: Tag[]): number; + + abstract checkPercent(arr: Tag[]): Tag[]; + abstract getTextFromImage(scannerRequest: ScannerRequest): Promise; } diff --git a/backend/scanner-service/src/repository/scanner.repository.ts b/backend/scanner-service/src/repository/scanner.repository.ts index 7bbc5d0..0c45363 100644 --- a/backend/scanner-service/src/repository/scanner.repository.ts +++ b/backend/scanner-service/src/repository/scanner.repository.ts @@ -7,7 +7,12 @@ class ScannerRepository implements ScannerProvider { public getMaterials = (text: string): Tag[] => { throw new Error("Method not implemented."); }; - + public checkPercent = (arr: Tag[]): Tag[] => { + throw new Error("Method not implemented."); + }; + public getSustainability = (arr: Tag[]): number => { + throw new Error("Method not implemented."); + }; public getTextFromImage = async ( scannerRequest: ScannerRequest, ): Promise => { diff --git a/backend/scanner-service/src/service/scanner.service.ts b/backend/scanner-service/src/service/scanner.service.ts index 4956baa..7a31fb1 100644 --- a/backend/scanner-service/src/service/scanner.service.ts +++ b/backend/scanner-service/src/service/scanner.service.ts @@ -1,5 +1,5 @@ import ScannerProvider from "../abstract/scanner.abstract.js"; -import { ScannerRequest, Tag } from "../types.js"; +import { ScannerRequest, Tag, sustainable } from "../types.js"; class ScannerService implements ScannerProvider { constructor(private provider: ScannerProvider) { @@ -23,6 +23,36 @@ class ScannerService implements ScannerProvider { return scannedTags; }; + public checkPercent = (arr: Tag[]): Tag[] => { //for checking if percentages add to 100 and ignoring any excess materials + let total = 0; + let res: Tag[] = []; + for(let i=0;i { //gives a score for the sustainability of the article of clothing + let score = 0; + for(let i=0;i => { diff --git a/backend/scanner-service/test/scanner.service.test.ts b/backend/scanner-service/test/scanner.service.test.ts index dcb1338..5d53863 100644 --- a/backend/scanner-service/test/scanner.service.test.ts +++ b/backend/scanner-service/test/scanner.service.test.ts @@ -47,7 +47,12 @@ class MockScannerProvider implements ScannerProvider { getMaterials(text: string): Tag[] { throw new Error("Method not implemented."); } - + checkPercent = (arr: Tag[]): Tag[] => { + throw new Error("Method not implemented."); + } + getSustainability = (arr: Tag[]): number => { + throw new Error("Method not implemented."); + } getTextFromImage(scannerRequest: ScannerRequest): Promise { const { imageUrl } = scannerRequest; if (!imageUrl) { From e7b42728207da9955b335a6eca7fc4eb4a7fb047 Mon Sep 17 00:00:00 2001 From: ryanjung1998 Date: Mon, 26 Feb 2024 09:47:18 -0700 Subject: [PATCH 2/5] Score changes pt.2 --- backend/scanner-service/src/types.ts | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/backend/scanner-service/src/types.ts b/backend/scanner-service/src/types.ts index d4c3c29..7955dd8 100644 --- a/backend/scanner-service/src/types.ts +++ b/backend/scanner-service/src/types.ts @@ -7,3 +7,35 @@ export type Tag = { }; export type ScannerRequest = z.infer["body"]; + +export const sustainable: Array = ["organic cotton", +"recycled cotton", +"hemp", +"linen", +"bamboo linen", +"cork", +"econyl", +"recycled polyester", +"deadstock", +"lyocell", +"modal", +"bamboo lyocell", +"ecovero", +"piñatex", +"bananatex", +"scoby leather", +"s.cafe", +"brewed protein", +"apple leather", +"woocoa", +"cupro", +"qmilk", +"sheep wool", +"merino wool", +"alpaca wool", +"cashmere", +"camel", +"yak wool", +"vegetable tanned leather", +"down", +"silk"]; //list of sustainable materials. must be lowercase \ No newline at end of file From 5032665c16010829466a852a0ca9e46b95fbb3fa Mon Sep 17 00:00:00 2001 From: ryanjung1998 Date: Mon, 26 Feb 2024 09:48:02 -0700 Subject: [PATCH 3/5] Score changes pt.3 --- backend/scanner-service/src/controller/scanner.controller.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/scanner-service/src/controller/scanner.controller.ts b/backend/scanner-service/src/controller/scanner.controller.ts index 3a36e6c..788d343 100644 --- a/backend/scanner-service/src/controller/scanner.controller.ts +++ b/backend/scanner-service/src/controller/scanner.controller.ts @@ -15,7 +15,8 @@ class ScannerController { try { const text: string = await this.service.getTextFromImage(req.body); const materials: Tag[] = this.service.getMaterials(text); - return res.status(201).json(materials); + const score: number = this.service.getSustainability(materials); + return res.status(201).json(score); } catch (e) { next(e); } From 3009f14d22787760170a07323784bbecf4177126 Mon Sep 17 00:00:00 2001 From: ryanjung1998 Date: Tue, 27 Feb 2024 12:45:49 -0700 Subject: [PATCH 4/5] caretaking and country considerations --- .../src/abstract/scanner.abstract.ts | 6 +- .../src/controller/scanner.controller.ts | 8 +- .../src/repository/scanner.repository.ts | 7 +- .../src/service/scanner.service.ts | 90 ++++++++++++++++--- backend/scanner-service/src/types.ts | 25 +++++- .../test/scanner.service.test.ts | 25 +++--- 6 files changed, 125 insertions(+), 36 deletions(-) diff --git a/backend/scanner-service/src/abstract/scanner.abstract.ts b/backend/scanner-service/src/abstract/scanner.abstract.ts index 4dc24da..6282aba 100644 --- a/backend/scanner-service/src/abstract/scanner.abstract.ts +++ b/backend/scanner-service/src/abstract/scanner.abstract.ts @@ -1,9 +1,9 @@ -import { ScannerRequest, Tag } from "../types"; +import { ScannerRequest, Tag, Info, Flags } from "../types"; abstract class ScannerProvider { - abstract getMaterials(text: string): Tag[]; + abstract getMaterials(text: string): Flags; - abstract getSustainability(arr: Tag[]): number; + abstract getSustainability(arr: Flags): Info; abstract checkPercent(arr: Tag[]): Tag[]; diff --git a/backend/scanner-service/src/controller/scanner.controller.ts b/backend/scanner-service/src/controller/scanner.controller.ts index 788d343..88c8aa6 100644 --- a/backend/scanner-service/src/controller/scanner.controller.ts +++ b/backend/scanner-service/src/controller/scanner.controller.ts @@ -1,4 +1,4 @@ -import { ScannerRequest, Tag } from "../types.js"; +import { ScannerRequest, Tag, Info, Flags} from "../types.js"; import ScannerProvider from "../abstract/scanner.abstract.js"; import { Request, Response, NextFunction } from "express"; @@ -12,10 +12,12 @@ class ScannerController { res: Response, next: NextFunction, ): Promise> | void> => { + let notes = ""; try { const text: string = await this.service.getTextFromImage(req.body); - const materials: Tag[] = this.service.getMaterials(text); - const score: number = this.service.getSustainability(materials); + // const text: string = "20% Cashmere 50% Recycled Cotton 30% Cork made in Canada dry clean only"; + const materials: Flags = this.service.getMaterials(text); + const score: Info = this.service.getSustainability(materials); return res.status(201).json(score); } catch (e) { next(e); diff --git a/backend/scanner-service/src/repository/scanner.repository.ts b/backend/scanner-service/src/repository/scanner.repository.ts index 0c45363..26e853a 100644 --- a/backend/scanner-service/src/repository/scanner.repository.ts +++ b/backend/scanner-service/src/repository/scanner.repository.ts @@ -1,16 +1,16 @@ import ScannerProvider from "../abstract/scanner.abstract.js"; import { createWorker } from "tesseract.js"; -import { ScannerRequest, Tag } from "../types.js"; +import { ScannerRequest, Tag, Info, Flags} from "../types.js"; import { TesseractServiceError } from "../error/tesseract.error.js"; class ScannerRepository implements ScannerProvider { - public getMaterials = (text: string): Tag[] => { + public getMaterials = (text: string): Flags => { throw new Error("Method not implemented."); }; public checkPercent = (arr: Tag[]): Tag[] => { throw new Error("Method not implemented."); }; - public getSustainability = (arr: Tag[]): number => { + public getSustainability = (arr: Flags): Info => { throw new Error("Method not implemented."); }; public getTextFromImage = async ( @@ -22,6 +22,7 @@ class ScannerRepository implements ScannerProvider { scannerRequest.imageUrl, ); await worker.terminate(); + console.log(ret.data.text); return ret.data.text; } catch (e) { throw new TesseractServiceError(); diff --git a/backend/scanner-service/src/service/scanner.service.ts b/backend/scanner-service/src/service/scanner.service.ts index 7a31fb1..ff1be6e 100644 --- a/backend/scanner-service/src/service/scanner.service.ts +++ b/backend/scanner-service/src/service/scanner.service.ts @@ -1,26 +1,61 @@ import ScannerProvider from "../abstract/scanner.abstract.js"; -import { ScannerRequest, Tag, sustainable } from "../types.js"; +import { ScannerRequest, Tag, sustainable, Info, Flags, countries} from "../types.js"; class ScannerService implements ScannerProvider { constructor(private provider: ScannerProvider) { this.provider = provider; } - - public getMaterials = (text: string): Tag[] => { - const regex = /(100|\d{1,2})% *(\b\w+\b)/g; - const matches = text.matchAll(regex); + //want to add functions that detect made in sustainable countries and care instructions + public getMaterials = (text: string): Flags => { + const materialRegex = /(100|\d{1,2})% *(\b\w+\b)/g; + const countryRegex = /(Made\s+in|Product\s+of) *(\b\w+\b)/ig; + const materialMatches = text.matchAll(materialRegex); + const countryMatches = text.matchAll(countryRegex); + let dryClean = false; + let coldWater = false; + let lineDry = false; + + const scannedTags: Tag[] = []; + let country = ""; - for (const match of matches) { + for (const match of materialMatches) { if (match) { scannedTags.push({ material: match[2], percentage: match[1], }); + console.log(match[2]); + } + } + + for (const match of countryMatches) { + if (match) { + country = match[2]; + console.log("Country: ", country); } } + + if (text.match(/dry\s+clean/ig)){//checking if dry cleaning is recommended + console.log("dry cleaning"); + dryClean = true; + } - return scannedTags; + if (text.match(/cold/ig)){//checking if cold is recommended + console.log("cold water"); + coldWater = true; + } + if (text.match(/(line|hang)\s+dry/ig)){//checking if line drying is recommended + lineDry = true; + } + const infoFound: Flags = { + country: country, + dryClean: dryClean, + coldWater: coldWater, + lineDry: lineDry, + tags: this.checkPercent(scannedTags), + }; + return infoFound; }; public checkPercent = (arr: Tag[]): Tag[] => { //for checking if percentages add to 100 and ignoring any excess materials @@ -39,18 +74,45 @@ class ScannerService implements ScannerProvider { return res; } - public getSustainability = (arr: Tag[]): number => { //gives a score for the sustainability of the article of clothing + public getSustainability = (arr: Flags): Info => { //gives a score for the sustainability of the article of clothing let score = 0; - for(let i=0;i["body"]; export const sustainable: Array = ["organic cotton", @@ -38,4 +51,14 @@ export const sustainable: Array = ["organic cotton", "yak wool", "vegetable tanned leather", "down", -"silk"]; //list of sustainable materials. must be lowercase \ No newline at end of file +"silk", +"recycled"]; //list of sustainable materials. must be lowercase + +export const countries: Array = ["sweden", +"norway", "denmark", "finland", +"germany", "france", "netherlands", +"canada", "australia", "new zealand", +"switzerland", "austria", "united kingdom", +"belgium", "luxembourg", "iceland", +"japan", "south korea", "costa rica", +"uruguay"]; \ No newline at end of file diff --git a/backend/scanner-service/test/scanner.service.test.ts b/backend/scanner-service/test/scanner.service.test.ts index 5d53863..99e4424 100644 --- a/backend/scanner-service/test/scanner.service.test.ts +++ b/backend/scanner-service/test/scanner.service.test.ts @@ -1,7 +1,7 @@ import ScannerProvider from "../src/abstract/scanner.abstract.js"; import { TesseractServiceError } from "../src/error/tesseract.error.js"; import ScannerService from "../src/service/scanner.service.js"; -import { ScannerRequest, Tag } from "../src/types.js"; +import { ScannerRequest, Tag, Info, Flags } from "../src/types.js"; describe("ScannerService Unit Tests", () => { let scannerService: ScannerService; @@ -12,16 +12,17 @@ describe("ScannerService Unit Tests", () => { scannerService = new ScannerService(mockScannerProvider); }); - test("Get materials from text", () => { - const materials: Tag[] = scannerService.getMaterials( - "90% Cotton 10% Nylon", + test("Get info from text", () => { + const info: Flags = scannerService.getMaterials( + "90% Cotton 10% Nylon Made in Canada Dry clean only", ); - expect(materials).toBeInstanceOf(Array); - expect(materials).toHaveLength(2); - expect(materials[0].material).toBe("Cotton"); - expect(materials[0].percentage).toBe("90"); - expect(materials[1].material).toBe("Nylon"); - expect(materials[1].percentage).toBe("10"); + expect(info).toBeInstanceOf(Object); + expect(info.country).toBe("Canada"); + expect(info.tags).toHaveLength(2); + expect(info.tags[0].material).toBe("Cotton"); + expect(info.tags[0].percentage).toBe("90"); + expect(info.tags[1].material).toBe("Nylon"); + expect(info.tags[1].percentage).toBe("10"); }); test("Get text from image", async () => { @@ -44,13 +45,13 @@ describe("ScannerService Unit Tests", () => { }); class MockScannerProvider implements ScannerProvider { - getMaterials(text: string): Tag[] { + getMaterials(text: string): Flags { throw new Error("Method not implemented."); } checkPercent = (arr: Tag[]): Tag[] => { throw new Error("Method not implemented."); } - getSustainability = (arr: Tag[]): number => { + getSustainability = (arr: Flags): Info => { throw new Error("Method not implemented."); } getTextFromImage(scannerRequest: ScannerRequest): Promise { From 6a90d0dcfd248886ba174f222355915a6e363346 Mon Sep 17 00:00:00 2001 From: Axeloooo Date: Sun, 31 Mar 2024 15:45:54 -0600 Subject: [PATCH 5/5] fix: Resolved lint errors --- .../src/controller/scanner.controller.ts | 6 +- .../src/service/scanner.service.ts | 111 ++++++++++-------- .../test/scanner.service.test.ts | 4 +- 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/backend/scanner-service/src/controller/scanner.controller.ts b/backend/scanner-service/src/controller/scanner.controller.ts index 88c8aa6..5607cf8 100644 --- a/backend/scanner-service/src/controller/scanner.controller.ts +++ b/backend/scanner-service/src/controller/scanner.controller.ts @@ -1,4 +1,4 @@ -import { ScannerRequest, Tag, Info, Flags} from "../types.js"; +import { ScannerRequest, Tag, Info, Flags } from "../types.js"; import ScannerProvider from "../abstract/scanner.abstract.js"; import { Request, Response, NextFunction } from "express"; @@ -12,12 +12,10 @@ class ScannerController { res: Response, next: NextFunction, ): Promise> | void> => { - let notes = ""; try { const text: string = await this.service.getTextFromImage(req.body); - // const text: string = "20% Cashmere 50% Recycled Cotton 30% Cork made in Canada dry clean only"; const materials: Flags = this.service.getMaterials(text); - const score: Info = this.service.getSustainability(materials); + const score: Info = this.service.getSustainability(materials); return res.status(201).json(score); } catch (e) { next(e); diff --git a/backend/scanner-service/src/service/scanner.service.ts b/backend/scanner-service/src/service/scanner.service.ts index ff1be6e..adf7eed 100644 --- a/backend/scanner-service/src/service/scanner.service.ts +++ b/backend/scanner-service/src/service/scanner.service.ts @@ -1,21 +1,27 @@ import ScannerProvider from "../abstract/scanner.abstract.js"; -import { ScannerRequest, Tag, sustainable, Info, Flags, countries} from "../types.js"; +import { + ScannerRequest, + Tag, + sustainable, + Info, + Flags, + countries, +} from "../types.js"; class ScannerService implements ScannerProvider { constructor(private provider: ScannerProvider) { this.provider = provider; } - //want to add functions that detect made in sustainable countries and care instructions + public getMaterials = (text: string): Flags => { const materialRegex = /(100|\d{1,2})% *(\b\w+\b)/g; - const countryRegex = /(Made\s+in|Product\s+of) *(\b\w+\b)/ig; + const countryRegex = /(Made\s+in|Product\s+of) *(\b\w+\b)/gi; const materialMatches = text.matchAll(materialRegex); const countryMatches = text.matchAll(countryRegex); let dryClean = false; let coldWater = false; let lineDry = false; - - + const scannedTags: Tag[] = []; let country = ""; @@ -35,17 +41,17 @@ class ScannerService implements ScannerProvider { console.log("Country: ", country); } } - - if (text.match(/dry\s+clean/ig)){//checking if dry cleaning is recommended + + if (text.match(/dry\s+clean/gi)) { console.log("dry cleaning"); dryClean = true; } - if (text.match(/cold/ig)){//checking if cold is recommended + if (text.match(/cold/gi)) { console.log("cold water"); coldWater = true; } - if (text.match(/(line|hang)\s+dry/ig)){//checking if line drying is recommended + if (text.match(/(line|hang)\s+dry/gi)) { lineDry = true; } const infoFound: Flags = { @@ -58,63 +64,76 @@ class ScannerService implements ScannerProvider { return infoFound; }; - public checkPercent = (arr: Tag[]): Tag[] => { //for checking if percentages add to 100 and ignoring any excess materials + public checkPercent = (arr: Tag[]): Tag[] => { let total = 0; - let res: Tag[] = []; - for(let i=0;i { //gives a score for the sustainability of the article of clothing + public getSustainability = (arr: Flags): Info => { let score = 0; let notes = ""; - if(arr.tags.length == 0){ + if (arr.tags.length == 0) { notes = "Composition did not add to 100%"; } - for(let i=0;i => { diff --git a/backend/scanner-service/test/scanner.service.test.ts b/backend/scanner-service/test/scanner.service.test.ts index 99e4424..dd9adc8 100644 --- a/backend/scanner-service/test/scanner.service.test.ts +++ b/backend/scanner-service/test/scanner.service.test.ts @@ -50,10 +50,10 @@ class MockScannerProvider implements ScannerProvider { } checkPercent = (arr: Tag[]): Tag[] => { throw new Error("Method not implemented."); - } + }; getSustainability = (arr: Flags): Info => { throw new Error("Method not implemented."); - } + }; getTextFromImage(scannerRequest: ScannerRequest): Promise { const { imageUrl } = scannerRequest; if (!imageUrl) {