Exemplo n.º 1
0
        private void ParseItem(string text)
        {
            // Start off optimistic with the results. Change the properties as necessary.
            var result = new ImportResult
            {
                Status = ImportStatus.Succeeded,
                Text   = text.Trim()
            };

            // Add the result to the results list.
            Results.Add(result);

            // See if there might be at least a token to parse, i.e., "t:text", the bare minimum.
            if (!text.ToLowerInvariant().Contains($"{Key.Text}{Delimiter.KeyValue}"))
            {
                // "t:" was not found, does it contain "e:", the alt?
                if (!text.ToLowerInvariant().Contains($"{Key.Text}{Delimiter.KeyValue}"))
                {
                    result.Status = ImportStatus.Error;
                    result.AddMessage($"{MessageType.ItemError} There were no recognizable tokens found.");
                    result.AddMessage($" The mandatory token ('t:' or 'e:') was not found.");

                    return;
                }

                // If we get here, the "e:" was used to specify the text
            }

            // Extract token of data from the text. Return if an error was found.
            var tokens = Tokenize(text, ref result);

            if (result.Status == ImportStatus.Error)
            {
                return;
            }

            // Now we can bother to create a stem since the text is passed error checks. Anything
            // found beyond this point is a warning, except for a missing value for the text key.
            Stem stem = new Stem();

            foreach (var token in tokens)
            {
                switch (token.Key)
                {
                case Key.Id:
                    if (Guid.TryParse(token.Value, out Guid guid))
                    {
                        result.Id = stem.Id = guid;
                    }
                    break;

                case Key.RootId:
                    if (Guid.TryParse(token.Value, out Guid rid))
                    {
                        stem.RootId = rid;
                    }
                    break;

                case Key.BaseId:
                    if (Guid.TryParse(token.Value, out Guid bid))
                    {
                        stem.BaseId = bid;
                    }
                    break;

                case Key.Text:
                case Key.TextAlt:
                    stem.Form = token.Value;
                    break;

                case Key.Category:
                    stem.Category = token.Value.ParseTag();
                    break;

                case Key.Root:

                    // Put the Id of this stem in the lookingForRoot
                    _lookingForRoot.Add(stem.Id, token.Value);
                    break;

                case Key.Base:

                    // Put the Id of this stem in the lookingForBase
                    _lookingForBase.Add(stem.Id, token.Value);
                    break;

                case Key.Requisite:
                    stem.RequisiteValues |= Requisites.ToValue(token.Value);
                    break;

                case Key.Suggestion:
                    stem.SuggestionValues |= Suggestion.ToValue(token.Value);
                    break;

                case Key.Compounding:
                    stem.CompoundingValues |= Compounding.ToValue(token.Value);
                    break;

                case Key.Affixes:
                case Key.AffixesAlt:
                    if (string.IsNullOrWhiteSpace(token.Value))
                    {
                        break;
                    }

                    stem.Affixes = new List <string>(token
                                                     .Value
                                                     .Split(new string[] { Delimiter.Item }, StringSplitOptions.RemoveEmptyEntries)
                                                     .Select(t => t.Trim())
                                                     .Distinct());

                    foreach (var affix in stem.Affixes)
                    {
                        if (!AffixExists(affix))
                        {
                            result.Status = ImportStatus.Warning;
                            result.AddMessage($"{MessageType.TokenWarning} Affix not found ('{affix}').");
                        }
                    }

                    break;

                case Key.Sense:

                    // Replace any space, ' ', with a period, '.'.
                    stem.Sense = token.Value?.Replace(Delimiter.Space, Delimiter.Morpheme)
                                 .Unescape()
                                 .Replace(Delimiter.Pipe, Delimiter.SemiColon);
                    break;

                case Key.Comments:
                    stem.Comments = token.Value?.Unescape()
                                    .Replace(Delimiter.Pipe, Delimiter.SemiColon);
                    break;

                default:
                    result.Status = ImportStatus.Warning;
                    result.AddMessage($"{MessageType.TokenWarning} Unrecognized key ('{token}').");
                    break;
                }
            }

            // Set the ID for the results for tracking. This is late as the ID might have been parsed
            // from the text.
            if (result.Id == Guid.Empty)
            {
                result.Id = stem.Id;
            }

            // Duplicate check
            if (_referenceStems.Where(s => s.Form == stem.Form & s.Category == stem.Category).Count() > 0)
            {
                result.Status = ImportStatus.Warning;
                result.AddMessage($"{MessageType.TokenWarning} Possible duplicate ('{stem.Form}').");

                DebugHelper.Warning(result.Message);
            }

            DebugHelper.Info($"Adding stem (text: '{stem.Form}')...");

            _referenceStems.Add(stem);
        }