diff --git a/service/src/main/inse/ranking/logic.py b/service/src/main/inse/ranking/logic.py index bbb57ec0..c85b201d 100644 --- a/service/src/main/inse/ranking/logic.py +++ b/service/src/main/inse/ranking/logic.py @@ -601,17 +601,21 @@ def _apply_scores(authors: List[RankedAuthor], weights: RankingWeights) -> None: citations_counts = [] companies_counts = [] news_counts = [] + patents_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) + patents_counts.append(author.patents_count) frame = pd.DataFrame({ "works": works_counts, "citations": citations_counts, "companies": companies_counts, "news": news_counts, + "patents": patents_counts, }) quantile_value = 0.95 @@ -620,17 +624,20 @@ def _apply_scores(authors: List[RankedAuthor], weights: RankingWeights) -> None: 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)) + frame["patents"] = frame["patents"].clip(upper=frame["patents"].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"]) + patents_normalized = _min_max_normalize(frame["patents"]) scores = ( works_normalized * weights.works + citations_normalized * weights.citation + companies_normalized * weights.companies + news_normalized * weights.news + + patents_normalized * weights.patents ) for index, author in enumerate(authors): @@ -715,6 +722,65 @@ async def _enrich_with_news_counts(authors: List[RankedAuthor]) -> None: author.news_count = counts.get(author.display_name, 0) +async def _get_patent_counts_by_author_ids(author_ids: List[str]) -> dict: + + short_to_full = {full.rsplit("/", 1)[-1]: full for full in author_ids} + + body = { + "size": 0, + "aggs": { + "matched": { + "nested": {"path": "matched_authors"}, + "aggs": { + "by_author": { + "terms": { + "field": "matched_authors.openalex_author_id", + "include": list(short_to_full.keys()), + "size": len(short_to_full), + "execution_hint": "map", + }, + "aggs": { + "patents": {"reverse_nested": {}}, + }, + }, + }, + }, + }, + } + + response: dict[str, Any] = await elasticsearch.search( + index="org_patents", + body=body, + cluster=ESCluster.INSE, + ) + + buckets = ( + response.get("aggregations", {}) + .get("matched", {}) + .get("by_author", {}) + .get("buckets", []) + ) + + counts = {} + for bucket in buckets: + full_id = short_to_full.get(bucket.get("key", "")) + if full_id: + counts[full_id] = bucket.get("patents", {}).get("doc_count", 0) + return counts + + +async def _enrich_with_patent_counts(authors: List[RankedAuthor]) -> None: + + if not authors: + return + + author_ids = [author.author_id for author in authors if author.author_id] + counts = await _get_patent_counts_by_author_ids(author_ids) + + for author in authors: + author.patents_count = counts.get(author.author_id, 0) + + async def _filter_author_ids_by_career_stage( author_ids: List[str], career_stages: List[CareerStage], @@ -830,6 +896,9 @@ async def rank_authors_by_keywords( if weights.news > 0: await _enrich_with_news_counts(ranked_authors) + if weights.patents > 0: + await _enrich_with_patent_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 1a65fff2..c89e39eb 100644 --- a/service/src/main/inse/ranking/model.py +++ b/service/src/main/inse/ranking/model.py @@ -87,6 +87,12 @@ class RankingWeights(BaseModel): le=1.0, description="Weight for the news/media-visibility metric (0.0 - 1.0).", ) + patents: float = Field( + default=0.0, + ge=0.0, + le=1.0, + description="Weight for the patents metric (0.0 - 1.0).", + ) class KeywordToAuthorRequest(BaseModel): @@ -213,6 +219,11 @@ class RankedAuthor(BaseModel): description="Number of news articles that mention the author by exact name " "(proxy for media visibility).", ) + patents_count: int = Field( + default=0, + description="Number of patents matched to the author (from the precomputed " + "org_patents index).", + ) 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 d77b6994..360507bc 100644 --- a/webapp/src/app/api/model/rankedAuthor.ts +++ b/webapp/src/app/api/model/rankedAuthor.ts @@ -33,6 +33,10 @@ export interface RankedAuthor { * Number of news articles that mention the author by exact name (proxy for media visibility). */ news_count?: number; + /** + * Number of patents matched to the author (from the precomputed org_patents index). + */ + patents_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 d1b0a39e..5f1d2df4 100644 --- a/webapp/src/app/api/model/rankingWeights.ts +++ b/webapp/src/app/api/model/rankingWeights.ts @@ -25,4 +25,8 @@ export interface RankingWeights { * Weight for the news/media-visibility metric (0.0 - 1.0). */ news?: number; + /** + * Weight for the patents metric (0.0 - 1.0). + */ + patents?: 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 6b43cde2..6b568150 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 @@ -88,6 +88,7 @@ export class KeywordsToPersonsResultsComponent implements OnInit { citations: 0.5, companies: 0.5, news: 0.5, + patents: 0.5, }; readonly weightOptions: WeightSliderOption[] = [ @@ -126,15 +127,15 @@ export class KeywordsToPersonsResultsComponent implements OnInit { defaultValue: 0.5, queryParamName: 'wNews', }, - /*{ + { key: 'patents', label: 'Patente', icon: 'workspace_premium', - description: 'Gewichtet Innovations- und Transferleistung.', + description: 'Gewichtet Innovations- und Transferleistung über Patente.', defaultValue: 0.5, queryParamName: 'wPatents', }, - { + /*{ key: 'projects', label: 'Projekte', icon: 'account_tree', @@ -147,12 +148,13 @@ export class KeywordsToPersonsResultsComponent implements OnInit { readonly currentWeights = signal({ raw: this.defaultWeights, normalized: { - works: 1 / 4, - citations: 1 / 4, - companies: 1 / 4, - news: 1 / 4, + works: 1 / 5, + citations: 1 / 5, + companies: 1 / 5, + news: 1 / 5, + patents: 1 / 5, }, - rawSum: 2.0, + rawSum: 2.5, normalizedSum: 1, valid: true, }); @@ -419,6 +421,7 @@ export class KeywordsToPersonsResultsComponent implements OnInit { citation: rawWeights['citations'], companies: rawWeights['companies'], news: rawWeights['news'], + patents: rawWeights['patents'], }, timespan: { from: this.timespanFrom(),