ContentDifferential.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. interface ContentDifferentialComparisonResult {
  2. matching : Array<Thing | typeof Thing | ContentDifferential | ContentMarker>;
  3. unmatched : Array<Thing | typeof Thing | ContentDifferential | ContentMarker>;
  4. }
  5. class ContentDifferential {
  6. public nouns : Array<Thing | typeof Thing | ContentDifferential | ContentMarker> = [];
  7. public score : number = 0;
  8. public constructor (...nouns : Array<any>) {
  9. this.addNoun(...nouns);
  10. }
  11. public addNoun (...nouns : Array<any>) {
  12. nouns.forEach(noun => {
  13. if (noun != undefined) {
  14. this.nouns.push(noun);
  15. }
  16. });
  17. this.score = this.getScore();
  18. return this;
  19. }
  20. public getNouns () {
  21. return this.nouns;
  22. }
  23. public replaceNouns (...nouns : Array<any>) {
  24. this.nouns = nouns;
  25. return this;
  26. }
  27. public isMatch (cd : ContentDifferential, allowPartial = false) {
  28. let check = this.getUnmatched(cd);
  29. if ((allowPartial || (check.unmatched.length) <= 0) && check.matching.length == 0) {
  30. return true;
  31. } else if ((allowPartial || (check.unmatched.length) <= 0) && check.matching.length > 0) {
  32. for (let i = 0; i < check.matching.length; i++) {
  33. if (!(check.matching[i] instanceof AdaptiveDifferential && (<AdaptiveDifferential> <unknown> check.matching[i]).countsAs == -1)) {
  34. return false;
  35. }
  36. return true;
  37. }
  38. } else if (check.matching.length == 0) {
  39. for (let i = 0; i < check.unmatched.length; i++) {
  40. if (!(check.unmatched[i] instanceof ContentMarker)) {
  41. return false;
  42. } else {
  43. if ((<ContentMarker> check.unmatched[i]).isImportant()) {
  44. return false;
  45. }
  46. }
  47. }
  48. return true;
  49. }
  50. return false;
  51. }
  52. public getUnmatched (cd : ContentDifferential) : ContentDifferentialComparisonResult {
  53. let unmatched = cd.nouns.slice();
  54. let matching = this.nouns.slice();
  55. for (let i = matching.length - 1; i >= 0; i--) {
  56. for (let k = unmatched.length - 1; k >= 0; k--) {
  57. if (ContentDifferential.compareNouns(matching[i], unmatched[k])) {
  58. unmatched.splice(k, 1);
  59. matching.splice(i, 1);
  60. break;
  61. }
  62. }
  63. }
  64. return {
  65. matching : matching,
  66. unmatched : unmatched
  67. };
  68. }
  69. public getScore () {
  70. let highest = 0;
  71. let count = this.nouns.length;
  72. this.nouns.forEach((noun) => {
  73. let level = ContentDifferential.getNounLevel(noun);
  74. if (highest < level) {
  75. highest = level;
  76. }
  77. });
  78. return highest + (count / 100);
  79. }
  80. public static getNounLevel (noun : Thing | typeof Thing | ContentDifferential | ContentMarker) {
  81. if (noun == undefined || noun == null) {
  82. return 0;
  83. } else if (typeof noun == "function") {
  84. if (<any>noun.prototype instanceof Thing) {
  85. let specifity = 2; // Vague Thing
  86. let parentClass = Object.getPrototypeOf(noun);
  87. while (parentClass != Thing) {
  88. specifity += 0.1;
  89. parentClass = Object.getPrototypeOf(parentClass);
  90. }
  91. return specifity;
  92. } else {
  93. return 2.5; // It's not a "Thing", so it's probably a weird class, which is kind of specific
  94. }
  95. } else if (noun instanceof Thing) {
  96. return 4; // Specific thing
  97. } else if (noun instanceof ContentDifferential) {
  98. return 1; // Minor thing
  99. } else {
  100. return 0.5;
  101. }
  102. }
  103. public static compareNouns (a : Thing | typeof Thing | ContentDifferential | ContentMarker, b : Thing | typeof Thing | ContentDifferential | ContentMarker) {
  104. if (a == undefined || a == null) {
  105. return true;
  106. }
  107. if (a instanceof AdaptiveDifferential) {
  108. //console.log(a, b, a.isMatch(b));
  109. return a.isMatch(b);
  110. } else if (typeof a == "function") {
  111. // b must inherit a or be a
  112. return b == a || b instanceof a || (typeof b == "function" && (<any>b).prototype instanceof a)
  113. } else if (a instanceof Thing) {
  114. // b must be a
  115. return b == a;
  116. }
  117. return a === b;
  118. }
  119. public static isMatch (matchFrom : Array<ContentDifferential>, matchAgainst : Array<ContentDifferential>) {
  120. let unmatched = matchAgainst.slice();
  121. let matching = matchFrom.slice();
  122. for (let i = matching.length - 1; i >= 0; i--) {
  123. for (let k = unmatched.length - 1; k >= 0; k--) {
  124. if (matching[i].isMatch(unmatched[k])) {
  125. unmatched.splice(k, 1);
  126. matching.splice(i, 1);
  127. break;
  128. }
  129. }
  130. }
  131. if (unmatched.length == 0 && matching.length == 0) {
  132. return true;
  133. } else if (unmatched.length == 0) {
  134. // Check if the only thing missing are unimportant ContentMarkers
  135. for (let i = 0; i < matching.length; i++) {
  136. let nouns = matching[i].getNouns();
  137. for (let k = 0; k < nouns.length; k++) {
  138. if (!(nouns[k] instanceof ContentMarker)) {
  139. return false;
  140. } else {
  141. if ((<ContentMarker> nouns[k]).isImportant()) {
  142. return false;
  143. }
  144. }
  145. }
  146. }
  147. // Nothing important found
  148. return true;
  149. }
  150. return false;
  151. }
  152. }