123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- enum ContentGroupMatch {
- NO_MATCH,
- PARTIAL_MATCH,
- PERFECT_MATCH
- }
- interface ContentGroupMatchResult {
- type : ContentGroupMatch;
- unmatched : Array<ContentUnit>;
- }
- class ContentGroup {
- private units : Array<ContentUnit> = [];
- private matching : Array<ContentUnit>;
- constructor (...units : Array<ContentUnit>) {
- units.forEach(unit => {
- this.addUnit(unit);
- })
- }
- public addUnit (unit : ContentUnit) {
- this.units.push(unit);
- return this;
- }
- public addDifferentials (...differentials : Array<any>) {
- let unit = new ContentUnit();
- unit.addCategory(...differentials);
- this.addUnit(unit);
- return this;
- }
- public reset () {
- this.matching = this.units.slice();
- }
-
- public isMatching () {
- return this.matching.length;
- }
- public setMatching (matching : Array<ContentUnit>) {
- this.matching = matching;
- }
- public isMatch (cg : ContentGroup) : ContentGroupMatchResult {
- let unmatched = cg.matching.slice();
- let matching = this.units.slice();
- for (let i = matching.length - 1; i >= 0; i--) {
- for (let k = unmatched.length - 1; k >= 0; k--) {
- if (matching[i].isMatch(unmatched[k])) {
- unmatched.splice(k, 1);
- matching.splice(i, 1);
- break;
- }
- }
- }
- return {
- type : matching.length > 0 ? ContentGroupMatch.NO_MATCH :
- unmatched.length == 0 ? ContentGroupMatch.PERFECT_MATCH :
- ContentGroupMatch.PARTIAL_MATCH,
- unmatched : unmatched
- }
- }
- public getScore () {
- let score = 0;
- this.units.forEach(unit => {
- score += unit.getScore();
- });
- return score;
- }
- public matchAgainst (a : Array<ContentGroup>) : Array<number> {
- let matches = [];
- this.reset();
- for (let i = 0; i < a.length; i++) {
- let match = a[i].isMatch(this);
- if (match.type != ContentGroupMatch.NO_MATCH) {
- matches.push(i);
- this.setMatching(match.unmatched);
- }
- if (!this.isMatching()) {
- return matches;
- }
- }
- return undefined;
- }
- public getLength () {
- return this.units.length;
- }
- public getUnit (n : number) {
- return this.units[n];
- }
- }
|