Skip to content

Commit

Permalink
caretaking and country considerations
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjung1998 committed Feb 27, 2024
1 parent 5032665 commit 3009f14
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 36 deletions.
6 changes: 3 additions & 3 deletions backend/scanner-service/src/abstract/scanner.abstract.ts
Original file line number Diff line number Diff line change
@@ -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[];

Expand Down
8 changes: 5 additions & 3 deletions backend/scanner-service/src/controller/scanner.controller.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -12,10 +12,12 @@ class ScannerController {
res: Response,
next: NextFunction,
): Promise<Response<any, Record<string, any>> | 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);
Expand Down
7 changes: 4 additions & 3 deletions backend/scanner-service/src/repository/scanner.repository.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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();
Expand Down
90 changes: 76 additions & 14 deletions backend/scanner-service/src/service/scanner.service.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<arr.length;i++){
if(sustainable.includes(arr[i].material.toLowerCase())){
score += +arr[i].percentage;
console.log(arr[i].material, " is sustainable");
let notes = "";
if(arr.tags.length == 0){
notes = "Composition did not add to 100%";
}
for(let i=0;i<arr.tags.length;i++){
if(sustainable.includes(arr.tags[i].material.toLowerCase())){
score += +arr.tags[i].percentage;
notes = notes.concat(" " + arr.tags[i].material + " is sustainable ");
}
else{
console.log(arr[i].material, " is not sustainable");
notes = notes.concat(" " + arr.tags[i].material + " is not sustainable");
}
}
return score
if(arr.country != ""){
if(countries.includes(arr.country.toLowerCase())){
score += 50;
notes = notes.concat(". Produced in " + arr.country + " which is has strong environmental and labor regulations")
}
else{
notes = notes.concat(". Produced in " + arr.country + " which does not have strong environmental and labor regulations")
}
}
if(arr.lineDry){
score += 20;
notes = notes.concat(". Line drying is recommended, which consumes less energy");
}
if(arr.coldWater){
score += 20;
notes = notes.concat(". Cold water is recommended, which consumes less energy");
}
if(arr.dryClean){
score -= 20;
notes = notes.concat(". Dry cleaning is recommended, which consumes more energy");
}
const info: Info = {score: score,
notes: notes,};
return info;
}

public getTextFromImage = async (
Expand Down
25 changes: 24 additions & 1 deletion backend/scanner-service/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { z } from "zod";
import { ScannerSchema } from "./schema/scanner.schema.js";

export type Info = {
score: number;
notes: string;
}

export type Tag = {
material: string;
percentage: string;
};

export type Flags = {
country: string;
dryClean: boolean;
coldWater: boolean;
lineDry: boolean;
tags: Tag[];
};

export type ScannerRequest = z.infer<typeof ScannerSchema>["body"];

export const sustainable: Array<string> = ["organic cotton",
Expand Down Expand Up @@ -38,4 +51,14 @@ export const sustainable: Array<string> = ["organic cotton",
"yak wool",
"vegetable tanned leather",
"down",
"silk"]; //list of sustainable materials. must be lowercase
"silk",
"recycled"]; //list of sustainable materials. must be lowercase

export const countries: Array<string> = ["sweden",
"norway", "denmark", "finland",
"germany", "france", "netherlands",
"canada", "australia", "new zealand",
"switzerland", "austria", "united kingdom",
"belgium", "luxembourg", "iceland",
"japan", "south korea", "costa rica",
"uruguay"];
25 changes: 13 additions & 12 deletions backend/scanner-service/test/scanner.service.test.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 () => {
Expand All @@ -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<string> {
Expand Down

0 comments on commit 3009f14

Please sign in to comment.