/// <summary> /// Loads properties from the given vocab entity. /// </summary> /// <param name="se">Target SRS entry.</param> /// <param name="v">Vocab to load.</param> public static void LoadFromVocab(this SrsEntry se, VocabEntity v) { // Compute the meaning string. string meaningString = string.Empty; foreach (VocabMeaning vm in v.Meanings) { meaningString += MultiValueFieldHelper.ReplaceSeparator(vm.Meaning) .Replace(" ;", MultiValueFieldHelper.ValueSeparator.ToString()) + MultiValueFieldHelper.ValueSeparator; } meaningString = meaningString.Trim( new char[] { MultiValueFieldHelper.ValueSeparator }); meaningString = MultiValueFieldHelper.Trim(meaningString); meaningString = MultiValueFieldHelper.Expand(meaningString); // Set values. se.Meanings = meaningString; se.Readings = v.KanaWriting; se.AssociatedVocab = v.KanjiWriting; if (string.IsNullOrEmpty(se.AssociatedVocab)) { se.AssociatedVocab = v.KanaWriting; } }
public ExtendedVocab(VocabEntity dbVocab) : this(dbVocab, null) { }
/// <summary> /// Retrieves and includes the SRS entries matching the given vocab and includes /// them in the entity. /// </summary> private void IncludeSrsEntries(DaoConnection connection, VocabEntity vocab) { string value = string.IsNullOrEmpty(vocab.KanjiWriting) ? vocab.KanaWriting : vocab.KanjiWriting; IEnumerable<NameValueCollection> nvcEntries = connection.Query( "SELECT * " + "FROM " + SqlHelper.Table_SrsEntry + " srs " + "WHERE srs." + SqlHelper.Field_SrsEntry_AssociatedVocab + "=@k", new DaoParameter("@k", value)); SrsEntryBuilder srsEntryBuilder = new SrsEntryBuilder(); foreach (NameValueCollection nvcEntry in nvcEntries) { vocab.SrsEntries.Add(srsEntryBuilder.BuildEntity(nvcEntry, null)); } }
/// <summary> /// Includes the meanings of the given vocab in the entity. /// </summary> private void IncludeMeanings(DaoConnection connection, VocabEntity vocab) { IEnumerable<NameValueCollection> meanings = connection.Query( "SELECT vm.* FROM " + SqlHelper.Table_Vocab_VocabMeaning + " vvm " + "JOIN " + SqlHelper.Table_VocabMeaning + " vm ON (vvm." + SqlHelper.Field_Vocab_VocabMeaning_VocabMeaningId + "=vm." + SqlHelper.Field_VocabMeaning_Id + ") WHERE vvm." + SqlHelper.Field_Vocab_VocabMeaning_VocabId + "=@vid", new DaoParameter("@vid", vocab.ID)); VocabMeaningBuilder meaningBuilder = new VocabMeaningBuilder(); foreach (NameValueCollection nvcMeaning in meanings) { VocabMeaning meaning = meaningBuilder.BuildEntity(nvcMeaning, null); IncludeMeaningCategories(connection, meaning); vocab.Meanings.Add(meaning); } }
/// <summary> /// Include the categories of the given vocab in the entity. /// </summary> private void IncludeCategories(DaoConnection connection, VocabEntity vocab) { IEnumerable<NameValueCollection> categories = connection.Query( "SELECT vc.* FROM " + SqlHelper.Table_VocabCategory_Vocab + " vcv " + "JOIN " + SqlHelper.Table_VocabCategory + " vc ON (vcv." + SqlHelper.Field_VocabCategory_Vocab_VocabCategoryId + "=vc." + SqlHelper.Field_VocabCategory_Id + ") WHERE vcv." + SqlHelper.Field_VocabCategory_Vocab_VocabId + "=@vid", new DaoParameter("@vid", vocab.ID)); VocabCategoryBuilder categoryBuilder = new VocabCategoryBuilder(); foreach (NameValueCollection nvcCategory in categories) { VocabCategory category = categoryBuilder.BuildEntity(nvcCategory, null); vocab.Categories.Add(category); } }
/// <summary> /// Includes the vocab variants in the entity. /// </summary> private void IncludeVariants(DaoConnection connection, VocabEntity vocab) { IEnumerable<NameValueCollection> results = connection.Query( "SELECT * FROM " + SqlHelper.Table_Vocab + " WHERE " + SqlHelper.Field_Vocab_GroupId + "=@gid AND " + SqlHelper.Field_Vocab_Id + "!=@id", new DaoParameter("@gid", vocab.GroupId), new DaoParameter("@id", vocab.ID)); VocabBuilder builder = new VocabBuilder(); foreach (NameValueCollection nvcVocab in results) { vocab.Variants.Add(builder.BuildEntity(nvcVocab, null)); } }
/// <summary> /// Includes the kanji of the given vocab in the entity. /// </summary> private void IncludeKanji(DaoConnection connection, DaoConnection srsConnection, VocabEntity vocab) { IEnumerable<NameValueCollection> results = connection.Query( "SELECT k.* FROM " + SqlHelper.Table_Kanji_Vocab + " kv " + "JOIN " + SqlHelper.Table_Kanji + " k ON (k." + SqlHelper.Field_Kanji_Id + "=kv." + SqlHelper.Field_Kanji_Vocab_KanjiId + ") WHERE kv." + SqlHelper.Field_Kanji_Vocab_VocabId + "=@vid", new DaoParameter("@vid", vocab.ID)); KanjiBuilder kanjiBuilder = new KanjiBuilder(); foreach (NameValueCollection nvcKanji in results) { KanjiEntity kanji = kanjiBuilder.BuildEntity(nvcKanji, null); KanjiDao.IncludeKanjiMeanings(connection, kanji); KanjiDao.IncludeRadicals(connection, kanji); KanjiDao.IncludeSrsEntries(srsConnection, kanji); vocab.Kanji.Add(kanji); } }
private VocabWritingPart MakeKanaPart(VocabEntity vocab, string reading) { VocabWritingPart part = new VocabWritingPart(); part.Furigana = string.Empty; part.OriginalVocab = vocab; part.Characters = new List<KanjiWritingCharacter>(); foreach (char c in reading) { part.Characters.Add(MakeCharacter(vocab, c)); } return part; }
/// <summary> /// Parses a reading element node. /// Updates the list with the available info. /// </summary> /// <param name="xreadingElement">Element to parse.</param> /// <param name="vocabList">Vocab list to be updated.</param> private void ParseReading(XElement xreadingElement, List<VocabEntity> vocabList, int groupId) { // First, we have to determine the target of the reading node. // Two possible cases: // - Scenario 1: There were no kanji readings. In that case, the reading should // add a new vocab element which has no kanji reading. // - Scenario 2: There was at least one kanji reading. In that case, the reading // node targets a set of existing vocabs. They may be filtered by kanji reading // with the reading constraint nodes. VocabEntity[] targets; if (!vocabList.Any()) { // Scenario 1. Create a new kanji reading, add it to the list, and set it as target. VocabEntity newVocab = new VocabEntity(); newVocab.GroupId = groupId; vocabList.Add(newVocab); targets = new VocabEntity[] { newVocab }; } else { // Scenario 2. Check constraint nodes to filter the targets. // Get all reading constraints in an array. string[] readingConstraints = xreadingElement.Elements(XmlNode_ReadingConstraint) .Select(x => x.Value).ToArray(); // Filter from the vocab list. if (readingConstraints.Any()) { targets = vocabList.Where(v => readingConstraints.Contains(v.KanjiWriting)).ToArray(); } else { targets = vocabList.ToArray(); } } // Now that we have the target vocabs, we can get the proper information from the node. string kanaReading = xreadingElement.Element(XmlNode_KanaReading).Value; bool isCommon = IsCommonWord(xreadingElement, XmlNode_ReadingVocabReference); // Get the optional categories defined by the "reading info" nodes. VocabCategory[] categories = xreadingElement.Elements(XmlNode_ReadingInfo) .Select(x => GetCategoryByLabel(x.Value)) .Where(c => c != null).ToArray(); // We have the info. Now we can apply it to the targets. // For each target foreach (VocabEntity target in targets) { // Set the kana reading if not already set. if (string.IsNullOrEmpty(target.KanaWriting)) { target.KanaWriting = kanaReading; // Set the common flag to false only if both the kanji and the reading values are false. target.IsCommon = target.IsCommon || isCommon; // Append the categories to the existing collection. target.Categories = target.Categories.Concat(categories).ToArray(); } else if (!vocabList.Where(v => v.KanaWriting == kanaReading).Any() && target == targets.Last()) { // If a target already has a kana reading, we need to create a new vocab. VocabEntity newVocab = new VocabEntity() { GroupId = target.GroupId, KanjiWriting = target.KanjiWriting, // Assign the old kanji reading, IsCommon = target.IsCommon || isCommon, // combined common flag, FrequencyRank = target.FrequencyRank, // same frequency rank, KanaWriting = kanaReading, // new kana reading, Categories = target.Categories.Concat(categories).ToArray() // combined categories. }; vocabList.Add(newVocab); } } }
/// <summary> /// Parses a kanji element node. /// Updates the list with the available info. /// </summary> /// <param name="xkanjiElement">Element to parse.</param> /// <param name="vocabList">Vocab list to be updated.</param> /// <param name="groupId">Current group ID.</param> private void ParseKanji(XElement xkanjiElement, List<VocabEntity> vocabList, int groupId) { // Create a new vocab with the associated writing. VocabEntity vocab = new VocabEntity(); vocab.GroupId = groupId; vocab.KanjiWriting = xkanjiElement.Element(XmlNode_KanjiReading).Value; vocab.IsCommon = IsCommonWord(xkanjiElement, XmlNode_KanjiVocabReference); // For each kanji info node foreach (XElement xkanjiInf in xkanjiElement.Elements(XmlNode_KanjiInfo)) { // Associate the vocab with the category referred by the info. VocabCategory category = GetCategoryByLabel(xkanjiInf.Value); if (category != null && !vocab.Categories.Contains(category)) { vocab.Categories.Add(category); } } // Add the created vocab to the list. vocabList.Add(vocab); }
public VocabVariant(ExtendedVocab parent, VocabEntity variant) { Parent = parent; Variant = variant; }
public VocabAudio(VocabEntity vocab) { KanjiReading = vocab.KanjiWriting; KanaReading = vocab.KanaWriting; }
private VocabWritingPart MakeFuriganaPart(VocabEntity vocab, FuriganaPart furiganaPart) { VocabWritingPart part = new VocabWritingPart(); part.OriginalVocab = vocab; part.Furigana = furiganaPart.Value; part.Characters = new List<KanjiWritingCharacter>(); for (int i = furiganaPart.StartIndex; i <= furiganaPart.EndIndex; i++) { part.Characters.Add(MakeCharacter(vocab, vocab.KanjiWriting[i])); } return part; }
private KanjiWritingCharacter MakeCharacter(VocabEntity vocab, char c) { return new KanjiWritingCharacter() { Character = c, Kanji = vocab.Kanji.Where(k => k.Character == c.ToString()).FirstOrDefault(), OriginalVocab = vocab }; }
public ExtendedVocab(VocabEntity dbVocab, ExtendedSrsEntry srsEntry) { DbVocab = dbVocab; Audio = new VocabAudio(dbVocab); SrsEntry = srsEntry; }
public void UpdateFrequencyRank(VocabEntity vocab, int rank) { DaoConnection connection = null; try { //connection = DaoConnection.Open(DaoConnectionEnum.KanjiDatabase); connection = _connection; connection.ExecuteNonQuery("UPDATE " + SqlHelper.Table_Vocab + " SET " + SqlHelper.Field_Vocab_FrequencyRank + "=@rank " + "WHERE " + SqlHelper.Field_Vocab_Id + "=@id", new DaoParameter("@rank", rank), new DaoParameter("@id", vocab.ID)); } finally { //if (connection != null) //{ // connection.Dispose(); //} } }
/// <summary> /// Background task work method. /// Retrieves the first matching vocab. /// </summary> private void DoGetAssociatedVocab(object sender, DoWorkEventArgs e) { IEnumerable<VocabEntity> results = _vocabDao.GetMatchingVocab(AssociatedVocabString); if (results.Any()) { VocabEntity entity = new VocabEntity(); StringBuilder meanings = new StringBuilder(); StringBuilder readings = new StringBuilder(); foreach (VocabEntity result in results) { foreach (VocabMeaning meaning in result.Meanings) { meanings.Append(MultiValueFieldHelper.ReplaceSeparator(meaning.Meaning) .Replace(';', MultiValueFieldHelper.ValueSeparator) + MultiValueFieldHelper.ValueSeparator); } readings.Append(result.KanaWriting + MultiValueFieldHelper.ValueSeparator); } entity.Meanings.Add(new VocabMeaning() { Meaning = MultiValueFieldHelper.Expand(MultiValueFieldHelper.Distinct(meanings.ToString())) }); entity.KanaWriting = MultiValueFieldHelper.Expand(MultiValueFieldHelper.Distinct(readings.ToString())); AssociatedVocab = entity; } else { AssociatedVocab = null; } }
public void LoadFromVocab(VocabEntity v) { Reference.LoadFromVocab(v); RaisePropertyChanged("Meanings"); RaisePropertyChanged("Readings"); }