Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Kana

Public general use code classes and xml files that we've compiled and used over the years:

Kana class

   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:      /// Kana 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), Internet 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 Kana : Test
  34:      {
  35:          private static XDocument xDocument;
  36:   
  37:          protected override TestTopic testTopic
  38:          {
  39:              get
  40:              {
  41:                  return Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Kana;
  42:              }
  43:          }
  44:   
  45:          ////////////////////////////////////////////////////////////////////////////
  46:   
  47:          /// <summary>
  48:          ///
  49:          /// </summary>
  50:          public enum CharType { Hiragana, Katakana };
  51:   
  52:          ////////////////////////////////////////////////////////////////////////////
  53:   
  54:          /// <summary>
  55:          ///
  56:          /// </summary>
  57:          public struct Char
  58:          {
  59:              public string Hiragana;
  60:              public string Katakana;
  61:              public string Pronunciation;
  62:   
  63:              public Char(string hiragana, string katakana, string pronunciation)
  64:              {
  65:                  this.Hiragana = hiragana;
  66:                  this.Katakana = katakana;
  67:                  this.Pronunciation = pronunciation;
  68:              }
  69:          }
  70:   
  71:          ////////////////////////////////////////////////////////////////////////////
  72:   
  73:          /// <summary>
  74:          ///
  75:          /// </summary>
  76:          public Kana() { }
  77:   
  78:          ////////////////////////////////////////////////////////////////////////////
  79:   
  80:          /// <summary>
  81:          ///
  82:          /// </summary>
  83:          public override void PopulateTestDatabaseTableWithInitialQuestionsIfEmpty(string userId)
  84:          {
  85:              int count;
  86:              string question, answer;
  87:              CharType charType;
  88:              Ia.TentPlay.Cl.Model.Memorise.Score score;
  89:              List<Ia.TentPlay.Cl.Model.Memorise.Score> scoreList;
  90:   
  91:              // below: synch database words with XML list
  92:              using (var db = new Ia.TentPlay.Db.Memorise())
  93:              {
  94:                  count = (from s in db.Scores where s.TestId == (int)Ia.TentPlay.Cl.Model.Memorise.Test.TestTopic.Kana select s).Count();
  95:   
  96:                  if (count == 0)
  97:                  {
  98:                      scoreList = Ia.TentPlay.Cl.Model.Memorise.Score.KanaScoreList(userId);
  99:   
 100:                      foreach (bool b in new bool[] { true, false })
 101:                      {
 102:                          foreach (var @char in List)
 103:                          {
 104:                              if (b)
 105:                              {
 106:                                  question = @char.Hiragana;
 107:                                  charType = CharType.Hiragana;
 108:                              }
 109:                              else
 110:                              {
 111:                                  question = @char.Katakana;
 112:                                  charType = CharType.Katakana;
 113:                              }
 114:   
 115:                              answer = @char.Pronunciation;
 116:   
 117:                              score = (from s in scoreList where s.TestId == (int)testTopic && s.Question == question && s.Answer == answer && s.TypeId == (int)charType select s).FirstOrDefault();
 118:   
 119:                              if (score == null)
 120:                              {
 121:                                  score = new Ia.TentPlay.Cl.Model.Memorise.Score
 122:                                  {
 123:                                      Question = question,
 124:                                      Answer = answer,
 125:                                      TestId = (int)testTopic,
 126:                                      TypeId = (int)charType,
 127:                                      Created = DateTime.UtcNow.AddHours(3),
 128:                                      Updated = DateTime.UtcNow.AddHours(3),
 129:                                      Viewed = DateTime.UtcNow.AddHours(3)
 130:                                      //User.Id = identityUserId;
 131:                                  };
 132:   
 133:                                  db.Scores.Add(score);
 134:                              }
 135:                          }
 136:                      }
 137:   
 138:                      db.SaveChanges();
 139:                  }
 140:              }
 141:          }
 142:   
 143:          ////////////////////////////////////////////////////////////////////////////
 144:   
 145:          /// <summary>
 146:          ///
 147:          /// </summary>
 148:          public override int WeightedRandomTypeIdAccordingTypeDistribution(string userId)
 149:          {
 150:              CharType charType;
 151:   
 152:              if (Ia.Cl.Model.Default.RandomBool) charType = CharType.Hiragana;
 153:              else charType = CharType.Katakana;
 154:   
 155:              return (int)charType;
 156:          }
 157:   
 158:          ////////////////////////////////////////////////////////////////////////////
 159:   
 160:          /// <summary>
 161:          /// Read all Kana symbol characters
 162:          /// </summary>
 163:          public static List<Ia.TentPlay.Cl.Model.Memorise.Kana.Char> List
 164:          {
 165:              get
 166:              {
 167:                  Ia.TentPlay.Cl.Model.Memorise.Kana.Char @char;
 168:                  List<Ia.TentPlay.Cl.Model.Memorise.Kana.Char> charList;
 169:   
 170:                  charList = new List<Ia.TentPlay.Cl.Model.Memorise.Kana.Char>(2000);
 171:   
 172:                  foreach (XElement xe in XDocument.Element("kana").Elements("char"))
 173:                  {
 174:                      // <kana hiragana="あ" katakana="ア" pronunciation="a [a]" />
 175:   
 176:                      @char = new Ia.TentPlay.Cl.Model.Memorise.Kana.Char(
 177:                      xe.Attribute("hiragana").Value,
 178:                      xe.Attribute("katakana").Value,
 179:                      xe.Attribute("pronunciation").Value);
 180:   
 181:                      charList.Add(@char);
 182:                  }
 183:   
 184:                  return charList;
 185:              }
 186:          }
 187:   
 188:          ////////////////////////////////////////////////////////////////////////////
 189:          ////////////////////////////////////////////////////////////////////////////
 190:   
 191:          /// <summary>
 192:          /// 
 193:          /// How to embed and access resources by using Visual C# http://support.microsoft.com/kb/319292/en-us
 194:          /// 
 195:          /// 1. Change the "Build Action" property of your XML file from "Content" to "Embedded Resource".
 196:          /// 2. Add "using System.Reflection".
 197:          /// 3. See sample below.
 198:          /// 
 199:          /// </summary>
 200:          public static XDocument XDocument
 201:          {
 202:              get
 203:              {
 204:                  if (xDocument == null)
 205:                  {
 206:                      Assembly _assembly;
 207:                      StreamReader streamReader;
 208:   
 209:                      _assembly = Assembly.GetExecutingAssembly();
 210:                      streamReader = new StreamReader(_assembly.GetManifestResourceStream("Ia.TentPlay.Cl.model.data.memorise.kana.xml"));
 211:   
 212:                      try
 213:                      {
 214:                          if (streamReader.Peek() != -1)
 215:                          {
 216:                              xDocument = System.Xml.Linq.XDocument.Load(streamReader);
 217:                          }
 218:                      }
 219:                      catch (Exception)
 220:                      {
 221:                      }
 222:                      finally
 223:                      {
 224:                      }
 225:                  }
 226:   
 227:                  return xDocument;
 228:              }
 229:          }
 230:   
 231:          ////////////////////////////////////////////////////////////////////////////
 232:          ////////////////////////////////////////////////////////////////////////////    
 233:      }
 234:  }