ContentNoun.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Mandatory = One of the nouns MUST be present.
  3. * Optional = If on specific side, can be ignored. Behaves as mandatory on Vague side.
  4. * Fully_Adaptive = Behaves as optional on either side.
  5. */
  6. enum ContentNounType {MANDATORY, OPTIONAL, FULLY_ADAPTIVE};
  7. /**
  8. * Holds an array of valid nouns. This is meant to allow smarter responses that work off multiple Nouns without requiring too much code.
  9. * NOTE: This means only ONE of the nouns needs to fit!
  10. */
  11. class ContentNoun {
  12. private nouns : Array<any> = [];
  13. private type = ContentNounType.MANDATORY;
  14. public constructor (...nouns : Array<any>) {
  15. this.addNoun(...nouns);
  16. }
  17. public addNoun (...nouns : Array<any>) {
  18. this.nouns.push(...nouns);
  19. return this;
  20. }
  21. public getNouns () {
  22. return [...this.nouns];
  23. }
  24. public setType (type : ContentNounType) {
  25. this.type = type;
  26. return this;
  27. }
  28. public getType () {
  29. return this.type;
  30. }
  31. public compareAgainst (anything : any) {
  32. if (anything instanceof ContentNoun) {
  33. // test each of my nouns against each of other nouns
  34. for (let i = 0; i < this.nouns.length; i++) {
  35. let specificNoun = this.nouns[i];
  36. for (let k = 0; k < anything.nouns.length; k++) {
  37. let vagueNoun = anything.nouns[k];
  38. if (ContentAtom.compareNoun(specificNoun, vagueNoun)) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. } else {
  45. // test each noun against anything
  46. for (let i = 0; i < this.nouns.length; i++) {
  47. if (ContentAtom.compareNoun(this.nouns[i], anything)) {
  48. // A match was found.
  49. return true;
  50. }
  51. }
  52. return false;
  53. }
  54. }
  55. }