TryCalculateWordMetrics ( IGraph graph, CalculateGraphMetricsContext calculateGraphMetricsContext, String statusColumnName, out GraphMetricColumn [] wordMetricColumns ) { Debug.Assert(graph != null); Debug.Assert(calculateGraphMetricsContext != null); Debug.Assert(!String.IsNullOrEmpty(statusColumnName)); // Use the WordMetricCalculator2() class to calculate word metrics for // all groups. // // This is somewhat wasteful, because we don't actually need all the // word metrics for all groups. A future version might refactor common // code out of WordMetricCalculator2() that can be called by that class // and this one. GraphMetricUserSettings oGraphMetricUserSettings = calculateGraphMetricsContext.GraphMetricUserSettings; WordMetricUserSettings oWordMetricUserSettings = oGraphMetricUserSettings.WordMetricUserSettings; GraphMetrics eOriginalGraphMetricsToCalculate = oGraphMetricUserSettings.GraphMetricsToCalculate; Boolean bOriginalTextColumnIsOnEdgeWorksheet = oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet; String sOriginalTextColumnName = oWordMetricUserSettings.TextColumnName; Boolean bOriginalCountByGroup = oWordMetricUserSettings.CountByGroup; oGraphMetricUserSettings.GraphMetricsToCalculate = GraphMetrics.Words; oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet = true; oWordMetricUserSettings.TextColumnName = statusColumnName; oWordMetricUserSettings.CountByGroup = true; try { return((new WordMetricCalculator2()).TryCalculateGraphMetrics( graph, calculateGraphMetricsContext, out wordMetricColumns)); } finally { oGraphMetricUserSettings.GraphMetricsToCalculate = eOriginalGraphMetricsToCalculate; oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet = bOriginalTextColumnIsOnEdgeWorksheet; oWordMetricUserSettings.TextColumnName = sOriginalTextColumnName; oWordMetricUserSettings.CountByGroup = bOriginalCountByGroup; } }
ConvertFrom ( ITypeDescriptorContext context, CultureInfo culture, Object value ) { Debug.Assert(value != null); Debug.Assert(value is String); AssertValid(); PersistableStringDictionary oDictionary = PersistableStringDictionary.FromString((String)value); WordMetricUserSettings oWordMetricUserSettings = new WordMetricUserSettings(); Boolean bValue; String sValue; if (oDictionary.TryGetValue( WordMetricUserSettings.TextColumnIsOnEdgeWorksheetKey, out bValue)) { oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet = bValue; } if (oDictionary.TryGetValue( WordMetricUserSettings.TextColumnNameKey, out sValue)) { oWordMetricUserSettings.TextColumnName = sValue; } if (oDictionary.TryGetValue( WordMetricUserSettings.CountByGroupKey, out bValue)) { oWordMetricUserSettings.CountByGroup = bValue; } if (oDictionary.TryGetValue( WordMetricUserSettings.SkipSingleTermsKey, out bValue)) { oWordMetricUserSettings.SkipSingleTerms = bValue; } oWordMetricUserSettings.WordsToSkip = oDictionary.TryGetValue(WordMetricUserSettings.WordsToSkipKey, out sValue) ? sValue : String.Empty; return(oWordMetricUserSettings); }
EditWordMetricUserSettings() { AssertValid(); WordMetricUserSettings oWordMetricUserSettings = m_oGraphMetricUserSettings.WordMetricUserSettings; if ((new WordMetricUserSettingsDialog( oWordMetricUserSettings, m_oWorkbook)).ShowDialog() == DialogResult.OK) { m_oGraphMetricUserSettings.WordMetricUserSettings = oWordMetricUserSettings; } }
ConvertTo ( ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType ) { Debug.Assert(value != null); Debug.Assert(value is WordMetricUserSettings); Debug.Assert(destinationType == typeof(String)); AssertValid(); // Note: // // Earlier user settings type converter classes used a string of // ordered, tab-delimited values to persist the user settings. That // was a brittle solution. Newer classes, including this one, use an // unordered dictionary. WordMetricUserSettings oWordMetricUserSettings = (WordMetricUserSettings)value; PersistableStringDictionary oDictionary = new PersistableStringDictionary(); oDictionary.Add( WordMetricUserSettings.TextColumnIsOnEdgeWorksheetKey, oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet); oDictionary.Add(WordMetricUserSettings.TextColumnNameKey, oWordMetricUserSettings.TextColumnName); oDictionary.Add(WordMetricUserSettings.CountByGroupKey, oWordMetricUserSettings.CountByGroup); oDictionary.Add(WordMetricUserSettings.SkipSingleTermsKey, oWordMetricUserSettings.SkipSingleTerms); oDictionary.Add(WordMetricUserSettings.WordsToSkipKey, oWordMetricUserSettings.WordsToSkip); return(oDictionary.ToString()); }
//************************************************************************* // Constructor: WordMetricUserSettingsDialog() // /// <summary> /// Initializes a new instance of the <see /// cref="WordMetricUserSettingsDialog" /> class. /// </summary> /// /// <param name="wordMetricUserSettings"> /// The object being edited. /// </param> /// /// <param name="workbook"> /// Workbook containing the graph contents. /// </param> //************************************************************************* public WordMetricUserSettingsDialog ( WordMetricUserSettings wordMetricUserSettings, Microsoft.Office.Interop.Excel.Workbook workbook ) { Debug.Assert(wordMetricUserSettings != null); Debug.Assert(workbook != null); m_oWordMetricUserSettings = wordMetricUserSettings; InitializeComponent(); // Populate the column name ComboBoxes with column names from the // workbook. ListObject oTable; if ( ExcelTableUtil.TryGetTable(workbook, WorksheetNames.Edges, TableNames.Edges, out oTable) ) { cbxEdgeColumnName.PopulateWithTableColumnNames(oTable); } if ( ExcelTableUtil.TryGetTable(workbook, WorksheetNames.Vertices, TableNames.Vertices, out oTable) ) { cbxVertexColumnName.PopulateWithTableColumnNames(oTable); } // Instantiate an object that saves and retrieves the user settings for // this dialog. Note that the object automatically saves the settings // when the form closes. m_oWordMetricUserSettingsDialogUserSettings = new WordMetricUserSettingsDialogUserSettings(this); DoDataExchange(false); AssertValid(); }
//************************************************************************* // Constructor: WordMetricUserSettingsDialog() // /// <summary> /// Initializes a new instance of the <see /// cref="WordMetricUserSettingsDialog" /> class. /// </summary> /// /// <param name="wordMetricUserSettings"> /// The object being edited. /// </param> /// /// <param name="workbook"> /// Workbook containing the graph contents. /// </param> //************************************************************************* public WordMetricUserSettingsDialog ( WordMetricUserSettings wordMetricUserSettings, Microsoft.Office.Interop.Excel.Workbook workbook ) { Debug.Assert(wordMetricUserSettings != null); Debug.Assert(workbook != null); m_oWordMetricUserSettings = wordMetricUserSettings; InitializeComponent(); // Populate the column name ComboBoxes with column names from the // workbook. ListObject oTable; if (ExcelTableUtil.TryGetTable(workbook, WorksheetNames.Edges, TableNames.Edges, out oTable)) { cbxEdgeColumnName.PopulateWithTableColumnNames(oTable); } if (ExcelTableUtil.TryGetTable(workbook, WorksheetNames.Vertices, TableNames.Vertices, out oTable)) { cbxVertexColumnName.PopulateWithTableColumnNames(oTable); } // Instantiate an object that saves and retrieves the user settings for // this dialog. Note that the object automatically saves the settings // when the form closes. m_oWordMetricUserSettingsDialogUserSettings = new WordMetricUserSettingsDialogUserSettings(this); DoDataExchange(false); AssertValid(); }
AddCountedWordsToValueLists ( IEnumerable<CountedWord> oCountedWords, WordMetricUserSettings oWordMetricUserSettings, String sGroupName, List<GraphMetricValueOrdered> oWordWordValues, List<GraphMetricValueOrdered> oWordCountValues, List<GraphMetricValueOrdered> oWordSalienceValues, List<GraphMetricValueOrdered> oWordGroupNameValues ) { Debug.Assert(oCountedWords != null); Debug.Assert(oWordMetricUserSettings != null); Debug.Assert( !String.IsNullOrEmpty(sGroupName) ); Debug.Assert(oWordWordValues != null); Debug.Assert(oWordCountValues != null); Debug.Assert(oWordSalienceValues != null); Debug.Assert(oWordGroupNameValues != null); foreach (CountedWord oCountedWord in oCountedWords) { if ( AddCountedWordToValueLists(oCountedWord, oWordMetricUserSettings, oWordWordValues, oWordCountValues, oWordSalienceValues) ) { oWordGroupNameValues.Add( new GraphMetricValueOrdered(sGroupName) ); } } }
TryCountTermsNoGroups ( IGraph oGraph, WordMetricUserSettings oWordMetricUserSettings, WordCounter oWordCounter, WordPairCounter oWordPairCounter, HashSet<String> oUniqueImportedIDs, out GraphMetricColumn [] oGraphMetricColumns ) { Debug.Assert(oGraph != null); Debug.Assert(oWordMetricUserSettings != null); Debug.Assert(oWordCounter != null); Debug.Assert(oWordPairCounter != null); AssertValid(); Boolean bTextColumnIsOnEdgeWorksheet = oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet; System.Collections.IEnumerable oEdgesOrVertices = bTextColumnIsOnEdgeWorksheet ? (System.Collections.IEnumerable)oGraph.Edges : (System.Collections.IEnumerable)oGraph.Vertices; // Count the terms in each of the column's cells. foreach ( IMetadataProvider oEdgeOrVertex in EnumerateEdgesOrVertices( oEdgesOrVertices, bTextColumnIsOnEdgeWorksheet, oGraph, oUniqueImportedIDs) ) { CountTermsInEdgeOrVertex(oEdgeOrVertex, oWordMetricUserSettings.TextColumnName, oWordCounter, oWordPairCounter); } oWordCounter.CalculateSalienceOfCountedTerms(); oWordPairCounter.CalculateSalienceOfCountedTerms(); oWordPairCounter.CalculateMutualInformationOfCountedTerms(); // Transfer the words and word pairs to graph metric value lists. List<GraphMetricValueOrdered> oWordWordValues, oWordCountValues, oWordSalienceValues; List<GraphMetricValueOrdered> oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues; CreateGraphMetricValueLists( out oWordWordValues, out oWordCountValues, out oWordSalienceValues, out oWordPairWord1Values, out oWordPairWord2Values, out oWordPairCountValues, out oWordPairSalienceValues, out oWordPairMutualInformationValues ); foreach (CountedWord oCountedWord in oWordCounter.CountedTerms) { AddCountedWordToValueLists(oCountedWord, oWordMetricUserSettings, oWordWordValues, oWordCountValues, oWordSalienceValues); } foreach (CountedWordPair oCountedWordPair in oWordPairCounter.CountedTerms) { AddCountedWordPairToValueLists(oCountedWordPair, oWordMetricUserSettings, oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues); } oGraphMetricColumns = CreateGraphMetricColumns( oWordWordValues, oWordCountValues, oWordSalienceValues, null, oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues, null ); return (true); }
TryCountVertexTermsByGroup ( IGraph oGraph, WordMetricUserSettings oWordMetricUserSettings, WordCounter oWordCounter, WordPairCounter oWordPairCounter, HashSet<String> oUniqueImportedIDs, out GraphMetricColumn [] oGraphMetricColumns ) { Debug.Assert(oGraph != null); Debug.Assert(oWordMetricUserSettings != null); Debug.Assert(oWordCounter != null); Debug.Assert(oWordPairCounter != null); AssertValid(); List<GraphMetricValueOrdered> oWordWordValues, oWordCountValues, oWordSalienceValues; List<GraphMetricValueOrdered> oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues; CreateGraphMetricValueLists( out oWordWordValues, out oWordCountValues, out oWordSalienceValues, out oWordPairWord1Values, out oWordPairWord2Values, out oWordPairCountValues, out oWordPairSalienceValues, out oWordPairMutualInformationValues ); List<GraphMetricValueOrdered> oWordGroupNameValues = new List<GraphMetricValueOrdered>(); List<GraphMetricValueOrdered> oWordPairGroupNameValues = new List<GraphMetricValueOrdered>(); // Get a list of the graph's groups, adding a dummy group for the // entire graph and another to contain any non-grouped vertices. foreach ( GroupInfo oGroup in EnumerateGroupsForCountingVertexTerms(oGraph) ) { // Count the terms in this group. oWordCounter.Clear(); oWordPairCounter.Clear(); foreach ( IVertex oVertex in EnumerateEdgesOrVertices( oGroup.Vertices, false, oGraph, oUniqueImportedIDs) ) { CountTermsInEdgeOrVertex(oVertex, oWordMetricUserSettings.TextColumnName, oWordCounter, oWordPairCounter); } oWordCounter.CalculateSalienceOfCountedTerms(); oWordPairCounter.CalculateSalienceOfCountedTerms(); oWordPairCounter.CalculateMutualInformationOfCountedTerms(); // Transfer the words and word pairs to the graph metric value // lists. AddCountedWordsToValueLists(oWordCounter.CountedTerms, oWordMetricUserSettings, oGroup.Name, oWordWordValues, oWordCountValues, oWordSalienceValues, oWordGroupNameValues); AddCountedWordPairsToValueLists(oWordPairCounter.CountedTerms, oWordMetricUserSettings, oGroup.Name, oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues, oWordPairGroupNameValues); } oGraphMetricColumns = CreateGraphMetricColumns( oWordWordValues, oWordCountValues, oWordSalienceValues, oWordGroupNameValues, oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues, oWordPairGroupNameValues ); return (true); }
TryCountEdgeTermsByGroup ( IGraph oGraph, WordMetricUserSettings oWordMetricUserSettings, WordCounter oWordCounter, WordPairCounter oWordPairCounter, HashSet<String> oUniqueImportedIDs, out GraphMetricColumn [] oGraphMetricColumns ) { Debug.Assert(oGraph != null); Debug.Assert(oWordMetricUserSettings != null); Debug.Assert(oWordCounter != null); Debug.Assert(oWordPairCounter != null); AssertValid(); List<GraphMetricValueOrdered> oWordWordValues, oWordCountValues, oWordSalienceValues; List<GraphMetricValueOrdered> oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues; CreateGraphMetricValueLists( out oWordWordValues, out oWordCountValues, out oWordSalienceValues, out oWordPairWord1Values, out oWordPairWord2Values, out oWordPairCountValues, out oWordPairSalienceValues, out oWordPairMutualInformationValues ); List<GraphMetricValueOrdered> oWordGroupNameValues = new List<GraphMetricValueOrdered>(); List<GraphMetricValueOrdered> oWordPairGroupNameValues = new List<GraphMetricValueOrdered>(); // Get the edges in each of the graph's groups. Include a "dummy" // group that contains the edges that aren't contained in any real // groups. foreach ( GroupEdgeInfo oGroupEdgeInfo in GroupEdgeSorter.SortGroupEdges(oGraph, Int32.MaxValue, true, true) ) { // Count the terms in this group. oWordCounter.Clear(); oWordPairCounter.Clear(); foreach ( IEdge oEdge in EnumerateEdgesOrVertices( oGroupEdgeInfo.Edges, true, oGraph, oUniqueImportedIDs) ) { CountTermsInEdgeOrVertex(oEdge, oWordMetricUserSettings.TextColumnName, oWordCounter, oWordPairCounter); } oWordCounter.CalculateSalienceOfCountedTerms(); oWordPairCounter.CalculateSalienceOfCountedTerms(); oWordPairCounter.CalculateMutualInformationOfCountedTerms(); // Transfer the words and word pairs to the graph metric value // lists. String sGroupName = oGroupEdgeInfo.GroupName; AddCountedWordsToValueLists( oWordCounter.CountedTerms, oWordMetricUserSettings, sGroupName, oWordWordValues, oWordCountValues, oWordSalienceValues, oWordGroupNameValues); AddCountedWordPairsToValueLists( oWordPairCounter.CountedTerms, oWordMetricUserSettings, sGroupName, oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues, oWordPairGroupNameValues); if ( sGroupName == GroupEdgeSorter.DummyGroupNameForEntireGraph && oUniqueImportedIDs != null ) { // This is the dummy group that stores all the edges in the // graph. Note that SortGroupEdges() guarantees that this is // the first group, so the imported IDs need to be cleared only // once within this loop. oUniqueImportedIDs.Clear(); } } oGraphMetricColumns = CreateGraphMetricColumns( oWordWordValues, oWordCountValues, oWordSalienceValues, oWordGroupNameValues, oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues, oWordPairGroupNameValues ); return (true); }
AddCountedWordPairToValueLists ( CountedWordPair oCountedWordPair, WordMetricUserSettings oWordMetricUserSettings, List<GraphMetricValueOrdered> oWordPairWord1Values, List<GraphMetricValueOrdered> oWordPairWord2Values, List<GraphMetricValueOrdered> oWordPairCountValues, List<GraphMetricValueOrdered> oWordPairSalienceValues, List<GraphMetricValueOrdered> oWordPairMutualInformationValues ) { Debug.Assert(oCountedWordPair != null); Debug.Assert(oWordMetricUserSettings != null); Debug.Assert(oWordPairWord1Values != null); Debug.Assert(oWordPairWord2Values != null); Debug.Assert(oWordPairCountValues != null); Debug.Assert(oWordPairSalienceValues != null); Debug.Assert(oWordPairMutualInformationValues != null); AssertValid(); if ( oCountedWordPair.Count == 1 && oWordMetricUserSettings.SkipSingleTerms ) { return (false); } oWordPairWord1Values.Add( new GraphMetricValueOrdered( ExcelUtil.ForceCellText(oCountedWordPair.Word1) ) ); oWordPairWord2Values.Add( new GraphMetricValueOrdered( ExcelUtil.ForceCellText(oCountedWordPair.Word2) ) ); oWordPairCountValues.Add( new GraphMetricValueOrdered(oCountedWordPair.Count) ); oWordPairSalienceValues.Add( new GraphMetricValueOrdered(oCountedWordPair.Salience) ); oWordPairMutualInformationValues.Add( new GraphMetricValueOrdered(oCountedWordPair.MutualInformation) ); return (true); }
AddCountedWordToValueLists ( CountedWord oCountedWord, WordMetricUserSettings oWordMetricUserSettings, List<GraphMetricValueOrdered> oWordWordValues, List<GraphMetricValueOrdered> oWordCountValues, List<GraphMetricValueOrdered> oWordSalienceValues ) { Debug.Assert(oCountedWord != null); Debug.Assert(oWordMetricUserSettings != null); Debug.Assert(oWordWordValues != null); Debug.Assert(oWordCountValues != null); Debug.Assert(oWordSalienceValues != null); AssertValid(); if ( oCountedWord.Count == 1 && oWordMetricUserSettings.SkipSingleTerms ) { return (false); } oWordWordValues.Add( new GraphMetricValueOrdered( ExcelUtil.ForceCellText(oCountedWord.Word) ) ); oWordCountValues.Add( new GraphMetricValueOrdered(oCountedWord.Count) ); oWordSalienceValues.Add( new GraphMetricValueOrdered(oCountedWord.Salience) ); return (true); }
AddCountedWordPairsToValueLists ( IEnumerable<CountedWordPair> oCountedWordPairs, WordMetricUserSettings oWordMetricUserSettings, String sGroupName, List<GraphMetricValueOrdered> oWordPairWord1Values, List<GraphMetricValueOrdered> oWordPairWord2Values, List<GraphMetricValueOrdered> oWordPairCountValues, List<GraphMetricValueOrdered> oWordPairSalienceValues, List<GraphMetricValueOrdered> oWordPairMutualInformationValues, List<GraphMetricValueOrdered> oWordPairGroupNameValues ) { Debug.Assert(oCountedWordPairs != null); Debug.Assert(oWordMetricUserSettings != null); Debug.Assert( !String.IsNullOrEmpty(sGroupName) ); Debug.Assert(oWordPairWord1Values != null); Debug.Assert(oWordPairWord2Values != null); Debug.Assert(oWordPairCountValues != null); Debug.Assert(oWordPairSalienceValues != null); Debug.Assert(oWordPairMutualInformationValues != null); Debug.Assert(oWordPairGroupNameValues != null); foreach (CountedWordPair oCountedWordPair in oCountedWordPairs) { if ( AddCountedWordPairToValueLists(oCountedWordPair, oWordMetricUserSettings, oWordPairWord1Values, oWordPairWord2Values, oWordPairCountValues, oWordPairSalienceValues, oWordPairMutualInformationValues) ) { oWordPairGroupNameValues.Add( new GraphMetricValueOrdered(sGroupName) ); } } }
ConvertFrom ( ITypeDescriptorContext context, CultureInfo culture, Object value ) { Debug.Assert(value != null); Debug.Assert(value is String); AssertValid(); PersistableStringDictionary oDictionary = PersistableStringDictionary.FromString( (String)value ); WordMetricUserSettings oWordMetricUserSettings = new WordMetricUserSettings(); Boolean bValue; String sValue; if ( oDictionary.TryGetValue( WordMetricUserSettings.TextColumnIsOnEdgeWorksheetKey, out bValue) ) { oWordMetricUserSettings.TextColumnIsOnEdgeWorksheet = bValue; } if ( oDictionary.TryGetValue( WordMetricUserSettings.TextColumnNameKey, out sValue) ) { oWordMetricUserSettings.TextColumnName = sValue; } if ( oDictionary.TryGetValue( WordMetricUserSettings.CountByGroupKey, out bValue) ) { oWordMetricUserSettings.CountByGroup = bValue; } if ( oDictionary.TryGetValue( WordMetricUserSettings.SkipSingleTermsKey, out bValue) ) { oWordMetricUserSettings.SkipSingleTerms = bValue; } oWordMetricUserSettings.WordsToSkip = oDictionary.TryGetValue(WordMetricUserSettings.WordsToSkipKey, out sValue) ? sValue : String.Empty; return (oWordMetricUserSettings); }