/// <summary> /// Attempts to parse a string on the <see cref="TagDelimiter"/> /// followed by a conversion of the string array into <see cref="TagsetBase"/> /// list. /// </summary> /// <param name="taggedString">The string to be parsed into a list of <see cref="TagsetBase"/>.</param> /// <param name="tagset">The resulting list, will be null when return value is false.</param> /// <remarks> /// The argument <see cref="taggedString"/> is split first on the space character (0x20) then each /// value in the resulting array is split on the <see cref="TagDelimiter"/>. Non-matching results are /// assigned to a <see cref="NullTagset"/> instance - this is only the case when there was at least one /// valid match in the results. /// </remarks> /// <returns>True if even one match was found, false otherwise.</returns> public static bool TryParse(string taggedString, out TagsetBase[] tagset) { if(string.IsNullOrWhiteSpace(taggedString)) { tagset = null; return false; } if(!taggedString.Contains(TagDelimiter.ToString())) { tagset = null; return false; } try { tagset = Parse(taggedString); } catch { tagset = null; return false; } if (tagset.Length == 0 || tagset.All(t => t.GetType() == typeof(NullTagset))) { tagset = null; return false; } return true; }
/// <summary> /// Attempts to parse a string on the <see cref="TagDelimiter"/> /// followed by a conversion of the string array of array's into <see cref="TagsetBase"/> /// list. /// </summary> /// <param name="taggedString"></param> /// <param name="tagsets"></param> /// <returns></returns> public static bool TryParse(string taggedString, out TagsetBase[][] tagsets) { if (string.IsNullOrWhiteSpace(taggedString)) { tagsets = null; return false; } try { //get all tags as a single array TagsetBase[] allTags; if(!TryParse(taggedString,out allTags)) { tagsets = null; return false; } //determine how many sentence breaks the array contains var numberOfSentences = allTags.Count(tag => tag.GetType() == typeof (SentenceBreakPunctuation)); //instantiate the out variable tagsets = new TagsetBase[numberOfSentences+1][];//the sentence break and end-of-data may not be coterminus var sentenceIndex = 0; var tagBuffer = new List<TagsetBase>(); for(var i = 0; i< allTags.Length; i++) { var tagEntry = allTags[i]; tagBuffer.Add(tagEntry); //flush the buffer on each sentence break or at the end of the array if (tagEntry.GetType() == typeof(SentenceBreakPunctuation) || i == allTags.Length - 1) { tagsets[sentenceIndex] = tagBuffer.ToArray(); tagBuffer.Clear(); //increment the sentence counter sentenceIndex += 1; } } } catch { tagsets = null; return false; } //protect calling assemlby from having to check for nulls when returning a jagged-array for (var i = 0; i < tagsets.Length; i++ ) { if (tagsets[i] == null) tagsets[i] = new TagsetBase[] {new NullTagset()}; } return true; }