|
| 1 | +import { HashFilter } from "../hashing/hashFilter.js"; |
| 2 | +import { Range } from "../util/range.js"; |
| 3 | +import { Region } from "../util/region.js"; |
| 4 | +import { WinnowFilter } from "../hashing/winnowFilter.js"; |
| 5 | +import { TokenizedFile } from "..//file/tokenizedFile.js"; |
| 6 | +import { SharedFingerprint } from "./sharedFingerprint.js"; |
| 7 | +import { ASTRegion } from "./pairedOccurrence.js"; |
| 8 | +import { Pair } from "./pair.js"; |
| 9 | +import { assert, assertDefined, closestMatch } from "../util/utils.js"; |
| 10 | + |
| 11 | +export type Hash = number; |
| 12 | + |
| 13 | +export interface FileEntry { |
| 14 | + file: TokenizedFile; |
| 15 | + kgrams: Array<Range>, |
| 16 | + shared: Set<SharedFingerprint>; |
| 17 | +} |
| 18 | + |
| 19 | +export interface Occurrence { |
| 20 | + file: TokenizedFile; |
| 21 | + side: ASTRegion; |
| 22 | +} |
| 23 | + |
| 24 | +export class FingerprintIndex { |
| 25 | + private readonly hashFilter: HashFilter; |
| 26 | + private readonly files: Map<number, FileEntry>; |
| 27 | + private readonly index: Map<Hash, SharedFingerprint>; |
| 28 | + |
| 29 | + /** |
| 30 | + * Creates a Fingerprint Index which is able to compare files with each other |
| 31 | + * based on their winnowed fingerprints (kgrams of tokens). |
| 32 | + * |
| 33 | + */ |
| 34 | + constructor( |
| 35 | + private readonly kgramLength: number, |
| 36 | + private readonly kgramsInWindow: number, |
| 37 | + kgramData?: boolean |
| 38 | + ) { |
| 39 | + this.hashFilter = new WinnowFilter(this.kgramLength, this.kgramsInWindow, kgramData); |
| 40 | + this.files = new Map<number, FileEntry>(); |
| 41 | + this.index = new Map<Hash, SharedFingerprint>(); |
| 42 | + } |
| 43 | + |
| 44 | + public async addFiles(tokenizedFiles: TokenizedFile[]): Promise<Map<Hash, SharedFingerprint>> { |
| 45 | + |
| 46 | + for (const f of tokenizedFiles) { |
| 47 | + assert(!this.files.has(f.id), `This file has already been analyzed: ${f.file.path}`); |
| 48 | + } |
| 49 | + |
| 50 | + for (const file of tokenizedFiles) { |
| 51 | + let kgram = 0; |
| 52 | + |
| 53 | + const entry: FileEntry = { |
| 54 | + file, |
| 55 | + kgrams: [], |
| 56 | + shared: new Set<SharedFingerprint>() |
| 57 | + }; |
| 58 | + |
| 59 | + this.files.set(file.id, entry); |
| 60 | + |
| 61 | + for await ( |
| 62 | + const { data, hash, start, stop } |
| 63 | + of this.hashFilter.fingerprints(file.tokens) |
| 64 | + ) { |
| 65 | + |
| 66 | + // add kgram to file |
| 67 | + entry.kgrams.push(new Range(start, stop)); |
| 68 | + |
| 69 | + // sanity check |
| 70 | + assert( |
| 71 | + Region.isInOrder( |
| 72 | + file.mapping[start], |
| 73 | + file.mapping[stop] |
| 74 | + ) |
| 75 | + // If we end our kgram on a ')', the location of the opening token is used. |
| 76 | + // However, the location of this token in the file might be before |
| 77 | + // the location of the starting token of the kmer |
| 78 | + // For example: the last token of every ast is ')', closing the program. |
| 79 | + // The location of this token is always (0, 0), since the program root is the first token. |
| 80 | + // In this way, the 'end' token is before any other token in the AST. |
| 81 | + || file.tokens[stop] === ")" , |
| 82 | + `Invalid ordering: |
| 83 | + expected ${file.mapping[start]} |
| 84 | + to start be before the end of ${file.mapping[stop]}` |
| 85 | + ); |
| 86 | + |
| 87 | + const location = Region.merge( |
| 88 | + file.mapping[start], |
| 89 | + file.mapping[stop] |
| 90 | + ); |
| 91 | + |
| 92 | + const part: Occurrence = { |
| 93 | + file, |
| 94 | + side: { index: kgram, start, stop, data, location } |
| 95 | + }; |
| 96 | + |
| 97 | + // look if the index already contains the given hashing |
| 98 | + let shared: SharedFingerprint | undefined = this.index.get(hash); |
| 99 | + |
| 100 | + if (!shared) { |
| 101 | + // if the hashing does not yet exist in the index, add it |
| 102 | + shared = new SharedFingerprint(hash, data); |
| 103 | + this.index.set(hash, shared); |
| 104 | + } |
| 105 | + |
| 106 | + shared.add(part); |
| 107 | + entry.shared.add(shared); |
| 108 | + |
| 109 | + kgram += 1; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return this.index; |
| 114 | + } |
| 115 | + |
| 116 | + public sharedFingerprints(): Array<SharedFingerprint> { |
| 117 | + return Array.from(this.index.values()); |
| 118 | + } |
| 119 | + |
| 120 | + public getPair(file1: TokenizedFile, file2: TokenizedFile): Pair { |
| 121 | + const entry1 = this.files.get(file1.id); |
| 122 | + const entry2 = this.files.get(file2.id); |
| 123 | + assertDefined(entry1, `File ${file1.path} not found in index`); |
| 124 | + assertDefined(entry2, `File ${file2.path} not found in index`); |
| 125 | + return new Pair(entry1, entry2); |
| 126 | + } |
| 127 | + |
| 128 | + public allPairs(sortBy?: string): Array<Pair> { |
| 129 | + const pairs = []; |
| 130 | + const entries = Array.from(this.files.values()); |
| 131 | + for (let i = 0; i < entries.length; i++) { |
| 132 | + for (let j = i + 1; j < entries.length; j++) { |
| 133 | + pairs.push(new Pair(entries[i], entries[j])); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (sortBy) { |
| 138 | + type SortFn = (a: Pair, b: Pair) => number; |
| 139 | + const sortfn = closestMatch<SortFn>(sortBy || "similarity", { |
| 140 | + "total overlap": (a, b) => b.overlap - a.overlap, |
| 141 | + "longest fragment": (a, b) => b.longest - a.longest, |
| 142 | + similarity: (a, b) => b.similarity - a.similarity, |
| 143 | + }); |
| 144 | + |
| 145 | + assertDefined(sortfn, `${sortBy} is not a valid field to sort on`); |
| 146 | + |
| 147 | + pairs.sort(sortfn); |
| 148 | + } |
| 149 | + return pairs; |
| 150 | + } |
| 151 | +} |
0 commit comments