<commit_before>﻿using Marimo.LinqToDejizo;
using Microsoft.AspNetCore.WebUtilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Marimo.LinqToDejizo
{
    public class DejizoSource
    {
        public IQueryable<DejizoItem> EJdict { get; set; } = new Query<DejizoItem>(new DejizoProvider());
    }

    public class DejizoItem
    {
        public string HeaderText { get; set; }
        public string BodyText { get; set; }
    }

    public class DejizoProvider : QueryProvider
    {
        public override object Execute(Expression expression)
        {
            SearchDicItemResult result = null;
            Task.Run(async () =>
            {
                var client = new HttpClient();
                
                var uri =
                    QueryHelpers.AddQueryString(
                        "http://public.dejizo.jp/NetDicV09.asmx/SearchDicItemLite",
                        new Dictionary<string, string>
                        {
                            {"Dic", "EJdict"},
                            {"Word", (string)((ConstantExpression)((MethodCallExpression)((LambdaExpression)((UnaryExpression)((MethodCallExpression)((MethodCallExpression)expression).Arguments[0]).Arguments[1]).Operand).Body).Arguments[0]).Value},
                            {"Scope", "HEADWORD"},
                            {"Match", "STARTWITH"},
                            {"Merge", "AND"},
                            {"Prof", "XHTML"},
                            {"PageSize", "20"},
                            {"PageIndex", "0"}
                        });

                var response = await client.GetAsync(uri);

                var stream = await response.Content.ReadAsStreamAsync();

                var serializer = new DataContractSerializer(typeof(SearchDicItemResult));

                result = serializer.ReadObject(stream) as SearchDicItemResult;
            }).Wait();<commit_msg>リファクタリング<commit_after>﻿using Marimo.LinqToDejizo;
using Microsoft.AspNetCore.WebUtilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Marimo.LinqToDejizo
{
    public class DejizoSource
    {
        public IQueryable<DejizoItem> EJdict { get; set; } = new Query<DejizoItem>(new DejizoProvider());
    }

    public class DejizoItem
    {
        public string HeaderText { get; set; }
        public string BodyText { get; set; }
    }

    public class DejizoProvider : QueryProvider
    {
        public override object Execute(Expression expression)
        {
            SearchDicItemResult result = null;
            Task.Run(async () =>
            {
                var client = new HttpClient();

                string word = null;
                switch(expression)
                {
                    case MethodCallExpression m:
                        switch(m.Arguments[0])
                        {
                            case MethodCallExpression mm:
                                switch(mm.Arguments[1])
                                {
                                    case UnaryExpression u:
                                        switch(u.Operand)
                                        {
                                            case LambdaExpression l:
                                                switch(l.Body)
                                                {
                                                    case MethodCallExpression mmm:
                                                        
                                                        switch (mmm.Arguments[0])
                                                        {
                                                            case ConstantExpression c:
                                                                switch (c.Value)
                                                                {
                                                                    case string s:
                                                                        word = s;
                                                                        break;
                                                                }

                                                                break;
                                                        }
                                                        break;
                                                }
                                                break;
                                        }
                                        break;

                                }
                                break;
                        }
                        break;
                }

                var uri =
                    QueryHelpers.AddQueryString(
                        "http://public.dejizo.jp/NetDicV09.asmx/SearchDicItemLite",
                        new Dictionary<string, string>
                        {
                            {"Dic", "EJdict"},
                            {"Word", word},
                            {"Scope", "HEADWORD"},
                            {"Match", "STARTWITH"},
                            {"Merge", "AND"},
                            {"Prof", "XHTML"},
                            {"PageSize", "20"},
                            {"PageIndex", "0"}
                        });

                var response = await client.GetAsync(uri);

                var stream = await response.Content.ReadAsStreamAsync();

                var serializer = new DataContractSerializer(typeof(SearchDicItemResult));

                result = serializer.ReadObject(stream) as SearchDicItemResult;
            }).Wait();
{'fraction_non_alphanumeric': 0.07782101167315175, 'fraction_numerical': 0.0025940337224383916, 'mean_word_length': 0.9368916797488226, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1754105', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1212, 'n_words': 259}