ContentDifferential.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. this.nouns.forEach((noun) => {
  72. highest += ContentDifferential.getNounLevel(noun);
  73. });
  74. return highest;
  75. }
  76. public static getNounLevel (noun : Thing | typeof Thing | ContentDifferential | ContentMarker) {
  77. if (noun == undefined || noun == null) {
  78. return 0;
  79. } else if (noun instanceof AdaptiveDifferential) {
  80. return noun.getScore();
  81. } else if (typeof noun == "function") {
  82. if (<any>noun.prototype instanceof Thing) {
  83. let specifity = 2; // Vague Thing
  84. let parentClass = Object.getPrototypeOf(noun);
  85. while (parentClass != Thing) {
  86. specifity += 0.1;
  87. parentClass = Object.getPrototypeOf(parentClass);
  88. }
  89. return specifity;
  90. } else {
  91. return 2.5; // It's not a "Thing", so it's probably a weird class, which is kind of specific
  92. }
  93. } else if (noun instanceof Thing) {
  94. return 4; // Specific thing
  95. } else if (noun instanceof ContentDifferential) {
  96. return 1; // Minor thing
  97. } else {
  98. return 0.5;
  99. }
  100. }
  101. public static compareNouns (a : Thing | typeof Thing | ContentDifferential | ContentMarker, b : Thing | typeof Thing | ContentDifferential | ContentMarker) {
  102. if (a == undefined || a == null) {
  103. return true;
  104. }
  105. if (a instanceof AdaptiveDifferential) {
  106. //console.log(a, b, a.isMatch(b));
  107. return a.isMatch(b);
  108. } else if (typeof a == "function") {
  109. // b must inherit a or be a
  110. return b == a || b instanceof a || (typeof b == "function" && (<any>b).prototype instanceof a)
  111. } else if (a instanceof Thing) {
  112. // b must be a
  113. return b == a;
  114. }
  115. return a === b;
  116. }
  117. public static isMatch (matchFrom : Array<ContentDifferential>, matchAgainst : Array<ContentDifferential>) {
  118. let unmatched = matchAgainst.slice();
  119. let matching = matchFrom.slice();
  120. for (let i = matching.length - 1; i >= 0; i--) {
  121. for (let k = unmatched.length - 1; k >= 0; k--) {
  122. if (matching[i].isMatch(unmatched[k])) {
  123. unmatched.splice(k, 1);
  124. matching.splice(i, 1);
  125. break;
  126. }
  127. }
  128. }
  129. if (unmatched.length == 0 && matching.length == 0) {
  130. return true;
  131. } else if (unmatched.length == 0) {
  132. // Check if the only thing missing are unimportant ContentMarkers
  133. for (let i = 0; i < matching.length; i++) {
  134. let nouns = matching[i].getNouns();
  135. for (let k = 0; k < nouns.length; k++) {
  136. if (!(nouns[k] instanceof ContentMarker)) {
  137. return false;
  138. } else {
  139. if ((<ContentMarker> nouns[k]).isImportant()) {
  140. return false;
  141. }
  142. }
  143. }
  144. }
  145. // Nothing important found
  146. return true;
  147. }
  148. return false;
  149. }
  150. }