created utils for research wordcloud to enable compatibility to the shared keywords-wordcloud component
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
import { topicHierarchyColor } from '../../resolvers/utils/topic-colors';
|
||||
import {
|
||||
getTopicLegendFieldId,
|
||||
getTopicLegendSubfieldId,
|
||||
TopicLegendTopic,
|
||||
} from '../../resolvers/utils/topic-legend';
|
||||
import type { WordCloudWordData } from '../../shared/ui/keyword-wordcloud/keyword-wordcloud.component';
|
||||
import type {
|
||||
TopicTimelineElement,
|
||||
TopicTimelineTopic,
|
||||
} from './components/research-timeline/research-timeline.component';
|
||||
|
||||
export interface WordCloudWord extends WordCloudWordData {
|
||||
topicId: string;
|
||||
rawTopicId: string;
|
||||
fieldLegendId: string;
|
||||
subfieldLegendId: string;
|
||||
color: string;
|
||||
fontSize: number;
|
||||
fontWeight: number;
|
||||
}
|
||||
|
||||
export interface ResearchWordcloud {
|
||||
words: WordCloudWord[];
|
||||
legendTopics: TopicLegendTopic[];
|
||||
}
|
||||
|
||||
const displayedWordLimit = 30;
|
||||
const minimumFontSize = 12;
|
||||
const maximumFontSize = 32;
|
||||
|
||||
interface AggregatedTopic {
|
||||
name: string;
|
||||
count: number;
|
||||
legendTopic: TopicLegendTopic;
|
||||
}
|
||||
|
||||
export function buildResearchWordcloud(
|
||||
elements: readonly TopicTimelineElement[],
|
||||
startYear: number | undefined,
|
||||
endYear: number | undefined,
|
||||
): ResearchWordcloud {
|
||||
const yearsInRange = elements.filter((element) =>
|
||||
isWithinYearRange(element.year, startYear, endYear),
|
||||
);
|
||||
|
||||
const aggregated = aggregateTopics(yearsInRange);
|
||||
|
||||
if (aggregated.size === 0) {
|
||||
return { words: [], legendTopics: [] };
|
||||
}
|
||||
|
||||
const displayed = Array.from(aggregated.values())
|
||||
.sort(
|
||||
(left, right) =>
|
||||
right.count - left.count || left.name.localeCompare(right.name),
|
||||
)
|
||||
.slice(0, displayedWordLimit);
|
||||
|
||||
const intensity = buildIntensityScale(displayed);
|
||||
|
||||
const words = displayed.map((topic): WordCloudWord => {
|
||||
const legendTopic = topic.legendTopic;
|
||||
const scale = intensity(topic.count);
|
||||
|
||||
return {
|
||||
name: topic.name,
|
||||
topicId: legendTopic.id ?? '',
|
||||
rawTopicId: stripOpenAlexId(legendTopic.id),
|
||||
fieldLegendId: getTopicLegendFieldId(legendTopic),
|
||||
subfieldLegendId: getTopicLegendSubfieldId(legendTopic),
|
||||
color: topicHierarchyColor(legendTopic),
|
||||
fontSize: Math.round(
|
||||
minimumFontSize + scale * (maximumFontSize - minimumFontSize),
|
||||
),
|
||||
fontWeight: scale > 0.66 ? 800 : scale > 0.33 ? 600 : 400,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
words,
|
||||
legendTopics: displayed.map((topic) => topic.legendTopic),
|
||||
};
|
||||
}
|
||||
|
||||
function aggregateTopics(
|
||||
elements: readonly TopicTimelineElement[],
|
||||
): Map<string, AggregatedTopic> {
|
||||
const aggregated = new Map<string, AggregatedTopic>();
|
||||
|
||||
for (const element of elements) {
|
||||
for (const topic of element.primary_topics ?? []) {
|
||||
if (!topic.name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = topic.id ?? topic.name;
|
||||
const existing = aggregated.get(key);
|
||||
|
||||
if (existing) {
|
||||
existing.count += topic.count;
|
||||
continue;
|
||||
}
|
||||
|
||||
aggregated.set(key, {
|
||||
name: topic.name,
|
||||
count: topic.count,
|
||||
legendTopic: toLegendTopic(topic),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return aggregated;
|
||||
}
|
||||
|
||||
function buildIntensityScale(
|
||||
topics: readonly AggregatedTopic[],
|
||||
): (count: number) => number {
|
||||
const weights = topics.map((topic) => Math.sqrt(Math.max(topic.count, 0)));
|
||||
const minimumWeight = Math.min(...weights);
|
||||
const maximumWeight = Math.max(...weights);
|
||||
|
||||
if (maximumWeight === minimumWeight) {
|
||||
return () => 0.5;
|
||||
}
|
||||
|
||||
return (count) =>
|
||||
(Math.sqrt(Math.max(count, 0)) - minimumWeight) /
|
||||
(maximumWeight - minimumWeight);
|
||||
}
|
||||
|
||||
function toLegendTopic(topic: TopicTimelineTopic): TopicLegendTopic {
|
||||
return {
|
||||
id: topic.id,
|
||||
name: topic.name,
|
||||
field_id: topic.field_id,
|
||||
field_name: topic.field_name,
|
||||
field_description: topic.field_description,
|
||||
domain_id: topic.domain_id,
|
||||
domain_name: topic.domain_name,
|
||||
domain_description: topic.domain_description,
|
||||
subfield_id: topic.subfield_id,
|
||||
subfield_name: topic.subfield_name,
|
||||
subfield_description: topic.subfield_description,
|
||||
};
|
||||
}
|
||||
|
||||
function isWithinYearRange(
|
||||
year: number | undefined,
|
||||
startYear: number | undefined,
|
||||
endYear: number | undefined,
|
||||
): boolean {
|
||||
if (startYear !== undefined && (year === undefined || year < startYear)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (endYear !== undefined && (year === undefined || year > endYear)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function stripOpenAlexId(id: string | null | undefined): string {
|
||||
return (id ?? '').replace('https://openalex.org/', '');
|
||||
}
|
||||
Reference in New Issue
Block a user