-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.service.ts
144 lines (133 loc) · 3.61 KB
/
scanner.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import ScannerProvider from "../abstract/scanner.abstract.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): Flags => {
const materialRegex = /(100|\d{1,2})% *(\b\w+\b)/g;
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 = "";
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/gi)) {
console.log("dry cleaning");
dryClean = true;
}
if (text.match(/cold/gi)) {
console.log("cold water");
coldWater = true;
}
if (text.match(/(line|hang)\s+dry/gi)) {
lineDry = true;
}
const infoFound: Flags = {
country: country,
dryClean: dryClean,
coldWater: coldWater,
lineDry: lineDry,
tags: this.checkPercent(scannedTags),
};
return infoFound;
};
public checkPercent = (arr: Tag[]): Tag[] => {
let total = 0;
const res: Tag[] = [];
for (let i = 0; i < arr.length; i++) {
if (total < 100) {
res.push(arr[i]);
total += +arr[i].percentage;
}
}
if (total != 100) {
console.log("Composition did not add to 100%");
return [];
}
return res;
};
public getSustainability = (arr: Flags): Info => {
let score = 0;
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 {
notes = notes.concat(
" " + arr.tags[i].material + " is not sustainable",
);
}
}
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 (
scannerRequest: ScannerRequest,
): Promise<string> => {
return await this.provider.getTextFromImage(scannerRequest);
};
}
export default ScannerService;