1
1

ContentDifferential.ts 5.2 KB

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