Enable wildcard search in terms list
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
namespace Rsdo.Concordancer.Core.Extensions;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static bool IsWildcardSearch(this string value)
|
||||
{
|
||||
return !string.IsNullOrEmpty(value) && (value.Contains('*') || value.Contains('?'));
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenSearch.Client;
|
||||
using Rsdo.Concordancer.Core.Extensions;
|
||||
|
||||
namespace Rsdo.Concordancer.Infrastructure.Extensions;
|
||||
|
||||
@@ -30,4 +32,36 @@ public static class ElasticQueryExtensions
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public static QueryContainer ToQuery(this string value, string field)
|
||||
{
|
||||
if (value.IsWildcardSearch())
|
||||
{
|
||||
return new WildcardQuery()
|
||||
{
|
||||
Field = field,
|
||||
Value = value,
|
||||
};
|
||||
}
|
||||
|
||||
return new TermQuery()
|
||||
{
|
||||
Field = field,
|
||||
Value = value,
|
||||
};
|
||||
}
|
||||
|
||||
public static QueryContainer ToQuery(this List<string> values, string field)
|
||||
{
|
||||
if (values.Any(v => v.IsWildcardSearch()))
|
||||
{
|
||||
return values.Select(value => value.ToQuery(field)).ToList().ToBooleanOrQuery();
|
||||
}
|
||||
|
||||
return new TermsQuery()
|
||||
{
|
||||
Field = field,
|
||||
Terms = values,
|
||||
};
|
||||
}
|
||||
}
|
||||
+2
-12
@@ -35,22 +35,12 @@ public class TermListQueryBuilder : IQueryBuilder<TermListQuery>
|
||||
|
||||
if (!string.IsNullOrEmpty(wordQuery.Form))
|
||||
{
|
||||
queries.Add(
|
||||
new TermQuery()
|
||||
{
|
||||
Field = $"{tokenField}.formLower",
|
||||
Value = wordQuery.Form.ToLower(),
|
||||
});
|
||||
queries.Add(wordQuery.Form.ToLower().ToQuery($"{tokenField}.formLower"));
|
||||
}
|
||||
|
||||
if (!wordQuery.Lemmas.IsNullOrEmpty())
|
||||
{
|
||||
queries.Add(
|
||||
new TermsQuery()
|
||||
{
|
||||
Field = $"{tokenField}.lemma",
|
||||
Terms = wordQuery.Lemmas,
|
||||
});
|
||||
queries.Add(wordQuery.Lemmas.ToQuery($"{tokenField}.lemma"));
|
||||
}
|
||||
|
||||
return queries.Count == 0 ? new MatchNoneQuery() : queries.ToBooleanAndQuery();
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using FluentValidation;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Rsdo.Concordancer.Core.Interfaces;
|
||||
using Rsdo.Concordancer.Core.Search.Queries.Concordances;
|
||||
using Rsdo.Concordancer.Core.Search.Queries.TermLists;
|
||||
using Rsdo.Concordancer.ServiceModel.Requests.Concordances;
|
||||
using Rsdo.Concordancer.ServiceModel.Requests.TermLists;
|
||||
using Rsdo.Concordancer.ServiceModel.Types;
|
||||
using Rsdo.Concordancer.Services.Framework;
|
||||
using Rsdo.Concordancer.Services.Framework.BulkLoaders;
|
||||
@@ -15,6 +19,8 @@ using Rsdo.Concordancer.Services.Framework.Decorators;
|
||||
using Rsdo.Concordancer.Services.Search.Aggregations;
|
||||
using Rsdo.Concordancer.Services.Search.AlternateSearches;
|
||||
using Rsdo.Concordancer.Services.Search.QueryFactories;
|
||||
using Rsdo.Concordancer.Services.Search.QueryFactories.Concordances;
|
||||
using Rsdo.Concordancer.Services.Search.QueryFactories.TermLists;
|
||||
using Rsdo.Concordancer.Services.Services.InputQueryParser;
|
||||
using Rsdo.Concordancer.Services.Services.LemmatizationService;
|
||||
using Rsdo.Concordancer.Services.Services.ParagraphService;
|
||||
@@ -83,7 +89,8 @@ public class ServicesModule : Module
|
||||
builder.RegisterType<DefaultTokenizerService>().Keyed<ITokenizerService>(TokenizerType.Default).SingleInstance();
|
||||
builder.RegisterType<InputQueryParser>().As<IInputQueryParser>().SingleInstance();
|
||||
builder.RegisterType<ParagraphService>().As<IParagraphService>().InstancePerLifetimeScope();
|
||||
builder.RegisterType<LemmatizationService>().As<ILemmatizationService>().InstancePerLifetimeScope();
|
||||
builder.RegisterType<SimpleLemmatizationService>().Keyed<ILemmatizationService>(LemmatizationType.Simple).InstancePerLifetimeScope();
|
||||
builder.RegisterType<WildcardLemmatizationService>().Keyed<ILemmatizationService>(LemmatizationType.Wildcard).InstancePerLifetimeScope();
|
||||
builder.RegisterType<PartOfSpeechService>().As<IPartOfSpeechService>().InstancePerLifetimeScope();
|
||||
}
|
||||
|
||||
@@ -103,7 +110,33 @@ public class ServicesModule : Module
|
||||
private void RegisterSearch(ContainerBuilder builder)
|
||||
{
|
||||
// Query factories
|
||||
builder.RegisterAssemblyTypes(ServicesAssembly).AsClosedTypesOf(typeof(IQueryFactory<,>)).AsImplementedInterfaces().InstancePerLifetimeScope();
|
||||
var wildcardLemmatizationServiceParameter = new ResolvedParameter(
|
||||
(p, ctx) => p.ParameterType == typeof(ILemmatizationService),
|
||||
(p, ctx) => ctx.ResolveKeyed<ILemmatizationService>(LemmatizationType.Wildcard));
|
||||
var simpleLemmatizationServiceParameter = new ResolvedParameter(
|
||||
(p, ctx) => p.ParameterType == typeof(ILemmatizationService),
|
||||
(p, ctx) => ctx.ResolveKeyed<ILemmatizationService>(LemmatizationType.Simple));
|
||||
|
||||
builder.RegisterType<ExportConcordancesQueryFactory>()
|
||||
.As<IQueryFactory<ExportConcordances, ConcordancesQuery>>()
|
||||
.WithParameter(simpleLemmatizationServiceParameter)
|
||||
.InstancePerLifetimeScope();
|
||||
builder.RegisterType<SearchConcordancesQueryFactory>()
|
||||
.As<IQueryFactory<SearchConcordances, ConcordancesQuery>>()
|
||||
.WithParameter(simpleLemmatizationServiceParameter)
|
||||
.InstancePerLifetimeScope();
|
||||
builder.RegisterType<ConcordanceDetailsQueryFactory>()
|
||||
.As<IQueryFactory<ConcordanceDetails, ConcordancesQuery>>()
|
||||
.WithParameter(simpleLemmatizationServiceParameter)
|
||||
.InstancePerLifetimeScope();
|
||||
builder.RegisterType<ExportTermListQueryFactory>()
|
||||
.As<IQueryFactory<ExportTermList, TermListQuery>>()
|
||||
.WithParameter(wildcardLemmatizationServiceParameter)
|
||||
.InstancePerLifetimeScope();
|
||||
builder.RegisterType<SearchTermListQueryFactory>()
|
||||
.As<IQueryFactory<SearchTermList, TermListQuery>>()
|
||||
.WithParameter(wildcardLemmatizationServiceParameter)
|
||||
.InstancePerLifetimeScope();
|
||||
|
||||
// Aggregations
|
||||
builder.RegisterType<AggregationProviderFactory>().As<IAggregationProviderFactory>().InstancePerLifetimeScope();
|
||||
@@ -112,6 +145,7 @@ public class ServicesModule : Module
|
||||
// Alternate searches
|
||||
builder.RegisterType<LemmasAlternateSearchProvider>()
|
||||
.As<IAlternateSearchProvider<SearchConcordances, SearchConcordancesResponse>>()
|
||||
.WithParameter(simpleLemmatizationServiceParameter)
|
||||
.InstancePerLifetimeScope();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Autofac.Features.Indexed;
|
||||
using Rsdo.Concordancer.Core.Extensions;
|
||||
using Rsdo.Concordancer.ServiceModel.Requests.Concordances;
|
||||
using Rsdo.Concordancer.ServiceModel.Types;
|
||||
using Rsdo.Concordancer.Services.Services.TokenizerService;
|
||||
@@ -21,6 +22,12 @@ public class InputQueryParser : IInputQueryParser
|
||||
|
||||
public async Task<IEnumerable<(TokenType type, string form)>> Tokenize(string query)
|
||||
{
|
||||
if (query.IsWildcardSearch())
|
||||
{
|
||||
// Classla tokenizer doesn't support wildcards, so use default tokenizer
|
||||
return await tokenizers[TokenizerType.Default].Tokenize(query);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return await tokenizers[TokenizerType.Classla].Tokenize(query);
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Rsdo.Concordancer.Services.Services.LemmatizationService;
|
||||
|
||||
public enum LemmatizationType
|
||||
{
|
||||
Simple,
|
||||
Wildcard,
|
||||
}
|
||||
+4
-3
@@ -7,17 +7,18 @@ using Rsdo.Concordancer.Services.Framework.DbContext;
|
||||
|
||||
namespace Rsdo.Concordancer.Services.Services.LemmatizationService;
|
||||
|
||||
public class LemmatizationService : ILemmatizationService
|
||||
public class SimpleLemmatizationService : ILemmatizationService
|
||||
{
|
||||
private readonly MasterDbContext dbContext;
|
||||
|
||||
public LemmatizationService(MasterDbContext dbContext)
|
||||
public SimpleLemmatizationService(MasterDbContext dbContext)
|
||||
{
|
||||
this.dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<List<string>> GetLemmas(string form)
|
||||
public virtual async Task<List<string>> GetLemmas(string form)
|
||||
{
|
||||
// Retrieving lemmas without support for wildcard search
|
||||
var lemmas = await dbContext.LemmaFormPair.Where(f => f.Form.ToLower() == form.ToLower()).Select(f => f.Lemma).Distinct().ToListAsync();
|
||||
if (lemmas.IsNullOrEmpty())
|
||||
{
|
||||
Reference in New Issue
Block a user