fix list problem with topic to Keyoword matching (use IDs instead of string matching)

This commit is contained in:
Kazgatari
2026-09-02 13:33:49 +02:00
parent fbdad69a1d
commit e7ad579b02
2 changed files with 128 additions and 114 deletions
@@ -224,18 +224,21 @@ export class SearchResultsStateService {
return result;
}
// Parallel topic-name list for trend dashboard strategies. Entries
// without a resolvable keyword are skipped when building the lookup map
// so they can never collide on an `undefined` key and shadow a real one.
// Parallel topic-name list for trend dashboard strategies. Directly resolves
// from structured selectedKeywordEntries without fragile regex.
readonly topicsList = computed(() => {
const entries = this.selectedKeywordEntries();
const entryByKeyword = new Map<string, KeywordEntryWithTopic>();
for (const entry of entries) {
const kw = entry.llm_keyword?.keyword?.trim().toLowerCase();
if (kw) {
entryByKeyword.set(kw, entry);
}
}
const list = this.keywords()
.map((kw) => {
const normalized = this.normalize(kw);
const entry = this.selectedKeywordEntries().find(
(e) =>
e.llm_keyword?.keyword?.trim().toLowerCase() === normalized ||
e.topic_mapping?.topic?.id?.trim().toLowerCase() === normalized,
);
const entry = entryByKeyword.get(kw.trim().toLowerCase());
const displayName = entry?.topic_mapping?.topic?.display_name;
if (displayName && !displayName.startsWith('http')) {
return displayName;
@@ -246,9 +249,8 @@ export class SearchResultsStateService {
return kw;
})
.filter((t) => t.length > 0);
const uniqueTopics = Array.from(new Set(list));
return uniqueTopics;
return Array.from(new Set(list));
});
setPayload(payload: SearchResultsPayload): void {
@@ -4,6 +4,7 @@ import {
input,
computed,
signal,
linkedSignal,
effect,
untracked,
inject,
@@ -167,7 +168,56 @@ export class UniTrendParentComponent implements OnInit {
readonly selectedSearchType = signal<
'publications' | 'authors' | 'patents' | 'projects' | 'news' | 'all'
>('publications');
readonly selectedTrendTopics = signal<string[]>([]);
readonly selectedTrendTopics = linkedSignal<
{ fuzzy: boolean; topics: string[]; kws: string[] },
string[]
>({
source: () => ({
fuzzy: this.isFuzzy(),
topics: this.state?.topicsList() ?? [],
kws: this.keywords(),
}),
computation: (src, previous) => {
const target = src.fuzzy
? src.topics.length > 0
? src.topics
: src.kws
: src.kws;
if (previous?.value && previous.value.length > 0) {
const preserved = previous.value.filter((item) =>
target.some(
(t) =>
t.toLowerCase().trim() === item.toLowerCase().trim() ||
cleanEntityId(t) === cleanEntityId(item),
),
);
if (preserved.length > 0) {
return preserved;
}
}
return target;
},
});
readonly activeChartTopic = linkedSignal<string[], string>({
source: () => this.selectedTrendTopics(),
computation: (topics, previous) => {
if (
previous?.value &&
topics.some(
(t) =>
t.toLowerCase().trim() === previous.value.toLowerCase().trim() ||
cleanEntityId(t) === cleanEntityId(previous.value),
)
) {
return previous.value;
}
const valid = this.findFirstNonEmptyTopic(topics);
return valid || (topics.length > 0 ? topics[0] : '');
},
});
readonly primaryInstitution = signal<string | null>('I62318514');
readonly primaryInstitutionName = signal<string>(
'Universität Duisburg-Essen',
@@ -191,12 +241,40 @@ export class UniTrendParentComponent implements OnInit {
readonly chartMode = signal<'normalized' | 'total' | 'cumulative'>(
'normalized',
);
readonly activeChartTopic = signal<string>('');
readonly selectedPieEntity = signal<{
id: string;
name: string;
type: 'institution' | 'author' | 'country' | 'region';
} | null>(null);
readonly selectedPieEntity = linkedSignal<
TrendResponse | null,
{
id: string;
name: string;
type: 'institution' | 'author' | 'country' | 'region';
} | null
>({
source: () => this.trendResponse(),
computation: (data, previous) => {
if (!data || !data.series || data.series.length === 0) {
return null;
}
if (previous?.value) {
const stillExists = data.series.some(
(s) =>
cleanEntityId(s.entity_id) === cleanEntityId(previous.value!.id),
);
if (stillExists) {
return previous.value;
}
}
const firstSeries = data.series[0];
if (firstSeries) {
return {
id: cleanEntityId(firstSeries.entity_id),
name: firstSeries.entity_name,
type: firstSeries.entity_type as any,
};
}
return null;
},
});
readonly activeConfig = computed(
() => TREND_CONFIGS[this.selectedSearchType()],
@@ -583,9 +661,22 @@ export class UniTrendParentComponent implements OnInit {
const allSeries =
this.trendResponse()?.series || this.rawTrendResponse()?.series || [];
if (allSeries.length === 0) return true;
const targetLower = topic.toLowerCase().trim();
const targetMappedLower = this.getMappedTopic(topic).toLowerCase().trim();
const series =
this.selectedSearchType() === 'news'
? allSeries.filter((s) => this.seriesTopicName(s.entity_id) === topic)
? allSeries.filter((s) => {
const sTopic = this.seriesTopicName(s.entity_id)
.toLowerCase()
.trim();
const sMapped = this.getMappedTopic(sTopic).toLowerCase().trim();
return (
sTopic === targetLower ||
sTopic === targetMappedLower ||
sMapped === targetLower ||
sMapped === targetMappedLower
);
})
: allSeries;
return isTopicEmptyForSeries(
topic,
@@ -777,14 +868,25 @@ export class UniTrendParentComponent implements OnInit {
if (type === 'news') {
const active = this.activeChartTopic() || topics[0] || '';
const wanted = new Set([
NEWS_ENTITY_PREFIX + active,
CC_NEWS_ENTITY_PREFIX + active,
]);
const activeLower = active.toLowerCase().trim();
const mappedActiveLower = this.getMappedTopic(active)
.toLowerCase()
.trim();
return {
...raw,
series: raw.series
.filter((s) => wanted.has(s.entity_id))
.filter((s) => {
const sTopic = this.seriesTopicName(s.entity_id)
.toLowerCase()
.trim();
const sMapped = this.getMappedTopic(sTopic).toLowerCase().trim();
return (
sTopic === activeLower ||
sTopic === mappedActiveLower ||
sMapped === activeLower ||
sMapped === mappedActiveLower
);
})
.map((s) => ({ ...normalizeTopicData(s), visible: true })),
};
}
@@ -925,63 +1027,6 @@ export class UniTrendParentComponent implements OnInit {
}
});
// Sync active topics list whenever keywords or fuzzy mode state changes
effect(() => {
const kws = this.keywords();
const fuzzy = this.isFuzzy();
const stateTopicsList = this.state?.topicsList();
const topics = fuzzy ? (stateTopicsList ?? kws) : kws;
if (topics.length > 0) {
const currentSelections = untracked(() => this.selectedTrendTopics());
const altTopics = fuzzy ? kws : (stateTopicsList ?? kws);
const mappedSelections: string[] = [];
currentSelections.forEach((item) => {
if (topics.includes(item)) {
mappedSelections.push(item);
} else {
const idx = altTopics.indexOf(item);
if (idx !== -1 && idx < topics.length && topics[idx]) {
mappedSelections.push(topics[idx]);
}
}
});
const targetSelections =
mappedSelections.length > 0
? Array.from(new Set(mappedSelections))
: topics;
// Only update selectedTrendTopics if array actually changed to prevent DOM re-creation
const current = untracked(() => this.selectedTrendTopics());
const hasChanged =
targetSelections.length !== current.length ||
targetSelections.some((t, i) => t !== current[i]);
if (hasChanged) {
this.selectedTrendTopics.set(targetSelections);
}
const currentActive = untracked(() => this.activeChartTopic());
const validSelections = untracked(() => this.selectedTrendTopics());
if (!currentActive || !validSelections.includes(currentActive)) {
const firstValid = this.findFirstNonEmptyTopic(validSelections);
if (firstValid) {
this.activeChartTopic.set(firstValid);
} else if (validSelections.length > 0) {
this.activeChartTopic.set(validSelections[0]);
}
}
} else {
const current = untracked(() => this.selectedTrendTopics());
if (current.length > 0) {
this.selectedTrendTopics.set([]);
}
}
});
// Effect: load suggestions when active topic or location filters change
effect(() => {
const topic = this.activeChartTopic();
@@ -994,39 +1039,6 @@ export class UniTrendParentComponent implements OnInit {
});
});
// Sync selectedPieEntity when trendResponse series update
effect(() => {
const data = this.trendResponse();
const currentPie = untracked(() => this.selectedPieEntity());
if (!data || data.series.length === 0) {
this.selectedPieEntity.set(null);
return;
}
if (currentPie) {
const stillExists = data.series.some((s) => {
const sId = cleanEntityId(s.entity_id);
const targetId = cleanEntityId(currentPie.id);
return sId === targetId;
});
if (stillExists) {
return;
}
}
const firstSeries = data.series[0];
if (firstSeries) {
this.selectedPieEntity.set({
id: cleanEntityId(firstSeries.entity_id),
name: firstSeries.entity_name,
type: firstSeries.entity_type as any,
});
} else {
this.selectedPieEntity.set(null);
}
});
// Load top 10 topics for selected pie entity
effect(() => {
const entity = this.selectedPieEntity();