<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Interop.Excel;
using VSTOContrib.Core.Extensions;
using VSTOContrib.Core.RibbonFactory;
using VSTOContrib.Core.RibbonFactory.Interfaces;

namespace VSTOContrib.Excel.RibbonFactory
{
    /// <summary>
    /// 
    /// </summary>
    public class ExcelViewProvider : IViewProvider<ExcelRibbonType>
    {
        readonly Dictionary<Workbook, List<Window>> workbooks;
        Application excelApplication;
        Window singleWindow;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExcelViewProvider"/> class.
        /// </summary>
        /// <param name="excelApplication">The Excel application.</param>
        public ExcelViewProvider(Application excelApplication)
        {
            workbooks = new Dictionary<Workbook, List<Window>>();
            this.excelApplication = excelApplication;
            var monitor = new WorkbookClosedMonitor(excelApplication);
            monitor.WorkbookClosed += MonitorWorkbookClosed;
        }

        void MonitorWorkbookClosed(object sender, WorkbookClosedEventArgs e)
        {
            var handler = ViewClosed;
            if (handler == null) return;

            var windows = workbooks[e.Workbook];

            foreach (var window in windows)
            {
                handler(this, new ViewClosedEventArgs(window, e.Workbook));
                if (!IsMdi())
                    window.ReleaseComObject();
            }
            workbooks.Remove(e.Workbook);
        }

        /// <summary>
        /// Initialises this instance.
        /// </summary>
        public void Initialise()
        {
            ((AppEvents_Event)excelApplication).NewWorkbook += OnNewWorkbook;
        }

        void OnNewWorkbook(Workbook wb)
        {
            var handler = NewView;
            if (handler == null) return;
            if (!workbooks.ContainsKey(wb))
                workbooks.Add(wb, new List<Window>());<commit_msg>Excel close error "The given key was not present in the dictionary."<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Office.Interop.Excel;
using VSTOContrib.Core.Extensions;
using VSTOContrib.Core.RibbonFactory;
using VSTOContrib.Core.RibbonFactory.Interfaces;

namespace VSTOContrib.Excel.RibbonFactory
{
    /// <summary>
    /// 
    /// </summary>
    public class ExcelViewProvider : IViewProvider<ExcelRibbonType>
    {
        readonly Dictionary<Workbook, List<Window>> workbooks;
        Application excelApplication;
        Window singleWindow;

        /// <summary>
        /// Initializes a new instance of the <see cref="ExcelViewProvider"/> class.
        /// </summary>
        /// <param name="excelApplication">The Excel application.</param>
        public ExcelViewProvider(Application excelApplication)
        {
            workbooks = new Dictionary<Workbook, List<Window>>();
            this.excelApplication = excelApplication;
            var monitor = new WorkbookClosedMonitor(excelApplication);
            monitor.WorkbookClosed += MonitorWorkbookClosed;
        }

        void MonitorWorkbookClosed(object sender, WorkbookClosedEventArgs e)
        {
            var handler = ViewClosed;
            if (handler == null) return;

            if (workbooks.ContainsKey(e.Workbook))
            {
                var windows = workbooks[e.Workbook];

                foreach (var window in windows)
                {
                    handler(this, new ViewClosedEventArgs(window, e.Workbook));
                    if (!IsMdi())
                        window.ReleaseComObject();
                }
                workbooks.Remove(e.Workbook);
            }
        }

        /// <summary>
        /// Initialises this instance.
        /// </summary>
        public void Initialise()
        {
            ((AppEvents_Event)excelApplication).NewWorkbook += OnNewWorkbook;
        }

        void OnNewWorkbook(Workbook wb)
        {
            var handler = NewView;
            if (handler == null) return;
            if (!workbooks.ContainsKey(wb))
                workbooks.Add(wb, new List<Window>());
{'fraction_non_alphanumeric': 0.09938330170777988, 'fraction_numerical': 0.0, 'mean_word_length': 2.0359971202303817, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '933378', 'n_tokens_mistral': 1115, 'n_tokens_neox': 1060, 'n_words': 238}