fix(wordcloud): build from timeline data and primary topics instead of the 100 most recent works to resolve empty wordcloud issue for early years
This commit is contained in:
+106
-60
@@ -8,6 +8,10 @@ import {
|
||||
getTopicLegendSubfieldId,
|
||||
TopicLegendTopic,
|
||||
} from '../../../../resolvers/utils/topic-legend';
|
||||
import type {
|
||||
TopicTimelineElement,
|
||||
TopicTimelineTopic,
|
||||
} from '../research-timeline/research-timeline.component';
|
||||
import { workQueryParamKeys } from '../../../../resolvers/work-page.resolver';
|
||||
|
||||
interface WordTooltipInputs {
|
||||
@@ -17,18 +21,6 @@ interface WordTooltipInputs {
|
||||
secondaryActionQueryParams: Record<string, string | number>;
|
||||
}
|
||||
|
||||
export interface WordCloudTopicOccurrence {
|
||||
name: string;
|
||||
topicId: string;
|
||||
year: number | undefined;
|
||||
fieldId: string | undefined;
|
||||
fieldName: string | undefined;
|
||||
domainId: string | undefined;
|
||||
domainName: string | undefined;
|
||||
subfieldId: string | undefined;
|
||||
subfieldName: string | undefined;
|
||||
}
|
||||
|
||||
export interface WordCloudWord {
|
||||
name: string;
|
||||
topicId: string;
|
||||
@@ -49,67 +41,55 @@ const displayedWordLimit = 30;
|
||||
const minimumFontSize = 12;
|
||||
const maximumFontSize = 32;
|
||||
|
||||
const minimumPrimaryTopicWords = 10;
|
||||
|
||||
interface AggregatedTopic {
|
||||
name: string;
|
||||
count: number;
|
||||
legendTopic: TopicLegendTopic;
|
||||
}
|
||||
|
||||
export function buildResearchWordcloud(
|
||||
occurrences: WordCloudTopicOccurrence[],
|
||||
elements: readonly TopicTimelineElement[],
|
||||
startYear: number | undefined,
|
||||
endYear: number | undefined,
|
||||
): ResearchWordcloud {
|
||||
const counts = new Map<string, number>();
|
||||
const legendTopicsByName = new Map<string, TopicLegendTopic>();
|
||||
const yearsInRange = elements.filter((element) =>
|
||||
isWithinYearRange(element.year, startYear, endYear),
|
||||
);
|
||||
|
||||
for (const occurrence of occurrences) {
|
||||
if (!isWithinYearRange(occurrence.year, startYear, endYear)) {
|
||||
continue;
|
||||
}
|
||||
const primaryTopics = aggregateTopics(
|
||||
yearsInRange,
|
||||
(element) => element.primary_topics ?? [],
|
||||
);
|
||||
const aggregated =
|
||||
primaryTopics.size >= minimumPrimaryTopicWords
|
||||
? primaryTopics
|
||||
: aggregateTopics(yearsInRange, (element) => element.topics ?? []);
|
||||
|
||||
counts.set(occurrence.name, (counts.get(occurrence.name) ?? 0) + 1);
|
||||
|
||||
if (!legendTopicsByName.has(occurrence.name)) {
|
||||
legendTopicsByName.set(occurrence.name, {
|
||||
id: occurrence.topicId,
|
||||
name: occurrence.name,
|
||||
field_id: occurrence.fieldId,
|
||||
field_name: occurrence.fieldName,
|
||||
domain_id: occurrence.domainId,
|
||||
domain_name: occurrence.domainName,
|
||||
subfield_id: occurrence.subfieldId,
|
||||
subfield_name: occurrence.subfieldName,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (counts.size === 0) {
|
||||
if (aggregated.size === 0) {
|
||||
return { words: [], legendTopics: [] };
|
||||
}
|
||||
|
||||
const values = Array.from(counts.values());
|
||||
const minimumValue = Math.min(...values);
|
||||
const maximumValue = Math.max(...values);
|
||||
const intensity = (value: number): number =>
|
||||
maximumValue === minimumValue
|
||||
? 1
|
||||
: (value - minimumValue) / (maximumValue - minimumValue);
|
||||
|
||||
const displayed = Array.from(counts.entries())
|
||||
.sort((left, right) => right[1] - left[1])
|
||||
const displayed = Array.from(aggregated.values())
|
||||
.sort(
|
||||
(left, right) =>
|
||||
right.count - left.count || left.name.localeCompare(right.name),
|
||||
)
|
||||
.slice(0, displayedWordLimit);
|
||||
|
||||
const legendTopics = displayed
|
||||
.map(([name]) => legendTopicsByName.get(name))
|
||||
.filter((topic): topic is TopicLegendTopic => Boolean(topic));
|
||||
const intensity = buildIntensityScale(displayed);
|
||||
|
||||
const words = displayed.map(([name, value]): WordCloudWord => {
|
||||
const legendTopic = legendTopicsByName.get(name);
|
||||
const scale = intensity(value);
|
||||
const words = displayed.map((topic): WordCloudWord => {
|
||||
const legendTopic = topic.legendTopic;
|
||||
const scale = intensity(topic.count);
|
||||
|
||||
return {
|
||||
name,
|
||||
topicId: legendTopic?.id ?? '',
|
||||
rawTopicId: stripOpenAlexId(legendTopic?.id),
|
||||
fieldLegendId: legendTopic ? getTopicLegendFieldId(legendTopic) : '',
|
||||
subfieldLegendId: legendTopic
|
||||
? getTopicLegendSubfieldId(legendTopic)
|
||||
: '',
|
||||
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),
|
||||
@@ -118,7 +98,73 @@ export function buildResearchWordcloud(
|
||||
};
|
||||
});
|
||||
|
||||
return { words: centreWords(words), legendTopics };
|
||||
return {
|
||||
words: centreWords(words),
|
||||
legendTopics: displayed.map((topic) => topic.legendTopic),
|
||||
};
|
||||
}
|
||||
|
||||
function aggregateTopics(
|
||||
elements: readonly TopicTimelineElement[],
|
||||
select: (element: TopicTimelineElement) => readonly TopicTimelineTopic[],
|
||||
): Map<string, AggregatedTopic> {
|
||||
const aggregated = new Map<string, AggregatedTopic>();
|
||||
|
||||
for (const element of elements) {
|
||||
for (const topic of select(element)) {
|
||||
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(
|
||||
|
||||
@@ -15,7 +15,6 @@ import {
|
||||
import {
|
||||
buildResearchWordcloud,
|
||||
ResearchWordcloudComponent,
|
||||
WordCloudTopicOccurrence,
|
||||
WordCloudWord,
|
||||
} from './components/research-wordcloud/research-wordcloud.component';
|
||||
import { workQueryParamKeys } from '../../resolvers/work-page.resolver';
|
||||
@@ -40,7 +39,7 @@ export class PersonOverviewComponent implements OnInit {
|
||||
'timeline_legend_visible';
|
||||
private static readonly timelineLegendExpandedQueryParam =
|
||||
'timeline_legend_expanded';
|
||||
private static readonly timelineTopicCountLimit = 4;
|
||||
private static readonly timelineTopicCountLimit = 20;
|
||||
private static readonly initialTimelineTargetPublications = 250;
|
||||
|
||||
private readonly route = inject(ActivatedRoute);
|
||||
@@ -73,8 +72,6 @@ export class PersonOverviewComponent implements OnInit {
|
||||
personName = '';
|
||||
personPublicationsLink: unknown[] | null = null;
|
||||
|
||||
private rawTopicOccurrences: WordCloudTopicOccurrence[] = [];
|
||||
|
||||
ngOnInit(): void {
|
||||
combineLatest(this.route.pathFromRoot.map((route) => route.paramMap))
|
||||
.pipe(
|
||||
@@ -88,7 +85,6 @@ export class PersonOverviewComponent implements OnInit {
|
||||
? ['/person', authorId, 'publications']
|
||||
: null;
|
||||
this.loadTopicTimeline(authorId);
|
||||
this.loadWordCloud(authorId);
|
||||
});
|
||||
|
||||
this.loadPersonName();
|
||||
@@ -99,12 +95,24 @@ export class PersonOverviewComponent implements OnInit {
|
||||
this.timelineViewMode = this.parseTimelineViewMode(
|
||||
queryParams.get(PersonOverviewComponent.timelineViewQueryParam),
|
||||
);
|
||||
this.timelineStartYear = this.parseOptionalYear(
|
||||
|
||||
const startYear = this.parseOptionalYear(
|
||||
queryParams.get(PersonOverviewComponent.timelineStartYearQueryParam),
|
||||
);
|
||||
this.timelineEndYear = this.parseOptionalYear(
|
||||
const endYear = this.parseOptionalYear(
|
||||
queryParams.get(PersonOverviewComponent.timelineEndYearQueryParam),
|
||||
);
|
||||
const yearRangeChanged =
|
||||
startYear !== this.timelineStartYear ||
|
||||
endYear !== this.timelineEndYear;
|
||||
|
||||
this.timelineStartYear = startYear;
|
||||
this.timelineEndYear = endYear;
|
||||
|
||||
if (yearRangeChanged) {
|
||||
this.rebuildWordCloud();
|
||||
}
|
||||
|
||||
this.timelineLegendVisible = this.parseBooleanQueryParam(
|
||||
queryParams.get(
|
||||
PersonOverviewComponent.timelineLegendVisibleQueryParam,
|
||||
@@ -246,9 +254,11 @@ export class PersonOverviewComponent implements OnInit {
|
||||
return;
|
||||
}
|
||||
|
||||
this.topicTimelineElements = [];
|
||||
this.setTimelineElements([]);
|
||||
this.loadingTimeline = true;
|
||||
this.loadingWordCloud = true;
|
||||
this.timelineLoadFailed = false;
|
||||
this.wordCloudLoadFailed = false;
|
||||
this.timelineCanLoadEarlierYears = false;
|
||||
this.timelineLoadingEarlierYears = false;
|
||||
this.timelineEarlierYearsLoadFailed = false;
|
||||
@@ -269,9 +279,11 @@ export class PersonOverviewComponent implements OnInit {
|
||||
|
||||
const timelineElements = this.mapTimelineElements(response.elements);
|
||||
|
||||
this.topicTimelineElements = timelineElements;
|
||||
this.setTimelineElements(timelineElements);
|
||||
this.loadingTimeline = false;
|
||||
this.loadingWordCloud = false;
|
||||
this.timelineLoadFailed = false;
|
||||
this.wordCloudLoadFailed = false;
|
||||
this.timelineCanLoadEarlierYears =
|
||||
response.is_limited ??
|
||||
this.isLikelyLimitedTimeline(timelineElements);
|
||||
@@ -282,9 +294,11 @@ export class PersonOverviewComponent implements OnInit {
|
||||
}
|
||||
|
||||
console.error('Topic timeline could not be loaded.', error);
|
||||
this.topicTimelineElements = [];
|
||||
this.setTimelineElements([]);
|
||||
this.loadingTimeline = false;
|
||||
this.loadingWordCloud = false;
|
||||
this.timelineLoadFailed = true;
|
||||
this.wordCloudLoadFailed = true;
|
||||
this.timelineCanLoadEarlierYears = false;
|
||||
this.timelineLoadingEarlierYears = false;
|
||||
this.timelineEarlierYearsLoadFailed = false;
|
||||
@@ -316,9 +330,11 @@ export class PersonOverviewComponent implements OnInit {
|
||||
response.elements,
|
||||
);
|
||||
|
||||
this.topicTimelineElements = this.mergeTimelineElements(
|
||||
this.topicTimelineElements,
|
||||
completeTimelineElements,
|
||||
this.setTimelineElements(
|
||||
this.mergeTimelineElements(
|
||||
this.topicTimelineElements,
|
||||
completeTimelineElements,
|
||||
),
|
||||
);
|
||||
this.timelineCanLoadEarlierYears = false;
|
||||
this.timelineLoadingEarlierYears = false;
|
||||
@@ -338,9 +354,11 @@ export class PersonOverviewComponent implements OnInit {
|
||||
}
|
||||
|
||||
private resetTimelineState(): void {
|
||||
this.topicTimelineElements = [];
|
||||
this.setTimelineElements([]);
|
||||
this.loadingTimeline = false;
|
||||
this.loadingWordCloud = false;
|
||||
this.timelineLoadFailed = false;
|
||||
this.wordCloudLoadFailed = false;
|
||||
this.timelineCanLoadEarlierYears = false;
|
||||
this.timelineLoadingEarlierYears = false;
|
||||
this.timelineEarlierYearsLoadFailed = false;
|
||||
@@ -365,63 +383,14 @@ export class PersonOverviewComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
private loadWordCloud(authorId: string): void {
|
||||
if (!authorId || authorId === '0') {
|
||||
this.wordCloudWords = [];
|
||||
this.wordCloudLegendTopics = [];
|
||||
this.wordCloudLoadFailed = false;
|
||||
this.loadingWordCloud = false;
|
||||
this.rawTopicOccurrences = [];
|
||||
return;
|
||||
}
|
||||
|
||||
this.loadingWordCloud = true;
|
||||
this.wordCloudLoadFailed = false;
|
||||
this.wordCloudWords = [];
|
||||
this.wordCloudLegendTopics = [];
|
||||
|
||||
this.worksService
|
||||
.getAuthorWorks(authorId, 1, 100)
|
||||
.pipe(takeUntilDestroyed(this.destroyRef))
|
||||
.subscribe({
|
||||
next: (page) => {
|
||||
this.rawTopicOccurrences = [];
|
||||
page.elements?.forEach((work) => {
|
||||
const year = work.publication_year ?? undefined;
|
||||
work.topics?.forEach((topic) => {
|
||||
if (topic.display_name && topic.id) {
|
||||
this.rawTopicOccurrences.push({
|
||||
name: topic.display_name,
|
||||
topicId: topic.id,
|
||||
year,
|
||||
fieldId: topic.field?.id ?? undefined,
|
||||
fieldName: topic.field?.display_name ?? undefined,
|
||||
domainId: topic.domain?.id ?? undefined,
|
||||
domainName: topic.domain?.display_name ?? undefined,
|
||||
subfieldId: topic.subfield?.id ?? undefined,
|
||||
subfieldName: topic.subfield?.display_name ?? undefined,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
this.loadingWordCloud = false;
|
||||
|
||||
setTimeout(() => this.rebuildWordCloud());
|
||||
},
|
||||
error: (error) => {
|
||||
console.error('Word cloud could not be loaded.', error);
|
||||
this.loadingWordCloud = false;
|
||||
this.rawTopicOccurrences = [];
|
||||
this.wordCloudLegendTopics = [];
|
||||
this.wordCloudLoadFailed = true;
|
||||
},
|
||||
});
|
||||
private setTimelineElements(elements: TopicTimelineElement[]): void {
|
||||
this.topicTimelineElements = elements;
|
||||
this.rebuildWordCloud();
|
||||
}
|
||||
|
||||
private rebuildWordCloud(): void {
|
||||
const wordCloud = buildResearchWordcloud(
|
||||
this.rawTopicOccurrences,
|
||||
this.topicTimelineElements,
|
||||
this.timelineStartYear,
|
||||
this.timelineEndYear,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user