Ejemplo n.º 1
0
        //public static IServiceCollection RegisterLexicala(this IServiceCollection services, IConfiguration configuration)
        //{
        //    var config = configuration.GetSection("Lexicala").Get<LexicalaConfig>();
        //    return RegisterLexicala(services, config);
        //}

        //public static IServiceCollection RegisterLexicala(this IServiceCollection services, LexicalaConfig config)
        //{
        //    services.AddHttpClient<ILexicalaClient, LexicalaClient>(client =>
        //    {
        //        client.BaseAddress = LexicalaConfig.BaseAddress;
        //        client.DefaultRequestHeaders.Authorization = config.CreateAuthenticationHeader();
        //    });

        //    services.AddMemoryCache();
        //    services.AddTransient<ILexicalaSearchParser, LexicalaSearchParser>();

        //    return services;
        //}
        public static IContainer RegisterLexixala(LexicalaConfig config)
        {
            var builder = new ContainerBuilder();

            var httpClient = config.CreateHttpClient();

            builder.RegisterInstance(httpClient)
            .SingleInstance();

            var lexicalaClient = new LexicalaClient(httpClient);

            builder.RegisterInstance(lexicalaClient)
            .As <ILexicalaClient>();

            // register options since MemoryCache depends on it
            builder.RegisterGeneric(typeof(OptionsManager <>)).As(typeof(IOptions <>)).SingleInstance();
            builder.RegisterGeneric(typeof(OptionsManager <>)).As(typeof(IOptionsSnapshot <>)).InstancePerLifetimeScope();
            builder.RegisterGeneric(typeof(OptionsMonitor <>)).As(typeof(IOptionsMonitor <>)).SingleInstance();
            builder.RegisterGeneric(typeof(OptionsFactory <>)).As(typeof(IOptionsFactory <>));
            builder.RegisterGeneric(typeof(OptionsCache <>)).As(typeof(IOptionsMonitorCache <>)).SingleInstance();

            builder.RegisterType <MemoryCache>()
            .As <IMemoryCache>()
            .SingleInstance();

            builder.RegisterType <LexicalaSearchParser>()
            .As <ILexicalaSearchParser>()
            .SingleInstance();

            return(builder.Build());
        }
Ejemplo n.º 2
0
        public static IServiceCollection RegisterLexicala(this IServiceCollection services, LexicalaConfig config)
        {
            services.AddHttpClient <ILexicalaClient, LexicalaClient>(client =>
            {
                client.BaseAddress = LexicalaConfig.BaseAddress;
                client.DefaultRequestHeaders.Authorization = config.CreateAuthenticationHeader();
            });

            services.AddMemoryCache();
            services.AddSingleton <ILexicalaSearchParser, LexicalaSearchParser>();

            return(services);
        }
Ejemplo n.º 3
0
        public static async Task Main(string[] args)
        {
            var config         = new LexicalaConfig(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"]);
            var container      = DependencyRegistration.RegisterLexixala(config);
            var lexicalaClient = container.Resolve <ILexicalaClient>();
            var memCache       = container.Resolve <IMemoryCache>();

            string input = string.Empty;

            while (input != ConsoleKey.Q.ToString())
            {
                Console.WriteLine("Enter search query: searchterm sourcelang targetlang (eg: estar es en). Q to exit");
                input = Console.ReadLine();
                if (input == null)
                {
                    continue;
                }

                var tokens = input.Split(' ');

                if (tokens.Length != 3)
                {
                    continue;
                }

                var searchTerm = tokens[0];
                var srcLang    = tokens[1];
                var tgtLang    = tokens[2];
                if (srcLang.Length != 2 || tgtLang.Length != 2)
                {
                    Console.WriteLine("Language must be 2 characters");
                    continue;
                }

                if (!memCache.TryGetValue("languages", out Resource languages))
                {
                    var response = await lexicalaClient.LanguagesAsync();

                    languages = response.Resources.Global;
                    memCache.Set("languages", languages);
                }

                if (!languages.SourceLanguages.Contains(srcLang))
                {
                    Console.WriteLine("Source language not available");
                    continue;
                }

                if (!languages.TargetLanguages.Contains(tgtLang))
                {
                    Console.WriteLine("Target language not available");
                    continue;
                }

                try
                {
                    var searchResponse = await lexicalaClient.BasicSearchAsync(searchTerm, srcLang);

                    bool hasTranslation = false;
                    foreach (var result in searchResponse.Results)
                    {
                        var entry = await lexicalaClient.GetEntryAsync(result.Id);

                        foreach (var sense in entry.Senses)
                        {
                            Console.WriteLine(sense.Definition);
                            if (sense.Translations.ContainsKey(tgtLang))
                            {
                                hasTranslation = true;
                                var senseTranslation = sense.Translations[tgtLang];
                                if (senseTranslation.Language != null)
                                {
                                    Console.WriteLine(senseTranslation.Language?.Text);
                                }
                                else
                                {
                                    Console.WriteLine(string.Join(", ", senseTranslation.CommonLanguageObjectArray.Select(l => l.Text)));
                                }
                            }
                        }
                    }

                    if (!searchResponse.Results.Any())
                    {
                        Console.WriteLine("No results");
                    }
                    else if (!hasTranslation)
                    {
                        Console.WriteLine("No translations available for target language " + tgtLang);
                    }

                    Console.WriteLine("Api calls remaining: " + searchResponse.Metadata.RateLimits.DailyLimitRemaining);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }