/// <summary> /// Indicates if the given entities match the radical. /// </summary> /// <param name="entities">Entities to test.</param> /// <returns>True if the given entities match the extended radical. /// False otherwise.</returns> public bool DoesMatch(RadicalEntity[] entities) { long[] mandatoryIds = RadicalGroups .Where(g => g.Radicals.Count() == 1) .Select(g => g.Radicals.First().ID) .ToArray(); long[][] idOptionGroups = RadicalGroups .Where(g => g.Radicals.Count() > 1) .Select(g => g.Radicals .Select(r => r.ID) .ToArray()) .ToArray(); long[] entitiesIds = entities .Select(r => r.ID) .ToArray(); // Intersect. if (entitiesIds.Intersect(mandatoryIds).Count() == mandatoryIds.Count()) { foreach (long[] idGroup in idOptionGroups) { if (!entitiesIds.Intersect(idGroup).Any()) { // No match for this option group. return false; } } // All mandatory matches // + At least one match for each option group. // // The submitted entities match the radical. return true; } // All mandatory IDs do not match. return false; }
/// <summary> /// Gets the radicals and stores them in the database. /// </summary> public override void Execute() { // Parse the files. RadicalDictionary = ParseKradFiles(); // Create a new bulk insert object. using (SQLiteBulkInsert<RadicalEntity> radicalInsert = new SQLiteBulkInsert<RadicalEntity>(RadicalMaxCommit)) { Dictionary<string, RadicalEntity> addedRadicals = new Dictionary<string, RadicalEntity>(); foreach (var composition in RadicalDictionary) { // For each composition read, browse the radicals. foreach (RadicalValue radicalValue in composition.Value) { if (addedRadicals.ContainsKey(radicalValue.Character)) { // The radical was already found and added. // Just set the radical of the RadicalValue. radicalValue.Radical = addedRadicals[radicalValue.Character]; } else { // Store in the database the radicals that have not already been stored in the // "already added" dictionary. RadicalEntity radical = new RadicalEntity() { Character = radicalValue.Character }; radical.ID = radicalInsert.Insert(radical); RadicalCount++; // Set the radical of the RadicalValue and add an entry to the "already added" dictionary. radicalValue.Radical = radical; addedRadicals.Add(radicalValue.Character, radical); // Log _log.InfoFormat("Added radical {0} ({1})", radical.Character, radical.ID); } } } } }
/// <summary> /// Retrieves and includes kanji entities in the given radical entity. /// </summary> private void IncludeKanji(DaoConnection connection, RadicalEntity radical) { IEnumerable<NameValueCollection> results = connection.Query( "SELECT kr." + SqlHelper.Field_Kanji_Radical_KanjiId + " " + SqlHelper.Field_Kanji_Id + " FROM " + SqlHelper.Table_Kanji_Radical + " kr WHERE kr." + SqlHelper.Field_Kanji_Radical_RadicalId + "=@rid", new DaoParameter("@rid", radical.ID)); KanjiBuilder kanjiBuilder = new KanjiBuilder(); foreach (NameValueCollection nvcKanjiRadical in results) { KanjiEntity kanji = kanjiBuilder.BuildEntity(nvcKanjiRadical, null); radical.Kanji.Add(kanji); } }