Пример #1
0
        public IActionResult Recommend(string id, [FromBody] PrcRecommendationRequest request)
        {
            if (request == null)
            {
                return(new StatusCodeResult(StatusCodes.Status400BadRequest));
            }
            // If Id is Alias, translate to Id
            if (GlobalStore.ServiceAliases.IsExist(id))
            {
                id = GlobalStore.ServiceAliases.Get(id);
            }

            if (!GlobalStore.ActivatedPrcs.IsExist(id))
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, string.Format(ServiceResources.ServiceNotExistsOrNotActivated, ServiceTypeEnum.Prc)));
            }

            if (!string.IsNullOrEmpty(request.TagId) && !GlobalStore.ActivatedPrcs.Get(id).PrcsSettings.Tags.Any(t => t.Id == request.TagId))
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, ServiceResources.TheGivenTagIsMissingFromThePRCService));
            }


            var globalStoreDataSet = GlobalStore.DataSets.Get(GlobalStore.ActivatedPrcs.Get(id).PrcsSettings.DataSetName);
            var dataSet            = globalStoreDataSet.DataSet;
            var analyzeQuery       = queryFactory.GetAnalyzeQuery(dataSet.Name);

            var tokens = analyzeQuery.Analyze(request.Text, 1).ToList();
            var text   = string.Join(" ", tokens);

            //tagId meghatározása
            var tagId = string.Empty;

            if (!string.IsNullOrEmpty(request.TagId))
            {
                tagId = request.TagId;
            }
            else
            {
                //ha nincs megadva tagId akkor kiszámoljuk a prc scorer-ekkel
                var allResults = new List <KeyValuePair <string, double> >();
                foreach (var scorerKvp in GlobalStore.ActivatedPrcs.Get(id).PrcScorers)
                {
                    var score = scorerKvp.Value.GetScore(text, 1.7, true);
                    allResults.Add(new KeyValuePair <string, double>(scorerKvp.Key, score));
                }
                var resultsList = allResults.Where(r => r.Value > 0).OrderByDescending(r => r.Value).ToList();
                if (resultsList.Count == 0)
                {
                    return(new OkObjectResult(new List <PrcRecommendationResult>()));
                }
                tagId = resultsList.First().Key;
            }

            var tagsToTest = new List <string>();

            if (request.Filter?.TagIdList?.Any() == true)
            {
                var existingTags = GlobalStore.ActivatedPrcs.Get(id).PrcsSettings.Tags.Select(t => t.Id).Intersect(request.Filter.TagIdList).ToList();
                if (existingTags.Count < request.Filter.TagIdList.Count)
                {
                    var missingTagIds = request.Filter.TagIdList.Except(existingTags).ToList();
                    return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest,
                                                             string.Format(ServiceResources.TheFollowingTagIdsNotExistInTheDataSet_0, string.Join(", ", missingTagIds))));
                }
                tagsToTest = request.Filter.TagIdList;
            }

            var globalSubset = GlobalStore.ActivatedPrcs.Get(id).PrcSubsets[tagId];

            if (globalSubset.WordsWithOccurences == null)
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status406NotAcceptable, ServiceResources.TheGivenTagHasNoWordsInDictionary));
            }

            var wordsInDic = globalSubset.WordsWithOccurences.Keys.Intersect(tokens).ToList();

            var baseSubset = new Cerebellum.Subset
            {
                AllWordsOccurencesSumInCorpus = globalSubset.AllWordsOccurencesSumInCorpus,
                AllWordsOccurencesSumInTag    = globalSubset.AllWordsOccurencesSumInTag,
                WordsWithOccurences           = wordsInDic.ToDictionary(w => w, w => globalSubset.WordsWithOccurences[w])
            };
            var baseDic = new Cerebellum.Dictionary.TwisterAlgorithm(baseSubset, true, false).GetDictionary();

            var globalScorer = GlobalStore.ActivatedPrcs.Get(id).PrcScorers[tagId];
            var baseScorer   = new Cerebellum.Scorer.PeSScorer(new Dictionary <int, Dictionary <string, double> > {
                { 1, baseDic }
            });

            var baseScore   = baseScorer.GetScore(text, 1.7);
            var globalScore = globalScorer.GetScore(text, 1.7);

            var results = new List <PrcRecommendationResult>();

            if (baseScore == 0 || globalScore == 0)
            {
                return(new OkObjectResult(results));
            }

            var filterQuery = request.Filter?.Query?.Trim();
            var query       = string.IsNullOrEmpty(filterQuery) ? string.Empty : $"({filterQuery}) AND ";

            // '+ 1' because we give score between 0 and 1 but in elasticsearch that means negative boost
            query = string.Format("{0}({1})", query, string.Join(" ", baseDic.Select(k => $"{k.Key}^{k.Value + 1}")));

            string shouldQuery = null;

            // weighting
            if (request.Weights?.Any() == true)
            {
                shouldQuery = string.Join(" ", request.Weights.Select(k => $"({k.Query})^{k.Value}"));
            }

            var fieldsForRecommendation = GlobalStore.ActivatedPrcs.Get(id).PrcsSettings.FieldsForRecommendation;

            var documentQuery    = queryFactory.GetDocumentQuery(dataSet.Name);
            var documentElastics = new List <DocumentElastic>();
            var scrollResult     = documentQuery
                                   .Filter(query,
                                           tagsToTest,
                                           dataSet.TagField,
                                           request.Count,
                                           null, false,
                                           fieldsForRecommendation,
                                           globalStoreDataSet.DocumentFields,
                                           DocumentService.GetFieldFilter(globalStoreDataSet, new List <string> {
                request.NeedDocumentInResult ? "*" : globalStoreDataSet.DataSet.IdField
            }),
                                           null, null, null,
                                           shouldQuery);

            documentElastics.AddRange(scrollResult.Items);

            var docIdsWithScore = new ConcurrentDictionary <string, double>(new Dictionary <string, double>());
            var wordQuery       = queryFactory.GetWordQuery(dataSet.Name);

            Func <string, bool> isAttachmentField = (field) => globalStoreDataSet.AttachmentFields.Any(attachmentField =>
                                                                                                       string.Equals(attachmentField, field, StringComparison.OrdinalIgnoreCase));

            Parallel.ForEach(documentElastics, parallelService.ParallelOptions(), docElastic =>
            {
                var fieldList = fieldsForRecommendation
                                .Select(field => isAttachmentField(field) ? $"{field}.content" : field)
                                .Select(DocumentQuery.MapDocumentObjectName)
                                .ToList();

                var wwo = wordQuery.GetWordsWithOccurences(new List <string> {
                    docElastic.Id
                }, fieldList, 1);
                var actualCleanedText = string.Join(" ", wwo.Select(w => string.Join(" ", Enumerable.Repeat(w.Key, w.Value.Tag))));

                var actualBaseScore = baseScorer.GetScore(actualCleanedText, 1.7);
                if (actualBaseScore == 0)
                {
                    return;
                }

                var actualGlobalScore = globalScorer.GetScore(actualCleanedText, 1.7);
                if (actualGlobalScore == 0)
                {
                    return;
                }

                var finalScore = (actualBaseScore / baseScore) / (actualGlobalScore / globalScore);
                docIdsWithScore.TryAdd(docElastic.Id, finalScore);
            });

            var resultDic = docIdsWithScore.OrderByDescending(rd => rd.Value).ToList();

            if (request.Count != 0 && resultDic.Count > request.Count)
            {
                resultDic = resultDic.Take(request.Count).ToList();
            }

            var docsDic = request.NeedDocumentInResult
                ? resultDic.Select(r => documentElastics.First(d => d.Id == r.Key)).ToDictionary(d => d.Id, d => d)
                : null;

            return(new OkObjectResult(resultDic.Select(kvp => new PrcRecommendationResult
            {
                DocumentId = kvp.Key,
                Score = kvp.Value,
                Document = request.NeedDocumentInResult ? docsDic[kvp.Key].DocumentObject : null
            })));
        }
