From 3009f14d22787760170a07323784bbecf4177126 Mon Sep 17 00:00:00 2001 From: ryanjung1998 Date: Tue, 27 Feb 2024 12:45:49 -0700 Subject: [PATCH] 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 {