1: using System;
2: using System.Collections;
3: using System.Collections.Generic;
4: using System.IO;
5: using System.Reflection;
6: using System.Linq;
7: using System.Data;
8: using System.Xml.Linq;
9:
10: namespace Ia.TentPlay.Cl.Model.Memorise
11: {
12: ////////////////////////////////////////////////////////////////////////////
13:
14: /// <summary publish="true">
15: /// Kanji 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 Kanji : Test
34: {
35: // <char id="1" wiki_id="1" name="一" grade="1" wiki_en="one" en="one" stroke="1" onyomi_ja="イチ、イツ" onyomi_en="ichi, itsu" kunyomi_ja="ひと、ひと-つ" kunyomi_en="hito, hito-tsu" />
36:
37: private static XDocument xDocument;
38:
39: protected override TestTopic testTopic
40: {
41: get
42: {
43: return Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Kanji;
44: }
45: }
46:
47: ////////////////////////////////////////////////////////////////////////////
48:
49: /// <summary>
50: ///
51: /// </summary>
52: //public enum WordType { Verb = 1, Noun, Adjective, Adverb, Unspecified };
53:
54: ////////////////////////////////////////////////////////////////////////////
55:
56: /// <summary>
57: ///
58: /// </summary>
59: public struct Symbol
60: {
61: public int Id;
62: public int WikiId;
63: public string Name;
64: public int Grade;
65: public string WikiEnglish;
66: public string English;
67: public int Stroke;
68: public string JapaneseOnyomi;
69: public string EnglishOnyomi;
70: public string JapaneseKunyomi;
71: public string EnglishKunyomi;
72:
73: public Symbol(int id, int wikiId, string name, int grade, string wikiEnglish, string english, int stroke, string japaneseOnyomi, string englishOnyomi, string japaneseKunyomi, string englishKunyomi)
74: {
75: this.Id = id;
76: this.WikiId = wikiId;
77: this.Name = name;
78: this.Grade = grade;
79: this.WikiEnglish = wikiEnglish;
80: this.English = english;
81: this.Stroke = stroke;
82: this.JapaneseOnyomi = japaneseOnyomi;
83: this.EnglishOnyomi = englishOnyomi;
84: this.JapaneseKunyomi = japaneseKunyomi;
85: this.EnglishKunyomi = englishKunyomi;
86: }
87: }
88:
89: ////////////////////////////////////////////////////////////////////////////
90:
91: /// <summary>
92: ///
93: /// </summary>
94: public Kanji() { }
95:
96: ////////////////////////////////////////////////////////////////////////////
97:
98: /// <summary>
99: ///
100: /// </summary>
101: public override void PopulateTestDatabaseTableWithInitialQuestionsIfEmpty(Guid userId)
102: {
103: int count;
104: string question, answer;
105: //WordType wordType;
106: Ia.TentPlay.Cl.Model.Memorise.Score score;
107: List<Ia.TentPlay.Cl.Model.Memorise.Score> scoreList;
108:
109: // below: synch database words with XML list
110: using (var db = new Ia.TentPlay.Db())
111: {
112: count = (from s in db.Scores where s.TestId == (int)Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Kanji select s).Count();
113:
114: if (count == 0)
115: {
116: scoreList = Ia.TentPlay.Cl.Model.Memorise.Score.KanjiScoreList(userId);
117:
118: foreach (var symbol in List)
119: {
120: question = symbol.Name;
121: answer = symbol.English;
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: Question = question,
130: Answer = answer,
131: TestId = (int)testTopic,
132: TypeId = 0, //(int)wordType
133: Created = DateTime.UtcNow.AddHours(3),
134: Updated = DateTime.UtcNow.AddHours(3),
135: Viewed = DateTime.UtcNow.AddHours(3),
136: UserId = userId
137: };
138:
139: db.Scores.Add(score);
140: }
141: }
142:
143: db.SaveChanges();
144: }
145: }
146: }
147:
148: ////////////////////////////////////////////////////////////////////////////
149:
150: /// <summary>
151: ///
152: /// </summary>
153: public override int WeightedRandomTypeIdAccordingTypeDistribution(Guid userId)
154: {
155: /*
156: int countOfAllTypes, countOfTypeId1, countOfTypeId2, countOfTypeId3, countOfTypeId4, countOfTypeId5, typeId;
157: ArrayList countArrayList;
158:
159: // public enum WordType { Verb = 1, Noun, Adjective, Adverb, Unspecified };
160:
161: using (var db = new Ia.TentPlay.Db())
162: {
163: countOfAllTypes = (from s in db.Scores where s.TestId == (int)testTopic select s).Count();
164: countOfTypeId1 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Verb select s).Count();
165: countOfTypeId2 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Noun select s).Count();
166: countOfTypeId3 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Adjective select s).Count();
167: countOfTypeId4 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Adverb select s).Count();
168: countOfTypeId5 = (from s in db.Scores where s.TestId == (int)testTopic && s.TypeId == (int)WordType.Unspecified select s).Count();
169:
170: countArrayList = new ArrayList(countOfAllTypes);
171:
172: // below: populate ArrayList with TypeId keys a number of times equal to its count
173: for (int i = 0; i < countOfTypeId1; i++) countArrayList.Add((int)WordType.Verb);
174: for (int i = 0; i < countOfTypeId2; i++) countArrayList.Add((int)Noun.Verb);
175: for (int i = 0; i < countOfTypeId3; i++) countArrayList.Add((int)Adjective.Verb);
176: for (int i = 0; i < countOfTypeId4; i++) countArrayList.Add((int)Adverb.Verb);
177: for (int i = 0; i < countOfTypeId5; i++) countArrayList.Add((int)Unspecified.Verb);
178:
179: typeId = (int)countArrayList[random.Next(countOfAllTypes)];
180: }
181: */
182:
183: return 0; // typeId;
184: }
185:
186: ////////////////////////////////////////////////////////////////////////////
187:
188: /// <summary>
189: /// Read all Kanji symbol characters
190: /// </summary>
191: public static List<Ia.TentPlay.Cl.Model.Memorise.Kanji.Symbol> List
192: {
193: get
194: {
195: Ia.TentPlay.Cl.Model.Memorise.Kanji.Symbol symbol;
196: List<Ia.TentPlay.Cl.Model.Memorise.Kanji.Symbol> symbolList;
197:
198: symbolList = new List<Ia.TentPlay.Cl.Model.Memorise.Kanji.Symbol>(2000);
199:
200: foreach (XElement xe in XDocument.Element("kanji").Elements("char"))
201: {
202: // <char id="1" wikiId="1" name="一" grade="1" wikiEnglish="one" english="one" stroke="1" onyomiJapanese="イチ、イツ" onyomiEnglish="ichi, itsu" kunyomiJapanese="ひと、ひと-つ" kunyomiEnglish="hito, hito-tsu" />
203:
204: symbol = new Ia.TentPlay.Cl.Model.Memorise.Kanji.Symbol(
205: int.Parse(xe.Attribute("id").Value),
206: int.Parse(xe.Attribute("wikiId").Value),
207: xe.Attribute("name").Value,
208: int.Parse(xe.Attribute("grade").Value),
209: xe.Attribute("wikiEnglish").Value,
210: xe.Attribute("english").Value,
211: int.Parse(xe.Attribute("stroke").Value),
212: xe.Attribute("japaneseOnyomi").Value,
213: xe.Attribute("englishOnyomi").Value,
214: xe.Attribute("japaneseKunyomi").Value,
215: xe.Attribute("englishKunyomi").Value);
216:
217: symbolList.Add(symbol);
218: }
219:
220: return symbolList;
221: }
222: }
223:
224: ////////////////////////////////////////////////////////////////////////////
225: ////////////////////////////////////////////////////////////////////////////
226:
227: /// <summary>
228: ///
229: /// How to embed and access resources by using Visual C# http://support.microsoft.com/kb/319292/en-us
230: ///
231: /// 1. Change the "Build Action" property of your XML file from "Content" to "Embedded Resource".
232: /// 2. Add "using System.Reflection".
233: /// 3. Manifest resource stream will start with the project namespace, the location of XML file.
234: ///
235: /// </summary>
236: public static XDocument XDocument
237: {
238: get
239: {
240: if (xDocument == null)
241: {
242: Assembly _assembly;
243: StreamReader streamReader;
244:
245: _assembly = Assembly.GetExecutingAssembly();
246: streamReader = new StreamReader(_assembly.GetManifestResourceStream("Ia.TentPlay.Cl.model.data.memorise.kanji.xml"));
247:
248: try
249: {
250: if (streamReader.Peek() != -1)
251: {
252: xDocument = System.Xml.Linq.XDocument.Load(streamReader);
253: }
254: }
255: catch (Exception)
256: {
257: }
258: finally
259: {
260: }
261: }
262:
263: return xDocument;
264: }
265: }
266:
267: ////////////////////////////////////////////////////////////////////////////
268: ////////////////////////////////////////////////////////////////////////////
269: }
270: }