feat(flyer-import): integrate AI-based flyer parsing with image support
- Add support for PNG, JPEG, and WebP image formats in flyer import - Replace external importer service with internal AI-based parsing pipeline - Add new services: TextExtractorService, AiFlyerParserService, FlyerNormalizerService - Integrate Mistral AI, pdf-parse, and tesseract.js dependencies - Add quality confidence indicators and warning panels in Flutter UI - Update package.json with new dependencies and transform ignore patterns - Add documentation for flyer importer system - Add Kilo AI planning file for Happy Island project BREAKING CHANGE: Flyer import now uses internal AI parsing instead of external importer service
This commit is contained in:
@@ -4,7 +4,7 @@ import { ThrottlerGuard, ThrottlerModule } from '@nestjs/throttler';
|
||||
import { JwtAuthGuard } from './auth/jwt-auth.guard';
|
||||
import { RolesGuard } from './auth/roles.guard';
|
||||
|
||||
describe('App security configuration', () => {
|
||||
describe('App security configuration', () => {
|
||||
function getAppModuleClass() {
|
||||
process.env.JWT_SECRET = process.env.JWT_SECRET ?? 'test-secret';
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
|
||||
@@ -18,6 +18,9 @@ const ALLOWED_MIMES = [
|
||||
'application/pdf',
|
||||
'application/octet-stream',
|
||||
'text/plain',
|
||||
'image/png',
|
||||
'image/jpeg',
|
||||
'image/webp',
|
||||
];
|
||||
|
||||
@Controller('flyer-import')
|
||||
@@ -41,7 +44,7 @@ export class FlyerImportController {
|
||||
throw new BadRequestException('Ingen fil skickades med.');
|
||||
}
|
||||
if (!ALLOWED_MIMES.includes(file.mimetype)) {
|
||||
throw new BadRequestException('Otillåten filtyp. Använd PDF eller textfil.');
|
||||
throw new BadRequestException('Otillåten filtyp. Använd PDF, textfil eller bild (PNG, JPEG, WebP).');
|
||||
}
|
||||
|
||||
const userId =
|
||||
|
||||
@@ -2,10 +2,18 @@ import { Module } from '@nestjs/common';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { FlyerImportController } from './flyer-import.controller';
|
||||
import { FlyerImportService } from './flyer-import.service';
|
||||
import { TextExtractorService } from './services/text-extractor.service';
|
||||
import { AiFlyerParserService } from './services/ai-flyer-parser.service';
|
||||
import { FlyerNormalizerService } from './services/flyer-normalizer.service';
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
controllers: [FlyerImportController],
|
||||
providers: [FlyerImportService],
|
||||
providers: [
|
||||
FlyerImportService,
|
||||
TextExtractorService,
|
||||
AiFlyerParserService,
|
||||
FlyerNormalizerService,
|
||||
],
|
||||
})
|
||||
export class FlyerImportModule {}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
Logger,
|
||||
ServiceUnavailableException,
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
Logger,
|
||||
ServiceUnavailableException,
|
||||
} from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
@@ -12,8 +12,9 @@ import {
|
||||
FlyerImportMatchVia,
|
||||
FlyerImportResponse,
|
||||
} from './dto/flyer-import.response';
|
||||
|
||||
const IMPORTER_SERVICE_URL = process.env.IMPORTER_SERVICE_URL || 'http://importer-api:3001';
|
||||
import { TextExtractorService } from './services/text-extractor.service';
|
||||
import { AiFlyerParserService } from './services/ai-flyer-parser.service';
|
||||
import { FlyerNormalizerService } from './services/flyer-normalizer.service';
|
||||
|
||||
type FlyerParseItem = {
|
||||
rawName: string;
|
||||
@@ -53,10 +54,15 @@ type ProductLite = {
|
||||
export class FlyerImportService {
|
||||
private readonly logger = new Logger(FlyerImportService.name);
|
||||
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly textExtractor: TextExtractorService,
|
||||
private readonly aiParser: AiFlyerParserService,
|
||||
private readonly normalizer: FlyerNormalizerService,
|
||||
) {}
|
||||
|
||||
async parseAndMatch(file: Express.Multer.File, userId: number): Promise<FlyerImportResponse> {
|
||||
const parsed = await this.parseViaImporter(file);
|
||||
const parsed = await this.parseViaInternal(file);
|
||||
|
||||
const [products, aliases] = await Promise.all([
|
||||
this.prisma.product.findMany({
|
||||
@@ -371,43 +377,59 @@ export class FlyerImportService {
|
||||
return allowed.has(cleaned) ? cleaned : cleaned;
|
||||
}
|
||||
|
||||
private async parseViaImporter(file: Express.Multer.File): Promise<FlyerParseResponse> {
|
||||
const form = new FormData();
|
||||
form.append(
|
||||
'file',
|
||||
new Blob([new Uint8Array(file.buffer)], { type: file.mimetype }),
|
||||
file.originalname,
|
||||
);
|
||||
form.append('retailer', 'willys');
|
||||
|
||||
let response: Response;
|
||||
private async parseViaInternal(file: Express.Multer.File): Promise<FlyerParseResponse> {
|
||||
try {
|
||||
response = await fetch(`${IMPORTER_SERVICE_URL}/api/flyer/parse`, {
|
||||
method: 'POST',
|
||||
body: form,
|
||||
});
|
||||
} catch (err) {
|
||||
this.logger.error(`Kunde inte nå importer-api för flyer-parse: ${String(err)}`);
|
||||
throw new ServiceUnavailableException('Importer-tjänsten är inte tillgänglig just nu.');
|
||||
}
|
||||
this.logger.debug(`Parsing flyer file: ${file.originalname}`);
|
||||
|
||||
if (!response.ok) {
|
||||
let message = `Importer-tjänsten svarade ${response.status}`;
|
||||
try {
|
||||
const body = (await response.json()) as { message?: string };
|
||||
if (typeof body.message === 'string' && body.message.trim()) {
|
||||
message = body.message;
|
||||
}
|
||||
} catch {
|
||||
// ignore parse issues
|
||||
// 1. Extrahera text från PDF/bild
|
||||
const text = await this.textExtractor.extractText(
|
||||
file.buffer,
|
||||
file.mimetype,
|
||||
file.originalname,
|
||||
);
|
||||
|
||||
// 2. Skicka till Mistral Tiny
|
||||
const aiItems = await this.aiParser.parseWithAI(text);
|
||||
|
||||
// 3. Normalisera resultatet
|
||||
const normalizedItems = this.normalizer.normalize(aiItems);
|
||||
|
||||
// 4. Konvertera till intern FlyerParseItem-format
|
||||
const items: FlyerParseItem[] = normalizedItems.map((item) => ({
|
||||
rawName: item.rawName,
|
||||
normalizedName: item.normalizedName,
|
||||
category: item.categoryHint,
|
||||
price: item.price,
|
||||
priceUnit: item.priceUnit,
|
||||
comparisonPrice: item.comparisonPrice,
|
||||
comparisonUnit: item.comparisonUnit,
|
||||
offerText: item.offerText,
|
||||
confidence: item.parseConfidence,
|
||||
reasonCodes: item.parseReasons,
|
||||
}));
|
||||
|
||||
const warnings: string[] = [];
|
||||
if (items.length === 0) {
|
||||
warnings.push('Inga produkter kunde extraheras från flyern.');
|
||||
}
|
||||
|
||||
if (response.status >= 400 && response.status < 500) {
|
||||
throw new BadRequestException(message);
|
||||
}
|
||||
throw new ServiceUnavailableException(message);
|
||||
return {
|
||||
retailer: 'willys',
|
||||
parserVersion: 'v1',
|
||||
items,
|
||||
warnings,
|
||||
};
|
||||
} catch (err) {
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err;
|
||||
}
|
||||
if (err instanceof ServiceUnavailableException) {
|
||||
throw err;
|
||||
}
|
||||
this.logger.error(`Internal flyer parse failed: ${String(err)}`);
|
||||
throw new BadRequestException(
|
||||
`Fel vid tolkning av flyer: ${err instanceof Error ? err.message : String(err)}`,
|
||||
);
|
||||
}
|
||||
|
||||
return response.json() as Promise<FlyerParseResponse>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,234 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
Logger,
|
||||
ServiceUnavailableException,
|
||||
} from '@nestjs/common';
|
||||
|
||||
export interface AiFlyerParseResult {
|
||||
rawName: string;
|
||||
normalizedName: string;
|
||||
category: string | null;
|
||||
price: number | null;
|
||||
priceUnit: string | null;
|
||||
comparisonPrice: number | null;
|
||||
comparisonUnit: string | null;
|
||||
offerText: string | null;
|
||||
confidence: number;
|
||||
reasonCodes: string[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AiFlyerParserService {
|
||||
private readonly logger = new Logger(AiFlyerParserService.name);
|
||||
private readonly timeoutMs = 15_000;
|
||||
private mistral: any;
|
||||
private apiKey: string;
|
||||
|
||||
constructor() {
|
||||
this.apiKey = process.env.MISTRAL_API_KEY ?? '';
|
||||
if (!this.apiKey) {
|
||||
throw new Error('MISTRAL_API_KEY environment variable not set');
|
||||
}
|
||||
}
|
||||
|
||||
private async getClient(): Promise<any> {
|
||||
if (this.mistral) return this.mistral;
|
||||
const mistralModule = await import('@mistralai/mistralai');
|
||||
this.mistral = new mistralModule.default(this.apiKey);
|
||||
return this.mistral;
|
||||
}
|
||||
|
||||
/**
|
||||
* Skickar flyer-text till Mistral Tiny för strukturerad extraktion.
|
||||
*
|
||||
* @param text Text från flyern (från pdf-parse eller OCR)
|
||||
* @returns Array av parsade produkter
|
||||
*/
|
||||
async parseWithAI(text: string): Promise<AiFlyerParseResult[]> {
|
||||
if (!text || text.trim().length === 0) {
|
||||
throw new BadRequestException('Flyer-texten är tom. Kan inte fortsätta.');
|
||||
}
|
||||
|
||||
const prompt = this.buildPrompt(text);
|
||||
|
||||
try {
|
||||
this.logger.debug('Sending request to Mistral Tiny');
|
||||
|
||||
const client = await this.getClient();
|
||||
const response = await this.withTimeout<any>(
|
||||
client.chat({
|
||||
model: 'mistral-tiny',
|
||||
messages: [{ role: 'user', content: prompt }],
|
||||
temperature: 0.1,
|
||||
}),
|
||||
this.timeoutMs,
|
||||
'Mistral-anrop timeout',
|
||||
);
|
||||
|
||||
const content = response.choices?.[0]?.message?.content;
|
||||
if (!content) {
|
||||
throw new BadRequestException('Tomt svar från AI-modellen.');
|
||||
}
|
||||
|
||||
this.logger.debug(`Mistral response length: ${content.length} chars`);
|
||||
|
||||
// Rensa och parse JSON
|
||||
const jsonString = this.sanitizeJsonResponse(content);
|
||||
const items = JSON.parse(jsonString) as Array<Record<string, unknown>>;
|
||||
|
||||
if (!Array.isArray(items)) {
|
||||
throw new BadRequestException('AI returnerade inte en JSON-array.');
|
||||
}
|
||||
|
||||
return items.map((item, idx) => this.normalizeAiItem(item, idx));
|
||||
} catch (err) {
|
||||
if (err instanceof SyntaxError) {
|
||||
this.logger.error(`JSON parse error: ${String(err)}`);
|
||||
throw new BadRequestException('AI returnerade ogiltigt JSON. Försök igen.');
|
||||
}
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err;
|
||||
}
|
||||
if (err instanceof ServiceUnavailableException) {
|
||||
throw err;
|
||||
}
|
||||
this.logger.error(`AI parsing failed: ${String(err)}`);
|
||||
throw new ServiceUnavailableException('AI-tjänsten är inte tillgänglig just nu.');
|
||||
}
|
||||
}
|
||||
|
||||
private async withTimeout<T>(
|
||||
promise: Promise<T>,
|
||||
timeoutMs: number,
|
||||
timeoutMessage: string,
|
||||
): Promise<T> {
|
||||
let timeoutHandle: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
const timeoutPromise = new Promise<never>((_, reject) => {
|
||||
timeoutHandle = setTimeout(() => {
|
||||
reject(new ServiceUnavailableException(timeoutMessage));
|
||||
}, timeoutMs);
|
||||
});
|
||||
|
||||
try {
|
||||
return await Promise.race([promise, timeoutPromise]);
|
||||
} finally {
|
||||
if (timeoutHandle) clearTimeout(timeoutHandle);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Bygger systemprompten för Mistral.
|
||||
*/
|
||||
private buildPrompt(text: string): string {
|
||||
// Trunkera långt text för att spara tokens
|
||||
const truncatedText = text.length > 5000 ? text.substring(0, 5000) : text;
|
||||
|
||||
return `Du är en expert på att tolka svenska matvaruflyers (t.ex. från Willys, Coop, ICA).
|
||||
|
||||
Extrahera ALL produktinformation från följande text och returnera den som en JSON-array.
|
||||
|
||||
För varje produkt, inkludera:
|
||||
- name: Produktnamn (fullständigt namn)
|
||||
- weight: Vikt (om tillgänglig, t.ex. "150g", "Ca 1kg") eller null
|
||||
- origin: Ursprung/land/märke (om tillgänglig, t.ex. "FALKENBERG") eller null
|
||||
- price: Pris som nummer (t.ex. 39.90) eller null
|
||||
- comparisonPrice: Jämförpris som nummer (t.ex. 266.00) eller null
|
||||
- unit: Enhet (kg, st, förp, l, etc.) eller null
|
||||
- offer: Erbjudande som array (t.ex. ["Max 3 köp/hushåll"]) eller []
|
||||
- category: Kategori (t.ex. "Fisk", "Kött", "Mejeri", "Grönsaker", "Frukt", "Dryck") eller null
|
||||
- validFrom: Giltig från (datum i formatet YYYY-MM-DD) eller null
|
||||
- validTo: Giltig till (datum i formatet YYYY-MM-DD) eller null
|
||||
|
||||
Texten att tolka:
|
||||
${truncatedText}
|
||||
|
||||
Returnera ENDAST en JSON-array. Inga andra kommentarer, ingen markdown-markup.
|
||||
Exempel på utdata:
|
||||
[
|
||||
{
|
||||
"name": "KALLRÖKT LAX, GRAVAD LAX",
|
||||
"weight": "150g",
|
||||
"origin": "FALKENBERG",
|
||||
"price": 39.90,
|
||||
"comparisonPrice": 266.00,
|
||||
"unit": "kg",
|
||||
"offer": ["Max 3 köp/hushåll"],
|
||||
"category": "Fisk",
|
||||
"validFrom": "2026-05-18",
|
||||
"validTo": "2026-05-24"
|
||||
}
|
||||
]`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rensa AI-svaret för att kunna parse som JSON.
|
||||
*/
|
||||
private sanitizeJsonResponse(content: string): string {
|
||||
// Ta bort markdown fences
|
||||
let cleaned = content.replace(/```json\n?/g, '').replace(/```\n?/g, '');
|
||||
cleaned = cleaned.trim();
|
||||
|
||||
// Försök att extrahera JSON om det finns omgivande text
|
||||
const jsonMatch = cleaned.match(/\[[\s\S]*\]/);
|
||||
if (jsonMatch) {
|
||||
cleaned = jsonMatch[0];
|
||||
}
|
||||
|
||||
return cleaned;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normaliserar och typkonverterar AI-item till vårt format.
|
||||
*/
|
||||
private normalizeAiItem(item: Record<string, unknown>, index: number): AiFlyerParseResult {
|
||||
const toNumber = (val: unknown): number | null => {
|
||||
if (typeof val === 'number') return val;
|
||||
if (typeof val === 'string') {
|
||||
const parsed = parseFloat(val.replace(',', '.'));
|
||||
return isFinite(parsed) ? parsed : null;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const toString = (val: unknown): string | null => {
|
||||
if (typeof val === 'string') return val.trim() || null;
|
||||
return null;
|
||||
};
|
||||
|
||||
const toArray = (val: unknown): string[] => {
|
||||
if (Array.isArray(val)) {
|
||||
return val.map(v => String(v)).filter(v => v.trim());
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
const rawName = toString(item.name) || `Produkt ${index + 1}`;
|
||||
const normalizedName = this.normalizeName(rawName);
|
||||
|
||||
return {
|
||||
rawName,
|
||||
normalizedName,
|
||||
category: toString(item.category),
|
||||
price: toNumber(item.price),
|
||||
priceUnit: toString(item.unit),
|
||||
comparisonPrice: toNumber(item.comparisonPrice),
|
||||
comparisonUnit: toString(item.comparisonUnit),
|
||||
offerText: toString(item.offer) || (toArray(item.offer).join(' ') || null),
|
||||
confidence: 0.85, // AI-parse får medelhög confidence
|
||||
reasonCodes: ['ai_parsed'],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Enkel normalisering av produktnamn.
|
||||
*/
|
||||
private normalizeName(name: string): string {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-zåäö0-9\s]/g, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { FlyerNormalizerService } from './flyer-normalizer.service';
|
||||
|
||||
describe('FlyerNormalizerService', () => {
|
||||
let service: FlyerNormalizerService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [FlyerNormalizerService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<FlyerNormalizerService>(FlyerNormalizerService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe('normalize', () => {
|
||||
it('should normalize a valid item', () => {
|
||||
const items = [
|
||||
{
|
||||
rawName: 'KALLRÖKT LAX, GRAVAD LAX',
|
||||
normalizedName: 'kallrökt lax gravad lax',
|
||||
category: 'Fisk',
|
||||
price: 39.9,
|
||||
comparisonPrice: 266.0,
|
||||
unit: 'kg',
|
||||
offer: ['Max 3 köp/hushåll'],
|
||||
confidence: 0.85,
|
||||
reasonCodes: ['ai_parsed'],
|
||||
},
|
||||
];
|
||||
|
||||
const result = service.normalize(items);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].rawName).toBe('KALLRÖKT LAX, GRAVAD LAX');
|
||||
expect(result[0].price).toBe(39.9);
|
||||
expect(result[0].priceUnit).toBe('kg');
|
||||
expect(result[0].categoryHint).toBe('Fisk');
|
||||
});
|
||||
|
||||
it('should handle missing fields gracefully', () => {
|
||||
const items = [
|
||||
{
|
||||
name: 'PRODUKT',
|
||||
// andra fält saknas
|
||||
},
|
||||
];
|
||||
|
||||
const result = service.normalize(items);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].rawName).toBe('PRODUKT');
|
||||
expect(result[0].price).toBeNull();
|
||||
expect(result[0].categoryHint).toBeNull();
|
||||
});
|
||||
|
||||
it('should skip items without name', () => {
|
||||
const items = [
|
||||
{ price: 100 }, // no name
|
||||
{ rawName: 'VALID PRODUCT', price: 50 },
|
||||
];
|
||||
|
||||
const result = service.normalize(items);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].rawName).toBe('VALID PRODUCT');
|
||||
});
|
||||
|
||||
it('should normalize units correctly', () => {
|
||||
const items = [
|
||||
{ rawName: 'Mjölk', unit: 'L' },
|
||||
{ rawName: 'Smör', unit: 'styck' },
|
||||
{ rawName: 'Socker', unit: 'KG' },
|
||||
];
|
||||
|
||||
const result = service.normalize(items);
|
||||
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[0].priceUnit).toBe('l');
|
||||
expect(result[1].priceUnit).toBe('st');
|
||||
expect(result[2].priceUnit).toBe('kg');
|
||||
});
|
||||
|
||||
it('should parse Swedish prices correctly', () => {
|
||||
const items = [
|
||||
{ rawName: 'Produkt1', price: '39,90' },
|
||||
{ rawName: 'Produkt2', price: 39.9 },
|
||||
{ rawName: 'Produkt3', price: '100' },
|
||||
];
|
||||
|
||||
const result = service.normalize(items);
|
||||
|
||||
expect(result[0].price).toBe(39.9);
|
||||
expect(result[1].price).toBe(39.9);
|
||||
expect(result[2].price).toBe(100);
|
||||
});
|
||||
|
||||
it('should return empty list for non-array input', () => {
|
||||
const result = service.normalize(null as any);
|
||||
expect(result).toEqual([]);
|
||||
|
||||
const result2 = service.normalize(undefined as any);
|
||||
expect(result2).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,158 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
export interface NormalizedFlyerItem {
|
||||
rawName: string;
|
||||
normalizedName: string;
|
||||
categoryHint: string | null;
|
||||
price: number | null;
|
||||
priceUnit: string | null;
|
||||
comparisonPrice: number | null;
|
||||
comparisonUnit: string | null;
|
||||
offerText: string | null;
|
||||
parseConfidence: number;
|
||||
parseReasons: string[];
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class FlyerNormalizerService {
|
||||
private readonly logger = new Logger(FlyerNormalizerService.name);
|
||||
|
||||
private readonly UNIT_MAPPING: Record<string, string> = {
|
||||
// Längd
|
||||
mm: 'mm',
|
||||
cm: 'cm',
|
||||
m: 'm',
|
||||
// Vikt
|
||||
mg: 'mg',
|
||||
g: 'g',
|
||||
hg: 'hg',
|
||||
kg: 'kg',
|
||||
ton: 'ton',
|
||||
// Volym
|
||||
ml: 'ml',
|
||||
cl: 'cl',
|
||||
dl: 'dl',
|
||||
l: 'l',
|
||||
// Övrigt
|
||||
st: 'st',
|
||||
styck: 'st',
|
||||
stycke: 'st',
|
||||
pkt: 'pkt',
|
||||
paket: 'pkt',
|
||||
fp: 'pkt',
|
||||
förp: 'pkt',
|
||||
förpackning: 'pkt',
|
||||
};
|
||||
|
||||
/**
|
||||
* Normaliserar en AI-parsad produktlista.
|
||||
*/
|
||||
normalize(items: any[]): NormalizedFlyerItem[] {
|
||||
if (!Array.isArray(items)) {
|
||||
this.logger.warn('normalize() received non-array, returning empty list');
|
||||
return [];
|
||||
}
|
||||
|
||||
return items
|
||||
.map((item, idx) => this.normalizeItem(item, idx))
|
||||
.filter((item): item is NormalizedFlyerItem => item !== null);
|
||||
}
|
||||
|
||||
private normalizeItem(item: any, index: number): NormalizedFlyerItem | null {
|
||||
if (!item || typeof item !== 'object') {
|
||||
this.logger.warn(`Item ${index} is not an object, skipping`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const rawName = this.extractString(item.rawName) || this.extractString(item.name);
|
||||
if (!rawName) {
|
||||
this.logger.warn(`Item ${index} has no name, skipping`);
|
||||
return null;
|
||||
}
|
||||
|
||||
const normalizedName = this.extractString(item.normalizedName) || this.normalizeName(rawName);
|
||||
|
||||
return {
|
||||
rawName,
|
||||
normalizedName,
|
||||
categoryHint: this.normalizeCategory(this.extractString(item.category)),
|
||||
price: this.extractPrice(item.price),
|
||||
priceUnit: this.normalizeUnit(this.extractString(item.unit)),
|
||||
comparisonPrice: this.extractPrice(item.comparisonPrice),
|
||||
comparisonUnit: this.normalizeUnit(this.extractString(item.comparisonUnit)),
|
||||
offerText: this.normalizeOfferText(item.offer),
|
||||
parseConfidence: item.confidence ?? 0.85,
|
||||
parseReasons: Array.isArray(item.reasonCodes)
|
||||
? item.reasonCodes.map(String)
|
||||
: ['normalized'],
|
||||
};
|
||||
}
|
||||
|
||||
private extractString(val: any): string | null {
|
||||
if (typeof val === 'string') return val.trim() || null;
|
||||
return null;
|
||||
}
|
||||
|
||||
private extractPrice(val: any): number | null {
|
||||
if (typeof val === 'number') return val;
|
||||
if (typeof val === 'string') {
|
||||
const num = parseFloat(val.replace(/,/g, '.'));
|
||||
return isFinite(num) ? num : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private normalizeName(name: string): string {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-zåäö0-9\s]/g, '')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
}
|
||||
|
||||
private normalizeUnit(unit: string | null): string | null {
|
||||
if (!unit) return null;
|
||||
|
||||
const cleaned = unit.trim().toLowerCase().replace(/\./g, '');
|
||||
return this.UNIT_MAPPING[cleaned] ?? null;
|
||||
}
|
||||
|
||||
private normalizeCategory(category: string | null): string | null {
|
||||
if (!category) return null;
|
||||
|
||||
const normalized = category.trim().toLowerCase();
|
||||
|
||||
// Mappning av tänkta kategorivärdena från AI
|
||||
const categoryMap: Record<string, string> = {
|
||||
fisk: 'Fisk',
|
||||
kött: 'Kött',
|
||||
mejeri: 'Mejeri',
|
||||
grönsaker: 'Grönsaker',
|
||||
frukt: 'Frukt',
|
||||
dryck: 'Dryck',
|
||||
frukt_grönsaker: 'Frukt & Grönsaker',
|
||||
fastfood: 'Fastfood',
|
||||
bröd: 'Bröd',
|
||||
fryst: 'Fryst',
|
||||
godis: 'Godis',
|
||||
pasta: 'Pasta',
|
||||
};
|
||||
|
||||
return categoryMap[normalized] ?? null;
|
||||
}
|
||||
|
||||
private normalizeOfferText(offer: any): string | null {
|
||||
if (!offer) return null;
|
||||
|
||||
if (typeof offer === 'string') {
|
||||
return offer.trim() || null;
|
||||
}
|
||||
|
||||
if (Array.isArray(offer)) {
|
||||
const joined = offer.map(String).filter(s => s.trim()).join(' ');
|
||||
return joined || null;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import * as pdf from 'pdf-parse';
|
||||
import Tesseract from 'tesseract.js';
|
||||
|
||||
@Injectable()
|
||||
export class TextExtractorService {
|
||||
private readonly logger = new Logger(TextExtractorService.name);
|
||||
|
||||
/**
|
||||
* Extraherar text från en PDF-buffer.
|
||||
* Försöker med pdf-parse först; om det inte ger resultat, fallback till OCR.
|
||||
*
|
||||
* @param buffer PDF-fil som buffer
|
||||
* @returns Extraherad text
|
||||
*/
|
||||
async extractText(
|
||||
buffer: Buffer,
|
||||
mimeType?: string,
|
||||
originalFilename?: string,
|
||||
): Promise<string> {
|
||||
// Försök primär PDF-extract
|
||||
try {
|
||||
this.logger.debug('Attempting pdf-parse extraction');
|
||||
const pdfData = await pdf(buffer);
|
||||
|
||||
const text = pdfData.text?.trim() || '';
|
||||
const wordCount = text.split(/\s+/).filter(w => w.length > 0).length;
|
||||
|
||||
this.logger.debug(`pdf-parse extracted ${wordCount} words`);
|
||||
|
||||
// Om vi fick tillräckligt med text, returnera det
|
||||
if (wordCount >= 10) {
|
||||
return text;
|
||||
}
|
||||
|
||||
this.logger.debug('pdf-parse gave too little text, falling back to OCR');
|
||||
} catch (err) {
|
||||
this.logger.warn(`pdf-parse failed: ${String(err)}`);
|
||||
}
|
||||
|
||||
// Fallback: OCR med Tesseract
|
||||
return this.extractTextViaOCR(buffer, mimeType, originalFilename);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extraherar text från en PDF eller bild via OCR (Tesseract).
|
||||
*
|
||||
* @param buffer Fil-buffer (PDF eller bild)
|
||||
* @returns Extraherad text
|
||||
*/
|
||||
private async extractTextViaOCR(
|
||||
buffer: Buffer,
|
||||
mimeType?: string,
|
||||
originalFilename?: string,
|
||||
): Promise<string> {
|
||||
try {
|
||||
this.logger.debug('Starting Tesseract OCR extraction');
|
||||
|
||||
// Tesseract.js kräver en sökväg eller data-URL; vi skriver temporär fil
|
||||
const ext = this.resolveTempExtension(mimeType, originalFilename);
|
||||
const tempPath = path.join(os.tmpdir(), `ocr-${Date.now()}${ext}`);
|
||||
await fs.promises.writeFile(tempPath, buffer);
|
||||
|
||||
try {
|
||||
const result = await Tesseract.recognize(tempPath, 'swe', {
|
||||
logger: (m) => this.logger.debug(`Tesseract: ${m.status}`),
|
||||
});
|
||||
|
||||
const text = result.data.text || '';
|
||||
this.logger.debug(`Tesseract extracted ${text.split(/\s+/).length} words`);
|
||||
return text;
|
||||
} finally {
|
||||
try {
|
||||
await fs.promises.unlink(tempPath);
|
||||
} catch {
|
||||
// ignorera om cleanup misslyckas
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error(`OCR extraction failed: ${String(err)}`);
|
||||
throw new Error('Kunde inte extrahera text från flyern (pdf-parse + OCR misslyckades).');
|
||||
}
|
||||
}
|
||||
|
||||
private resolveTempExtension(mimeType?: string, originalFilename?: string): string {
|
||||
if (mimeType === 'image/png') return '.png';
|
||||
if (mimeType === 'image/webp') return '.webp';
|
||||
if (mimeType === 'image/jpeg') return '.jpg';
|
||||
if (mimeType === 'text/plain') return '.txt';
|
||||
if (mimeType === 'application/pdf') return '.pdf';
|
||||
|
||||
const originalExt = originalFilename ? path.extname(originalFilename).toLowerCase() : '';
|
||||
if (originalExt) return originalExt;
|
||||
|
||||
return '.pdf';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user