# views.py
# 提取年份
def extract_latest_year(source_filenames):
max_year = 0
year_pattern = re.compile(r'(19|20)\d{2}')
if isinstance(source_filenames, list):
for filename in source_filenames:
matches = year_pattern.findall(str(filename))
for year_str in matches:
try:
year = int(year_str)
if year > max_year:
max_year = year
except ValueError:
pass
return max_year
# 獲取當前年份作為計算基準
CURRENT_YEAR = datetime.now().year
for i, item in enumerate(KNOWLEDGE_BASE):
q_id = item.get('id', i)
if 'error_count' not in item:
item['error_count'] = 0
item['latest_appearance_year'] = extract_latest_year(item.get('來源檔案', []))
item['base_trend_score'] = (q_id % 10) + 1