Ejemplo n.º 1
0
        static public void MapStringsToConstants(IAdapter adapter, List <Cell> cells, ColumnOrdering columnOrdering)
        {
            foreach (var cell in cells)
            {
                string text = cell.GetText(true);
                Logger.Debug(string.Format("column title: \"{0}\"[{1}]", text.ReplaceEolnWithSpace().CoalesceWhitespace(), cell.CellWidth));
                DeclarationField field;
                string           clean_text = AbsenceMarkers.Aggregate(text, (x, y) => x.Replace(y, "")).Trim();

                if (adapter.GetRowsCount() == cell.MergedRowsCount)
                {
                    continue;
                }

                if ((text == "" || clean_text.Length <= 1) && (text != "№"))
                {
                    // too short title, try to predict by values
                    field = ColumnPredictor.PredictEmptyColumnTitle(adapter, cell);
                    Logger.Debug("Predict: " + field.ToString());
                }
                else
                {
                    if (cell.TextAbove != null)
                    {
                        text = cell.TextAbove + " " + text;
                    }
                    field = HeaderHelpers.TryGetField(text.Replace('\n', ' '));
                    if ((field == DeclarationField.None) && clean_text.Length <= 4)
                    {
                        field = ColumnPredictor.PredictEmptyColumnTitle(adapter, cell);
                        Logger.Debug("Predict: " + field.ToString());
                    }
                    if (field == DeclarationField.None)
                    {
                        throw new SmartParserException(String.Format("Cannot recognize field \"{0}\"", text.Replace('\n', ' ')));
                    }
                }

                if (field == DeclarationField.None && !DataHelper.IsEmptyValue(text))
                {
                    throw new ColumnDetectorException(String.Format("Fail to detect column type row: {0} title:{1}", cell.Row, text));
                }
                if (ColumnPredictor.CalcPrecision)
                {
                    ColumnPredictor.PredictForPrecisionCheck(adapter, cell, field);
                }

                AddColumn(columnOrdering, field, cell);
                if (ColumnOrdering.SearchForFioColumnOnly)
                {
                    if (field == DeclarationField.NameAndOccupationOrRelativeType ||
                        field == DeclarationField.NameOrRelativeType)
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static DeclarationField GetField(string str)
        {
            var f = HeaderHelpers.TryGetField("", str);

            if (f == DeclarationField.None)
            {
                throw new Exception($"Could not determine column type for header {str}.");
            }
            return(f);
        }