Contents.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Fire.Serialize.Annotations;
  5. namespace Leah.Speech.text {
  6. [Flags]
  7. public enum eMood : Int32 {
  8. None = 0,
  9. angry = 1,
  10. aroused = 2,
  11. hungry = 4,
  12. happyness = 8,
  13. funny = 16,
  14. annoyed = 32,
  15. irritated = 64,
  16. bored = 128,
  17. intelligent = 256,
  18. sleepy = 512,
  19. energy = 1024,
  20. exhausted = 2048,
  21. depressive = 4096,
  22. sad = 8192,
  23. useless = 16384,
  24. usefull = 32768,
  25. compfortable = 65536,
  26. temperate = 131072,
  27. shy = 262144,
  28. excited = 524288,
  29. toLoud = 1048576,
  30. toQuiet = 2097152,
  31. playful = 4194304,
  32. naiv = 8388608,
  33. freezy = 16777216,
  34. hot = 33554432,
  35. humanity = 67108864,
  36. lonely = 134217728,
  37. soziality = 268435456,
  38. // Groups
  39. All = 2147483647
  40. }
  41. public class Contents {
  42. public List<ContentEntry> Entries = new List<ContentEntry>();
  43. private Random rnd = new Random();
  44. private List<string> _tags = new List<string>();
  45. private List<string> _optionalTags = new List<string>();
  46. /*public List<ContentEntry> Entries {
  47. get {
  48. return this._entries;
  49. }
  50. set {
  51. if (value != null)
  52. this._entries = value;
  53. }
  54. }*/
  55. public int Count {
  56. get => this.Entries.Count;
  57. }
  58. public ContentEntry GetEntry(int index) {
  59. if (index >= this.Entries.Count)
  60. return null;
  61. return this.Entries[index];
  62. }
  63. public void RemoveAt(int id) {
  64. this.Entries.RemoveAt(id);
  65. }
  66. public string getText(eMood CategoryId) {
  67. List<ContentEntry> ress = new List<ContentEntry>();
  68. if (CategoryId == eMood.None) {
  69. return this.Entries[rnd.Next(0, this.Entries.Count - 1)].Text;
  70. } else {
  71. foreach (ContentEntry ce in this.Entries)
  72. if ((ce.Mood & CategoryId) > 0)
  73. ress.Add(ce);
  74. if (ress.Count == 0) {
  75. return ress[rnd.Next(0, ress.Count - 1)].Text;
  76. } else {
  77. return "";
  78. }
  79. }
  80. }
  81. public string getText(eMood CategoryId, ushort Tag) {
  82. List<ContentEntry> ress = new List<ContentEntry>();
  83. if (CategoryId == eMood.None) {
  84. return this.Entries[rnd.Next(0, this.Entries.Count - 1)].Text;
  85. } else {
  86. foreach (ContentEntry ce in this.Entries)
  87. if ((ce.Mood & CategoryId) > 0 && ce.Tag_Has(Tag))
  88. ress.Add(ce);
  89. if (ress.Count == 0) {
  90. return ress[rnd.Next(0, ress.Count - 1)].Text;
  91. } else {
  92. return "";
  93. }
  94. }
  95. }
  96. public string getText(eMood CategoryId, ushort[] Tag) {
  97. List<ContentEntry> ress = new List<ContentEntry>();
  98. if (CategoryId == eMood.None) {
  99. return this.Entries[rnd.Next(0, this.Entries.Count - 1)].Text;
  100. } else {
  101. foreach (ContentEntry ce in this.Entries)
  102. if ((ce.Mood & CategoryId) > 0 && ce.Tag_Has(Tag))
  103. ress.Add(ce);
  104. if (ress.Count == 0) {
  105. return ress[rnd.Next(0, ress.Count - 1)].Text;
  106. } else {
  107. return "";
  108. }
  109. }
  110. }
  111. public string getText(eMood CategoryId, string Tags) {
  112. if (Tags == "")
  113. return this.getText(CategoryId);
  114. List<ushort> tl = new List<ushort>();
  115. string[] ts = Tags.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
  116. foreach (string t in ts) {
  117. string t2 = t.ToLower().Trim();
  118. if (t2 != "" && this._tags.Contains(t2))
  119. tl.Add((ushort)this._tags.IndexOf(t2));
  120. }
  121. if (tl.Count > 0)
  122. return this.getText(CategoryId, tl.ToArray());
  123. return this.getText(CategoryId);
  124. }
  125. public string getText(string Category, string Tags) {
  126. if (Tags == "")
  127. return this.getText(Category);
  128. List<ushort> tl = new List<ushort>();
  129. string[] ts = Tags.Split(new char[]{ '|'}, StringSplitOptions.RemoveEmptyEntries);
  130. foreach (string t in ts) {
  131. string t2 = t.ToLower().Trim();
  132. if (t2 != "" && this._tags.Contains(t2))
  133. tl.Add((ushort)this._tags.IndexOf(t2));
  134. }
  135. if (tl.Count > 0)
  136. return this.getText(Category, tl.ToArray());
  137. return this.getText(Category);
  138. }
  139. public string getText(string Category) {
  140. return this.getText(GetMood(Category));
  141. }
  142. public string getText(string Category, ushort tag) {
  143. return this.getText(GetMood(Category), tag);
  144. }
  145. public string getText(string Category, ushort[] tags) {
  146. return this.getText(GetMood(Category), tags);
  147. }
  148. public static string GetMoodString(eMood mood) {
  149. return GetMoodString(mood,",");
  150. }
  151. public static string GetMoodString(eMood mood, string Seperator) {
  152. string res = "";
  153. if (mood == eMood.None) return "none";
  154. if ((mood | eMood.All) == mood) return "all";
  155. if ((mood | eMood.angry) == mood) res += (res==""?"":Seperator) + "angry";
  156. if ((mood | eMood.annoyed) == mood) res += (res == "" ? "" : Seperator) + "annoyed";
  157. if ((mood | eMood.aroused) == mood) res += (res == "" ? "" : Seperator) + "aroused";
  158. if ((mood | eMood.bored) == mood) res += (res == "" ? "" : Seperator) + "bored";
  159. if ((mood | eMood.compfortable) == mood) res += (res == "" ? "" : Seperator) + "compfortable";
  160. if ((mood | eMood.depressive) == mood) res += (res == "" ? "" : Seperator) + "depressive";
  161. if ((mood | eMood.energy) == mood) res += (res == "" ? "" : Seperator) + "energy";
  162. if ((mood | eMood.excited) == mood) res += (res == "" ? "" : Seperator) + "excited";
  163. if ((mood | eMood.exhausted) == mood) res += (res == "" ? "" : Seperator) + "exhausted";
  164. if ((mood | eMood.freezy) == mood) res += (res == "" ? "" : Seperator) + "freezy";
  165. if ((mood | eMood.funny) == mood) res += (res == "" ? "" : Seperator) + "funny";
  166. if ((mood | eMood.happyness) == mood) res += (res == "" ? "" : Seperator) + "happyness";
  167. if ((mood | eMood.hot) == mood) res += (res == "" ? "" : Seperator) + "hot";
  168. if ((mood | eMood.humanity) == mood) res += (res == "" ? "" : Seperator) + "humanity";
  169. if ((mood | eMood.hungry) == mood) res += (res == "" ? "" : Seperator) + "hungry";
  170. if ((mood | eMood.intelligent) == mood) res += (res == "" ? "" : Seperator) + "intelligent";
  171. if ((mood | eMood.irritated) == mood) res += (res == "" ? "" : Seperator) + "irritated";
  172. if ((mood | eMood.lonely) == mood) res += (res == "" ? "" : Seperator) + "lonely";
  173. if ((mood | eMood.naiv) == mood) res += (res == "" ? "" : Seperator) + "naiv";
  174. if ((mood | eMood.playful) == mood) res += (res == "" ? "" : Seperator) + "playful";
  175. if ((mood | eMood.sad) == mood) res += (res == "" ? "" : Seperator) + "sad";
  176. if ((mood | eMood.shy) == mood) res += (res == "" ? "" : Seperator) + "shy";
  177. if ((mood | eMood.sleepy) == mood) res += (res == "" ? "" : Seperator) + "sleepy";
  178. if ((mood | eMood.soziality) == mood) res += (res == "" ? "" : Seperator) + "soziality";
  179. if ((mood | eMood.temperate) == mood) res += (res == "" ? "" : Seperator) + "temperate";
  180. if ((mood | eMood.toLoud) == mood) res += (res == "" ? "" : Seperator) + "toLoud";
  181. if ((mood | eMood.toQuiet) == mood) res += (res == "" ? "" : Seperator) + "toQuiet";
  182. if ((mood | eMood.usefull) == mood) res += (res == "" ? "" : Seperator) + "usefull";
  183. if ((mood | eMood.useless) == mood) res += (res == "" ? "" : Seperator) + "useless";
  184. return res;
  185. }
  186. public static eMood GetMood(string Category) {
  187. return GetMood(new char[] { '|',',',';','/' }, Category);
  188. //return GetMood(new char[] {',' }, Category);
  189. }
  190. public static eMood GetMood(char Seperator, string Category) {
  191. return GetMood(new char[] { Seperator }, Category);
  192. }
  193. public static eMood GetMood(char[] Seperator, string Category) {
  194. Category = Category.ToLower();
  195. if (Category == "" || Category == "none" || Category == "null" || Category=="-")
  196. return eMood.None;
  197. if (Category == "all" || Category == "any" || Category == "+")
  198. return eMood.All;
  199. eMood m = eMood.None;
  200. string[] x = Category.Split(Seperator);
  201. foreach (string y in x) {
  202. switch (y.Trim()) {
  203. case "a":
  204. case "angry":
  205. case "mad":
  206. m |= eMood.angry;
  207. break;
  208. case "annoyed":
  209. m |= eMood.annoyed;
  210. break;
  211. case "aroused":
  212. m |= eMood.aroused;
  213. break;
  214. case "bored":
  215. m |= eMood.bored;
  216. break;
  217. case "compfort":
  218. case "compfortable":
  219. m |= eMood.compfortable;
  220. break;
  221. case "depress":
  222. case "dep":
  223. case "depressive":
  224. m |= eMood.depressive;
  225. break;
  226. case "e":
  227. case "energy":
  228. case "energie":
  229. m |= eMood.energy;
  230. break;
  231. case "excited":
  232. m |= eMood.excited;
  233. break;
  234. case "weak":
  235. case "exhausted":
  236. m |= eMood.exhausted;
  237. break;
  238. case "freez":
  239. case "cold":
  240. case "freezy":
  241. m |= eMood.freezy;
  242. break;
  243. case "fun":
  244. case "funny":
  245. m |= eMood.funny;
  246. break;
  247. case "happy":
  248. case "happyness":
  249. m |= eMood.happyness;
  250. break;
  251. case "hot":
  252. m |= eMood.hot;
  253. break;
  254. case "humanity":
  255. case "human":
  256. m |= eMood.humanity;
  257. break;
  258. case "hungry":
  259. case "food":
  260. m |= eMood.hungry;
  261. break;
  262. case "int":
  263. case "intelligent":
  264. case "i":
  265. m |= eMood.intelligent;
  266. break;
  267. case "irritated":
  268. case "irr":
  269. m |= eMood.irritated;
  270. break;
  271. case "lonely":
  272. case "single":
  273. case "alone":
  274. case "lone":
  275. m |= eMood.lonely;
  276. break;
  277. case "n":
  278. case "naiv":
  279. m |= eMood.naiv;
  280. break;
  281. case "play":
  282. case "playful":
  283. m |= eMood.playful;
  284. break;
  285. case "sad":
  286. m |= eMood.sad;
  287. break;
  288. case "shy":
  289. m |= eMood.shy;
  290. break;
  291. case "sleepy":
  292. case "sleep":
  293. case "sl":
  294. m |= eMood.sleepy;
  295. break;
  296. case "soziality":
  297. case "sozial":
  298. m |= eMood.soziality;
  299. break;
  300. case "temperate":
  301. case "temp":
  302. case "temperature":
  303. m |= eMood.temperate;
  304. break;
  305. case "toloud":
  306. case "loud":
  307. m |= eMood.toLoud;
  308. break;
  309. case "quiet":
  310. case "toquiet":
  311. m |= eMood.toQuiet;
  312. break;
  313. case "use":
  314. case "usefull":
  315. m |= eMood.usefull;
  316. break;
  317. case "nouse":
  318. case "useless":
  319. m |= eMood.useless;
  320. break;
  321. }
  322. }
  323. return m;
  324. }
  325. public bool Add(string Text, eMood Mood, ushort[] Tags) {
  326. string lText = Text.ToLower().Trim();
  327. foreach (ContentEntry ce in this.Entries) {
  328. if (ce.Text.ToLower() == lText) {
  329. ce.Mood = Mood;
  330. ce.Tags = Tags;
  331. return true;
  332. }
  333. }
  334. this.Entries.Add(new ContentEntry(Text.Trim(), Mood, Tags));
  335. return true;
  336. }
  337. public bool Add(ContentEntry entry) {
  338. this.Entries.Add(entry);
  339. return true;
  340. }
  341. public bool Add(string Text, string Mood, ushort[] Tags) {
  342. string lText = Text.ToLower().Trim();
  343. foreach (ContentEntry ce in this.Entries) {
  344. if (ce.Text.ToLower() == lText) {
  345. ce.Mood = GetMood(Mood);
  346. ce.Tags = Tags;
  347. return true;
  348. }
  349. }
  350. this.Entries.Add(new ContentEntry(Text.Trim(), GetMood(Mood), Tags));
  351. return true;
  352. }
  353. public bool Add(string Text, eMood Mood) {
  354. string lText = Text.ToLower().Trim();
  355. foreach (ContentEntry ce in this.Entries) {
  356. if (ce.Text.ToLower() == lText) {
  357. ce.Mood = Mood;
  358. return true;
  359. }
  360. }
  361. this.Entries.Add(new ContentEntry(Text.Trim(), Mood));
  362. return true;
  363. }
  364. public bool Add(string Text, string Mood) {
  365. string lText = Text.ToLower().Trim();
  366. foreach (ContentEntry ce in this.Entries) {
  367. if (ce.Text.ToLower() == lText) {
  368. ce.Mood = GetMood(Mood);
  369. return true;
  370. }
  371. }
  372. this.Entries.Add(new ContentEntry(Text.Trim(), GetMood(Mood)));
  373. return true;
  374. }
  375. public string[] Tags {
  376. get => this._tags.ToArray();
  377. set => this._tags = new List<string>(value);
  378. }
  379. public string[] OptionalTags {
  380. get => this._optionalTags.ToArray();
  381. set => this._optionalTags = new List<string>(value);
  382. }
  383. [SerializeIgnore]
  384. public ContentUseage[] TagUseage {
  385. get {
  386. List<ContentUseage> res = new List<ContentUseage>();
  387. for(ushort i=0;i<this._tags.Count;i++) {
  388. ContentUseage cu = new ContentUseage(i, this._tags[i]);
  389. foreach(ContentEntry ce in this.Entries) {
  390. if(ce.Tag_Has(i)) {
  391. cu.Count++;
  392. }
  393. }
  394. res.Add(cu);
  395. }
  396. return res.ToArray();
  397. }
  398. }
  399. [SerializeIgnore]
  400. public ContentUseage[] OptionalTagUseage {
  401. get {
  402. List<ContentUseage> res = new List<ContentUseage>();
  403. for (ushort i = 0; i < this._optionalTags.Count; i++) {
  404. ContentUseage cu = new ContentUseage(i, this._optionalTags[i]);
  405. foreach (ContentEntry ce in this.Entries) {
  406. if (ce.Tag_Has(i)) {
  407. cu.Count++;
  408. }
  409. }
  410. res.Add(cu);
  411. }
  412. return res.ToArray();
  413. }
  414. }
  415. public ushort Tag_Add(string tag) {
  416. if (this._tags.Contains(tag))
  417. return (ushort)this._tags.IndexOf(tag);
  418. ushort res = (ushort)this._tags.Count;
  419. this._tags.Add(tag);
  420. return res;
  421. }
  422. public void Tag_Remove(string tag) {
  423. if (!this._tags.Contains(tag))
  424. return;
  425. ushort tag_id = (ushort)this._tags.IndexOf(tag);
  426. this.Tag_RemoveAt(tag_id);
  427. }
  428. public void Tag_RemoveAt(ushort tag_id) {
  429. foreach (ContentEntry entry in this.Entries)
  430. entry.Tag_Remove(tag_id);
  431. foreach (ContentEntry entry in this.Entries)
  432. for (int i = 0, c = entry.Tags.Length; i < c; i++)
  433. if (entry.Tags[i] > tag_id)
  434. entry.Tags[i]--;
  435. this._tags.RemoveAt(tag_id);
  436. }
  437. public void Tag_Clear() {
  438. foreach (ContentEntry entry in this.Entries)
  439. entry.Tag_Clear();
  440. this._tags.Clear();
  441. }
  442. public ushort OptionalTag_Add(string tag) {
  443. if (this._tags.Contains(tag))
  444. return (ushort)this._optionalTags.IndexOf(tag);
  445. ushort res = (ushort)this._optionalTags.Count;
  446. this._optionalTags.Add(tag);
  447. return res;
  448. }
  449. public void OptionalTag_Remove(string tag) {
  450. if (!this._optionalTags.Contains(tag))
  451. return;
  452. ushort tag_id = (ushort)this._optionalTags.IndexOf(tag);
  453. this.OptionalTag_RemoveAt(tag_id);
  454. }
  455. public void OptionalTag_RemoveAt(ushort tag_id) {
  456. foreach (ContentEntry entry in this.Entries)
  457. entry.OptionalTag_Remove(tag_id);
  458. foreach (ContentEntry entry in this.Entries)
  459. for(int i=0,c=entry.OptionalTags.Length;i<c;i++)
  460. if(entry.OptionalTags[i]>tag_id)
  461. entry.OptionalTags[i]--;
  462. this._optionalTags.RemoveAt(tag_id);
  463. }
  464. public void OptionalTag_Clear() {
  465. foreach (ContentEntry entry in this.Entries)
  466. entry.OptionalTag_Clear();
  467. this._optionalTags.Clear();
  468. }
  469. }
  470. }