diff --git a/service/src/main/inse/ranking/logic.py b/service/src/main/inse/ranking/logic.py index 0a66256b..bbb57ec0 100644 --- a/service/src/main/inse/ranking/logic.py +++ b/service/src/main/inse/ranking/logic.py @@ -593,34 +593,44 @@ def _min_max_normalize(values: pd.Series) -> pd.Series: def _apply_scores(authors: List[RankedAuthor], weights: RankingWeights) -> None: - """Normalizes works, citations and company connections to [0, 1] and sets each author's weighted score.""" + """Normalizes works, citations, company connections and news to [0, 1] and sets each author's weighted score.""" if not authors: return works_counts = [] citations_counts = [] companies_counts = [] + news_counts = [] for author in authors: works_counts.append(author.works_count) citations_counts.append(author.citations_count) companies_counts.append(author.companies_count) + news_counts.append(author.news_count) frame = pd.DataFrame({ "works": works_counts, "citations": citations_counts, "companies": companies_counts, + "news": news_counts, }) - frame["works"] = frame["works"].clip(upper=frame["works"].quantile(0.95)) - frame["citations"] = frame["citations"].clip(upper=frame["citations"].quantile(0.95)) - frame["companies"] = frame["companies"].clip(upper=frame["companies"].quantile(0.95)) + quantile_value = 0.95 + + frame["works"] = frame["works"].clip(upper=frame["works"].quantile(quantile_value)) + frame["citations"] = frame["citations"].clip(upper=frame["citations"].quantile(quantile_value)) + frame["companies"] = frame["companies"].clip(upper=frame["companies"].quantile(quantile_value)) + frame["news"] = frame["news"].clip(upper=frame["news"].quantile(quantile_value)) + works_normalized = _min_max_normalize(frame["works"]) citations_normalized = _min_max_normalize(frame["citations"]) companies_normalized = _min_max_normalize(frame["companies"]) + news_normalized = _min_max_normalize(frame["news"]) + scores = ( works_normalized * weights.works + citations_normalized * weights.citation + companies_normalized * weights.companies + + news_normalized * weights.news ) for index, author in enumerate(authors): @@ -651,6 +661,60 @@ async def _enrich_with_company_counts( for author in authors: author.companies_count = counts.get(author.display_name, 0) + +async def _get_news_counts_by_names(names: List[str]) -> dict: + """Returns a mapping ``display_name -> news count`` for the given author names. + """ + unique_names = sorted({name for name in names if name}) + + if not unique_names: + return {} + + body = { + "size": 0, + "query": {"terms": {"authors.keyword": unique_names}}, + "aggs": { + "by_author": { + "terms": { + "field": "authors.keyword", + "include": unique_names, + "size": len(unique_names), + "execution_hint": "map", + }, + }, + }, + } + + response: dict[str, Any] = await elasticsearch.search( + index="aktuell_postproc", + body=body, + cluster=ESCluster.INSE, + ) + + buckets = ( + response.get("aggregations", {}) + .get("by_author", {}) + .get("buckets", []) + ) + + return {bucket.get("key", ""): bucket.get("doc_count", 0) for bucket in buckets} + + +async def _enrich_with_news_counts(authors: List[RankedAuthor]) -> None: + """Sets each author's news_count from a single bulk news query. + Matching is by exact display name, so authors without a name match keep 0. + """ + + if not authors: + return + + names = [author.display_name for author in authors if author.display_name] + counts = await _get_news_counts_by_names(names) + + for author in authors: + author.news_count = counts.get(author.display_name, 0) + + async def _filter_author_ids_by_career_stage( author_ids: List[str], career_stages: List[CareerStage], @@ -760,11 +824,12 @@ async def rank_authors_by_keywords( for author_bucket in author_buckets: ranked_authors.append(_bucket_to_ranked_author(author_bucket)) - # Company connections are only fetched when they actually influence the score, - # avoiding an extra Handelsregister query for the common works/citations case. if weights.companies > 0: await _enrich_with_company_counts(ranked_authors, company_status) + if weights.news > 0: + await _enrich_with_news_counts(ranked_authors) + _apply_scores(ranked_authors, weights) # Sort by the weighted score, highest first. diff --git a/service/src/main/inse/ranking/model.py b/service/src/main/inse/ranking/model.py index 10efaf38..1a65fff2 100644 --- a/service/src/main/inse/ranking/model.py +++ b/service/src/main/inse/ranking/model.py @@ -81,6 +81,12 @@ class RankingWeights(BaseModel): le=1.0, description="Weight for the company-foundations metric (0.0 - 1.0).", ) + news: float = Field( + default=0.0, + ge=0.0, + le=1.0, + description="Weight for the news/media-visibility metric (0.0 - 1.0).", + ) class KeywordToAuthorRequest(BaseModel): @@ -202,6 +208,11 @@ class RankedAuthor(BaseModel): description="Number of Handelsregister company connections matched by the " "author's name (proxy for company foundations).", ) + news_count: int = Field( + default=0, + description="Number of news articles that mention the author by exact name " + "(proxy for media visibility).", + ) score: float = Field( default=0.0, description="Normalized weighted score used for ranking.", diff --git a/webapp/src/app/api/model/rankedAuthor.ts b/webapp/src/app/api/model/rankedAuthor.ts index db044599..d77b6994 100644 --- a/webapp/src/app/api/model/rankedAuthor.ts +++ b/webapp/src/app/api/model/rankedAuthor.ts @@ -29,6 +29,10 @@ export interface RankedAuthor { * Number of Handelsregister company connections matched by the author\'s name (proxy for company foundations). */ companies_count?: number; + /** + * Number of news articles that mention the author by exact name (proxy for media visibility). + */ + news_count?: number; /** * Normalized weighted score used for ranking. */ diff --git a/webapp/src/app/api/model/rankingWeights.ts b/webapp/src/app/api/model/rankingWeights.ts index 13ff0db8..d1b0a39e 100644 --- a/webapp/src/app/api/model/rankingWeights.ts +++ b/webapp/src/app/api/model/rankingWeights.ts @@ -21,4 +21,8 @@ export interface RankingWeights { * Weight for the company-foundations metric (0.0 - 1.0). */ companies?: number; + /** + * Weight for the news/media-visibility metric (0.0 - 1.0). + */ + news?: number; } diff --git a/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts b/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts index a5f8bc14..6b43cde2 100644 --- a/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts +++ b/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts @@ -87,6 +87,7 @@ export class KeywordsToPersonsResultsComponent implements OnInit { works: 0.5, citations: 0.5, companies: 0.5, + news: 0.5, }; readonly weightOptions: WeightSliderOption[] = [ @@ -116,6 +117,15 @@ export class KeywordsToPersonsResultsComponent implements OnInit { defaultValue: 0.5, queryParamName: 'wCompanies', }, + { + key: 'news', + label: 'Medien-Sichtbarkeit', + icon: 'visibility', + description: + 'Gewichtet die öffentliche Sichtbarkeit über Nennungen in Nachrichtenartikeln.', + defaultValue: 0.5, + queryParamName: 'wNews', + }, /*{ key: 'patents', label: 'Patente', @@ -131,21 +141,18 @@ export class KeywordsToPersonsResultsComponent implements OnInit { description: 'Gewichtet Beteiligung an relevanten Forschungsprojekten.', defaultValue: 0.5, queryParamName: 'wProjects', - }, - { - key: 'mediaVisibility', - label: 'Medien-Sichtbarkeit', - icon: 'visibility', - description: 'Gewichtet öffentliche und mediale Sichtbarkeit.', - defaultValue: 0.5, - queryParamName: 'wMedia', }, */ ]; readonly currentWeights = signal({ raw: this.defaultWeights, - normalized: { works: 1 / 3, citations: 1 / 3, companies: 1 / 3 }, - rawSum: 1.5, + normalized: { + works: 1 / 4, + citations: 1 / 4, + companies: 1 / 4, + news: 1 / 4, + }, + rawSum: 2.0, normalizedSum: 1, valid: true, }); @@ -411,6 +418,7 @@ export class KeywordsToPersonsResultsComponent implements OnInit { works: rawWeights['works'], citation: rawWeights['citations'], companies: rawWeights['companies'], + news: rawWeights['news'], }, timespan: { from: this.timespanFrom(),