Spaces:
Sleeping
Sleeping
File size: 2,140 Bytes
097eaf0 | 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 | interface ComparisonResult {
overall: {
rustLen: number;
muLen: number;
lenRatio: number;
rustNumberCount: number;
muNumberCount: number;
numberPreservationRatio: number;
rustTableCount: number;
muTableCount: number;
overallMatch: "good" | "acceptable" | "poor";
};
}
/** Extract all numbers (integers and decimals) from text as a Set of strings. */
export function extractNumbers(text: string): Set<string> {
const matches = text.match(/\d+(?:\.\d+)?/g);
return new Set(matches ?? []);
}
/** Count markdown tables by matching full header-separator rows. */
export function countTables(md: string): number {
const matches = md.match(/^\s*(?:\|\s*:?-+:?\s*)+\|\s*$/gm);
return matches?.length ?? 0;
}
export function comparePdfOutputs(
rustMd: string,
muMd: string,
): ComparisonResult {
const rustLen = rustMd.length;
const muLen = muMd.length;
const lenRatio = muLen > 0 ? rustLen / muLen : rustLen > 0 ? 0 : 1;
const rustNumbers = extractNumbers(rustMd);
const muNumbers = extractNumbers(muMd);
const rustNumberCount = rustNumbers.size;
const muNumberCount = muNumbers.size;
let numberPreservationRatio: number;
if (muNumberCount === 0) {
numberPreservationRatio = rustNumberCount === 0 ? 1 : 0;
} else {
let preserved = 0;
for (const n of muNumbers) {
if (rustNumbers.has(n)) preserved++;
}
numberPreservationRatio = preserved / muNumberCount;
}
const rustTableCount = countTables(rustMd);
const muTableCount = countTables(muMd);
let overallMatch: "good" | "acceptable" | "poor";
if (lenRatio >= 0.8 && numberPreservationRatio >= 0.9) {
overallMatch = "good";
} else if (lenRatio >= 0.5 && numberPreservationRatio >= 0.7) {
overallMatch = "acceptable";
} else {
overallMatch = "poor";
}
return {
overall: {
rustLen,
muLen,
lenRatio: Math.round(lenRatio * 1000) / 1000,
rustNumberCount,
muNumberCount,
numberPreservationRatio:
Math.round(numberPreservationRatio * 1000) / 1000,
rustTableCount,
muTableCount,
overallMatch,
},
};
}
|