Пример #2
0
        public IActionResult Keywords(string id, [FromBody] PrcKeywordsRequest request, [FromQuery] bool isStrict = false)
        {
            // If Id is Alias, translate to Id
            if (GlobalStore.ServiceAliases.IsExist(id))
            {
                id = GlobalStore.ServiceAliases.Get(id);
            }

            if (!GlobalStore.ActivatedPrcs.IsExist(id))
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, string.Format(ServiceResources.ServiceNotExistsOrNotActivated, ServiceTypeEnum.Prc)));
            }
            if (!string.IsNullOrEmpty(request.TagId) && !GlobalStore.ActivatedPrcs.Get(id).PrcsSettings.Tags.Any(t => t.Id == request.TagId))
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status400BadRequest, ServiceResources.TheGivenTagIsMissingFromThePRCService));
            }

            var dataSet      = GlobalStore.DataSets.Get(GlobalStore.ActivatedPrcs.Get(id).PrcsSettings.DataSetName).DataSet;
            var analyzeQuery = queryFactory.GetAnalyzeQuery(dataSet.Name);

            var tokens = analyzeQuery.Analyze(request.Text, 1).ToList();
            var text   = string.Join(" ", tokens);

            var tagId = string.Empty;

            if (!string.IsNullOrEmpty(request.TagId))
            {
                tagId = request.TagId;
            }
            else
            {
                //ha nincs megadva tagId akkor kiszámoljuk a prc scorer-ekkel
                var allResults = new List <KeyValuePair <string, double> >();
                foreach (var scorerKvp in GlobalStore.ActivatedPrcs.Get(id).PrcScorers)
                {
                    var score = scorerKvp.Value.GetScore(text, 1.7, true);
                    allResults.Add(new KeyValuePair <string, double>(scorerKvp.Key, score));
                }
                var resultsList = allResults.Where(r => r.Value > 0).OrderByDescending(r => r.Value).ToList();
                if (resultsList.Count == 0)
                {
                    return(new OkObjectResult(new List <PrcRecommendationResult>()));
                }
                tagId = resultsList.First().Key;
            }

            var globalSubset = GlobalStore.ActivatedPrcs.Get(id).PrcSubsets[tagId];

            if (globalSubset.WordsWithOccurences == null)
            {
                return(new HttpStatusCodeWithErrorResult(StatusCodes.Status406NotAcceptable, ServiceResources.TheGivenTagHasNoWordsInDictionary));
            }
            var wordsInDic = globalSubset.WordsWithOccurences.Keys.Intersect(tokens).ToList();

            var baseSubset = new Cerebellum.Subset
            {
                AllWordsOccurencesSumInCorpus = globalSubset.AllWordsOccurencesSumInCorpus,
                AllWordsOccurencesSumInTag    = globalSubset.AllWordsOccurencesSumInTag,
                WordsWithOccurences           = wordsInDic.ToDictionary(w => w, w => globalSubset.WordsWithOccurences[w])
            };
            var baseDic = new Cerebellum.Dictionary.TwisterAlgorithm(baseSubset, true, false).GetDictionary().OrderByDescending(d => d.Value).ToList();

            if (isStrict)
            {
                var avg = baseDic.Sum(d => d.Value) / baseDic.Count;
                baseDic.RemoveAll(d => d.Value < avg);
            }
            return(new OkObjectResult(baseDic.Select(d => new PrcKeywordsResult {
                Word = d.Key, Score = d.Value
            })));
        }