1: using System.Xml.Linq;
2: using System.Data;
3: using System.Collections;
4: using System.Reflection;
5:
6: namespace Ia.TentPlay.Cl.Model.Memorise
7: {
8: ////////////////////////////////////////////////////////////////////////////
9:
10: /// <summary publish="true">
11: /// German class
12: /// </summary>
13: /// <value>
14: /// https://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx
15: /// </value>
16: /// <remarks>
17: /// Copyright © 2008-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
18: ///
19: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
20: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
21: ///
22: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
23: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24: ///
25: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
26: ///
27: /// Copyright notice: This notice may not be removed or altered from any source distribution.
28: /// </remarks>
29: public class German : Test
30: {
31: private XDocument xDocument;
32: protected override TestTopic testTopic
33: {
34: get
35: {
36: return Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.German;
37: }
38: }
39:
40: ////////////////////////////////////////////////////////////////////////////
41:
42: /// <summary>
43: ///
44: /// </summary>
45: public enum WordType { Verb = 1, Noun, Adjective, Adverb, Unspecified };
46:
47: ////////////////////////////////////////////////////////////////////////////
48:
49: /// <summary>
50: ///
51: /// </summary>
52: public struct Word
53: {
54: public WordType Type;
55: public string English;
56: public string German;
57:
58: /// <summary/>
59: public Word(string english, string german, WordType type)
60: {
61: this.Type = type;
62: this.English = english;
63: this.German = german;
64: }
65:
66: /// <summary/>
67: public override string ToString()
68: {
69: return this.German + ":" + this.English + ":" + this.Type;
70: }
71: }
72:
73: ////////////////////////////////////////////////////////////////////////////
74:
75: /// <summary>
76: ///
77: /// </summary>
78: public German() { }
79:
80: ////////////////////////////////////////////////////////////////////////////
81:
82: /// <summary>
83: ///
84: /// </summary>
85: public override void PopulateTestDatabaseTableWithInitialQuestionsIfEmpty(Guid userId)
86: {
87: int count;
88: string question, answer;
89: XDocument xDocument;
90: WordType wordType;
91: Ia.TentPlay.Cl.Model.Memorise.Score score;
92: List<Ia.TentPlay.Cl.Model.Memorise.Score> scoreList;
93:
94: xDocument = XDocument;
95:
96: // below: synch database words with XML list
97: using (var db = new Ia.TentPlay.Db())
98: {
99: count = (from s in db.Scores where s.TestId == (int)Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.German select s).Count();
100:
101: if (count == 0)
102: {
103: scoreList = Ia.TentPlay.Cl.Model.Memorise.Score.GermanScoreList(userId);
104:
105: foreach (XElement xe in xDocument.Elements("german").Elements())
106: {
107: switch (xe.Name.LocalName)
108: {
109: case "verb": wordType = WordType.Verb; break;
110: case "noun": wordType = WordType.Noun; break;
111: case "adjective": wordType = WordType.Adjective; break;
112: case "adverb": wordType = WordType.Adverb; break;
113: default: wordType = WordType.Unspecified; break;
114: }
115:
116: question = xe.Attribute("german").Value;
117: answer = xe.Attribute("english").Value;
118:
119: score = (from s in scoreList where s.TestId == (int)testTopic && s.Question == question && s.Answer == answer && s.TypeId == (int)wordType select s).FirstOrDefault();
120:
121: if (score == null)
122: {
123: score = new Ia.TentPlay.Cl.Model.Memorise.Score();
124:
125: score.Question = question;
126: score.Answer = answer;
127: score.TestId = (int)testTopic;
128: score.TypeId = (int)wordType;
129: score.Created = score.Updated = score.Viewed = DateTime.UtcNow.AddHours(3);
130: score.UserId = userId;
131:
132: db.Scores.Add(score);
133: }
134: }
135:
136: db.SaveChanges();
137: }
138: }
139: }
140:
141: ////////////////////////////////////////////////////////////////////////////
142:
143: /// <summary>
144: ///
145: /// </summary>
146: public override int WeightedRandomTypeIdAccordingTypeDistribution(Guid userId)
147: {
148: int countOfAllTypes, countOfTypeId1, countOfTypeId2, countOfTypeId3, countOfTypeId4, countOfTypeId5, typeId;
149: ArrayList countArrayList;
150:
151: // public enum WordType { Verb = 1, Noun, Adjective, Adverb, Unspecified };
152:
153: using (var db = new Ia.TentPlay.Db())
154: {
155: countOfAllTypes = (from s in db.Scores where s.TestId == (int)testTopic select s).Count();
156: countOfTypeId1 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Verb select s).Count();
157: countOfTypeId2 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Noun select s).Count();
158: countOfTypeId3 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Adjective select s).Count();
159: countOfTypeId4 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Adverb select s).Count();
160: countOfTypeId5 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Unspecified select s).Count();
161:
162: countArrayList = new ArrayList(countOfAllTypes);
163:
164: // below: populate ArrayList with TypeId keys a number of times equal to its count
165: for (int i = 0; i < countOfTypeId1; i++) countArrayList.Add((int)WordType.Verb);
166: for (int i = 0; i < countOfTypeId2; i++) countArrayList.Add((int)WordType.Noun);
167: for (int i = 0; i < countOfTypeId3; i++) countArrayList.Add((int)WordType.Adjective);
168: for (int i = 0; i < countOfTypeId4; i++) countArrayList.Add((int)WordType.Adverb);
169: for (int i = 0; i < countOfTypeId5; i++) countArrayList.Add((int)WordType.Unspecified);
170:
171: typeId = (int)countArrayList[random.Next(countOfAllTypes)];
172: }
173:
174: return typeId;
175: }
176:
177: /*
178: ////////////////////////////////////////////////////////////////////////////
179:
180: /// <summary>
181: ///
182: /// </summary>
183: [Obsolete("", true)]
184: public static void CheckAndMergeGermanXmlEntries()
185: {
186: StringBuilder sb;
187: Word final;
188: List<Word> firstList, middleList, tempList, lastList;
189:
190: firstList = ReadList();
191: middleList = ReadList();
192:
193: sb = new StringBuilder(firstList.Count * 100);
194:
195: tempList = new List<Word>(10);
196:
197:
198: // below: examine and handle exact (English, German, and type) redundant entries
199:
200: lastList = new List<Word>(firstList.Count);
201:
202: foreach (Word word in firstList)
203: {
204: if (!lastList.Contains(word))
205: {
206: tempList = (from q in middleList where q.German == word.German && q.English == word.English && q.Type == word.Type select q).ToList<Word>();
207:
208: if (tempList.Count > 0)
209: {
210: final = new Word();
211:
212: final.German = word.German;
213: final.English = word.English;
214: final.Type = word.Type;
215:
216: lastList.Add(final);
217:
218: middleList.Remove(final);
219: }
220: else
221: {
222: }
223: }
224: }
225:
226:
227: // below: examine and handle redundant (English, German) entries but keep the one with a word type
228:
229: firstList = lastList;
230: middleList.Clear();
231: lastList.ForEach(i => middleList.Add(i));
232:
233: lastList = new List<Word>(firstList.Count);
234:
235: foreach (Word word in firstList)
236: {
237: if (!lastList.Contains(word))
238: {
239: tempList = (from q in middleList where q.German == word.German && q.English == word.English select q).ToList<Word>();
240:
241: if (tempList.Count == 1 || tempList.Count == 2)
242: {
243: final = new Word();
244:
245: final.German = word.German;
246: final.English = word.English;
247:
248: if (tempList.Count == 1)
249: {
250: final.Type = word.Type;
251: lastList.Add(final);
252: middleList.Remove(final);
253: }
254: else if (tempList.Count == 2)
255: {
256: if (tempList[0].Type != WordType.Unspecified && tempList[1].Type == WordType.Unspecified)
257: {
258: final.Type = tempList[0].Type;
259: lastList.Add(final);
260:
261: middleList.Remove(tempList[0]);
262: middleList.Remove(tempList[1]);
263: }
264: else if (tempList[0].Type == WordType.Unspecified && tempList[1].Type != WordType.Unspecified)
265: {
266: final.Type = tempList[1].Type;
267: lastList.Add(final);
268:
269: middleList.Remove(tempList[0]);
270: middleList.Remove(tempList[1]);
271: }
272: else
273: {
274: // below: a word is both adverb and adjective
275: final.Type = tempList[0].Type;
276: lastList.Add(final);
277: middleList.Remove(final);
278:
279: final.Type = tempList[1].Type;
280: lastList.Add(final);
281: middleList.Remove(final);
282: }
283: }
284: }
285: else
286: {
287: // below: manually fix XML
288: }
289: }
290: }
291:
292:
293: // below: remove exact redundant entries (German only)
294:
295: firstList = lastList;
296: middleList.Clear();
297: lastList.ForEach(i => middleList.Add(i));
298:
299: lastList = new List<Word>(firstList.Count);
300:
301: foreach (Word word in firstList)
302: {
303: if (!lastList.Contains(word))
304: {
305: tempList = (from q in middleList where q.German == word.German select q).ToList<Word>();
306:
307: if (tempList.Count > 0)
308: {
309: if (tempList.Count == 1 || tempList.Count == 2)
310: {
311: final = new Word();
312:
313: final.German = word.German;
314:
315: if (tempList.Count == 1)
316: {
317: final.Type = word.Type;
318: final.English = word.English;
319: lastList.Add(final);
320: middleList.Remove(final);
321: }
322: else if (tempList.Count == 2)
323: {
324: if (tempList[0].English.Contains(tempList[1].English))
325: {
326: final.Type = tempList[0].Type;
327: final.English = tempList[0].English;
328: lastList.Add(final);
329:
330: middleList.Remove(tempList[0]);
331: middleList.Remove(tempList[1]);
332: }
333: else if (tempList[1].English.Contains(tempList[0].English))
334: {
335: final.Type = tempList[1].Type;
336: final.English = tempList[1].English;
337: lastList.Add(final);
338:
339: middleList.Remove(tempList[0]);
340: middleList.Remove(tempList[1]);
341: }
342: else
343: {
344: final.Type = tempList[0].Type;
345: final.English = tempList[0].English;
346: lastList.Add(final);
347: middleList.Remove(tempList[0]);
348:
349: final.Type = tempList[1].Type;
350: final.English = tempList[1].English;
351: lastList.Add(final);
352: middleList.Remove(tempList[1]);
353:
354: //sb.AppendLine(tempList[0].ToString());
355: //sb.AppendLine(tempList[1].ToString());
356: }
357: }
358: }
359: else
360: {
361: // below: manually fix XML
362:
363: sb.AppendLine(word.ToString());
364: }
365: }
366: }
367: }
368:
369: // below: create new XML file
370: foreach (Word word in lastList)
371: {
372: sb.AppendLine(@"<" + word.Type.ToString().ToLower() + @" german=""" + word.German + @""" english=""" + word.English + @"""/>");
373: }
374:
375: }
376: */
377:
378: ////////////////////////////////////////////////////////////////////////////
379: ////////////////////////////////////////////////////////////////////////////
380:
381: /// <summary>
382: ///
383: /// How to embed and access resources by using Visual C# http://support.microsoft.com/kb/319292/en-us
384: ///
385: /// 1. Change the "Build Action" property of your XML file from "Content" to "Embedded Resource".
386: /// 2. Add "using System.Reflection".
387: /// 3. Manifest resource stream will start with the project namespace, the location of XML file.
388: ///
389: /// </summary>
390: public XDocument XDocument
391: {
392: get
393: {
394: if (xDocument == null)
395: {
396: Assembly _assembly;
397: StreamReader streamReader;
398:
399: _assembly = Assembly.GetExecutingAssembly();
400: streamReader = new StreamReader(_assembly.GetManifestResourceStream("Ia.TentPlay.Cl.model.data.memorise.german.xml"));
401:
402: try
403: {
404: if (streamReader.Peek() != -1)
405: {
406: xDocument = System.Xml.Linq.XDocument.Load(streamReader);
407: }
408: }
409: catch (Exception)
410: {
411: }
412: finally
413: {
414: }
415: }
416:
417: return xDocument;
418: }
419: }
420:
421: ////////////////////////////////////////////////////////////////////////////
422: ////////////////////////////////////////////////////////////////////////////
423: }
424:
425: ////////////////////////////////////////////////////////////////////////////
426: ////////////////////////////////////////////////////////////////////////////
427: }