<commit_before>﻿namespace ExcelX.AddIn.Module.SearchReplace
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Office.Interop.Excel;
    using Core = Microsoft.Office.Core;

    /// <summary>
    /// 検索処理を行う実態クラス。
    /// </summary>
    public class SearchEngine
    {
        /// <summary>
        /// 検索範囲
        /// </summary>
        private SearchScope scope;

        /// <summary>
        /// 検索対象
        /// </summary>
        private SearchTarget target;

        /// <summary>
        /// 検索条件
        /// </summary>
        private SearchCondition condition;

        /// <summary>
        /// 検索範囲、検索対象、検索条件を指定して検索を行います。
        /// </summary>
        /// <param name="scope">検索範囲</param>
        /// <param name="target">検索対象</param>
        /// <param name="condition">検索条件</param>
        /// <returns>検索結果</returns>
        public List<SearchResult> Execute(SearchScope scope, SearchTarget target, SearchCondition condition)
        {
            var result = new List<SearchResult>();

            this.scope = scope;
            this.target = target;
            this.condition = condition;

            foreach (var range in this.scope.Ranges)
            {
                if (this.target.IncludeFormula ||
                    this.target.IncludeValue ||
                    this.target.IncludeComment)
                {
                    var found = this.FindCell(range);
                    if (found.Count > 0)
                    {
                        result.AddRange(found);
                    }
                }

                if (this.target.IncludeShape)
                {
                    var found = this.FindShape(range);
                    if (found.Count > 0)
                    {
                        result.AddRange(found);
                    }
                }

                if (this.target.IncludeChart)
                {
                    var found = this.FindChart(range);
                    if (found.Count > 0)
                    {
                        result.AddRange(found);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 検索範囲、検索対象、検索条件を指定して非同期に検索を行います。
        /// </summary>
        /// <param name="scope">検索範囲</param>
        /// <param name="target">検索対象</param>
        /// <param name="condition">検索条件</param>
        /// <returns>検索結果</returns>
        public Task<List<SearchResult>> ExecuteAsync(SearchScope scope, SearchTarget target, SearchCondition condition)
        {
            return Task.Factory.StartNew(() =>
            {
                return this.Execute(scope, target, condition);
            });
        }

        /// <summary>
        /// 指定された範囲のセル情報から 式 、 値 、 コメント を探します。
        /// </summary>
        /// <param name="range">検索範囲</param>
        /// <returns>検索結果</returns>
        private List<SearchResult> FindCell(Range range)
        {
            var result = new List<SearchResult>();
<commit_msg>Add search result sort function.<commit_after>﻿namespace ExcelX.AddIn.Module.SearchReplace
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Office.Interop.Excel;
    using Core = Microsoft.Office.Core;

    /// <summary>
    /// 検索処理を行う実態クラス。
    /// </summary>
    public class SearchEngine
    {
        /// <summary>
        /// 検索範囲
        /// </summary>
        private SearchScope scope;

        /// <summary>
        /// 検索対象
        /// </summary>
        private SearchTarget target;

        /// <summary>
        /// 検索条件
        /// </summary>
        private SearchCondition condition;

        /// <summary>
        /// 検索範囲、検索対象、検索条件を指定して検索を行います。
        /// </summary>
        /// <param name="scope">検索範囲</param>
        /// <param name="target">検索対象</param>
        /// <param name="condition">検索条件</param>
        /// <returns>検索結果</returns>
        public SortableBindingList<SearchResult> Execute(SearchScope scope, SearchTarget target, SearchCondition condition)
        {
            var result = new List<SearchResult>();

            this.scope = scope;
            this.target = target;
            this.condition = condition;

            foreach (var range in this.scope.Ranges)
            {
                if (this.target.IncludeFormula ||
                    this.target.IncludeValue ||
                    this.target.IncludeComment)
                {
                    var found = this.FindCell(range);
                    if (found.Count > 0)
                    {
                        result.AddRange(found);
                    }
                }

                if (this.target.IncludeShape)
                {
                    var found = this.FindShape(range);
                    if (found.Count > 0)
                    {
                        result.AddRange(found);
                    }
                }

                if (this.target.IncludeChart)
                {
                    var found = this.FindChart(range);
                    if (found.Count > 0)
                    {
                        result.AddRange(found);
                    }
                }
            }

            return new SortableBindingList<SearchResult>(result);
        }

        /// <summary>
        /// 検索範囲、検索対象、検索条件を指定して非同期に検索を行います。
        /// </summary>
        /// <param name="scope">検索範囲</param>
        /// <param name="target">検索対象</param>
        /// <param name="condition">検索条件</param>
        /// <returns>検索結果</returns>
        public Task<SortableBindingList<SearchResult>> ExecuteAsync(SearchScope scope, SearchTarget target, SearchCondition condition)
        {
            return Task.Factory.StartNew(() =>
            {
                return this.Execute(scope, target, condition);
            });
        }

        /// <summary>
        /// 指定された範囲のセル情報から 式 、 値 、 コメント を探します。
        /// </summary>
        /// <param name="range">検索範囲</param>
        /// <returns>検索結果</returns>
        private List<SearchResult> FindCell(Range range)
        {
            var result = new List<SearchResult>();

{'fraction_non_alphanumeric': 0.12138084632516703, 'fraction_numerical': 0.0009545020680878142, 'mean_word_length': 1.2461593426223652, 'pattern_counts': {'":': 0, '<': 84, '<?xml version=': 0, '>': 92, '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': '6975084', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1897, 'n_words': 303}