Assets/Watson/Scripts/Services/DeepQA/DataModels.cs
/// <summary>
/// Helper function to extract all tables from the formatted answer
/// </summary>
/// <returns>An array of all tables found.</returns>
Helper function to extract all tables from the formatted answer
Helper function to extract all tables from the formatted answer
public Table[] ExtractTables(string answer)
        {
            if (!string.IsNullOrEmpty(formattedText))
            {
                HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(formattedText);
                if (doc.DocumentNode == null)
                    return null;
                var table_nodes = doc.DocumentNode.SelectNodes("//table");
                if (table_nodes == null)
                    return null;
                List<Table> tables = new List<Table>();
                foreach (var table in table_nodes)
                {
                    var row_nodes = table.SelectNodes("*/tr");
                    if (row_nodes == null)
                        continue;
                    List<Row> rows = new List<Row>();
                    foreach (var row in row_nodes)
                    {
                        var cell_nodes = row.SelectNodes("*/th|td");
                        if (cell_nodes == null)
                            continue;
                        List<Cell> cells = new List<Cell>();
                        foreach (var cell in cell_nodes)
                        {
                            string text = CleanInnerText(cell.InnerText);
                            int colspan = 1;
                            if (cell.Attributes.Contains("colspan"))
                                colspan = int.Parse(cell.Attributes["colspan"].Value);
                            bool bHighlighted = false;
                            if (text == answer)
                                bHighlighted = true;
                            cells.Add(new Cell() { Value = text, ColSpan = colspan, Highlighted = bHighlighted });
                            for (int i = 1; i < colspan; ++i)
                                cells.Add(null);      
                        }
                        rows.Add(new Row() { columns = cells.ToArray() });
                    }
                    tables.Add(new Table() { rows = rows.ToArray() });
                }
                return tables.ToArray();
            }
            return null;
        }
public Table [ ] ExtractTables ( string answer ) { if ( ! string . IsNullOrEmpty ( formattedText ) ) { HtmlAgilityPack . HtmlDocument doc = new HtmlAgilityPack . HtmlDocument ( ) ; doc . LoadHtml ( formattedText ) ; if ( doc . DocumentNode == null ) return null ; var table_nodes = doc . DocumentNode . SelectNodes ( " " ) ; if ( table_nodes == null ) return null ; List < Table > tables = new List < Table > ( ) ; foreach ( var table in table_nodes ) { var row_nodes = table . SelectNodes ( " " ) ; if ( row_nodes == null ) continue ; List < Row > rows = new List < Row > ( ) ; foreach ( var row in row_nodes ) { var cell_nodes = row . SelectNodes ( " " ) ; if ( cell_nodes == null ) continue ; List < Cell > cells = new List < Cell > ( ) ; foreach ( var cell in cell_nodes ) { string text = CleanInnerText ( cell . InnerText ) ; int colspan = 1 ; if ( cell . Attributes . Contains ( " " ) ) colspan = int . Parse ( cell . Attributes [ " " ] . Value ) ; bool bHighlighted = false ; if ( text == answer ) bHighlighted = true ; cells . Add ( new Cell ( ) { Value = text , ColSpan = colspan , Highlighted = bHighlighted } ) ; for ( int i = 1 ; i < colspan ; ++ i ) cells . Add ( null ) ; } rows . Add ( new Row ( ) { columns = cells . ToArray ( ) } ) ; } tables . Add ( new Table ( ) { rows = rows . ToArray ( ) } ) ; } return tables . ToArray ( ) ; } return null ; }
Helper function to extract all tables from the formatted answer
Helper function to extract all tables from the formatted answer
// add empty cells for the spans
An array of all tables found.
An array of all tables found .
answer
string