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