Huxxshadow commited on
Commit
1484003
·
verified ·
1 Parent(s): a088a71

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -25
app.py CHANGED
@@ -25,7 +25,9 @@ from scipy import stats
25
  from scipy.cluster.hierarchy import dendrogram, linkage
26
  import networkx as nx
27
  from matplotlib.patches import Rectangle
28
-
 
 
29
  warnings.filterwarnings('ignore')
30
 
31
  # 设置绘图样式
@@ -57,40 +59,99 @@ STOPWORDS = set([
57
  ])
58
 
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  def get_chinese_font():
61
- """根据操作系统获取中文字体路径"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  system = platform.system()
63
  if system == 'Windows':
64
- fonts = [
65
- 'C:/Windows/Fonts/msyh.ttc',
66
- 'C:/Windows/Fonts/simhei.ttf',
67
- 'C:/Windows/Fonts/simsun.ttc',
68
- ]
69
  elif system == 'Darwin':
70
- fonts = [
71
- '/System/Library/Fonts/STHeiti Light.ttc',
72
- '/Library/Fonts/Arial Unicode.ttf',
73
- ]
74
- else:
75
- fonts = [
76
- '/usr/share/fonts/truetype/wqy/wqy-microhei.ttc',
77
- '/usr/share/fonts/truetype/droid/DroidSansFallbackFull.ttf',
78
- ]
79
-
80
- for font in fonts:
81
- if os.path.exists(font):
82
- return font
83
  return None
84
 
85
 
 
86
  CHINESE_FONT = get_chinese_font()
87
 
 
88
  if CHINESE_FONT:
89
- if platform.system() == 'Windows':
90
- plt.rcParams['font.sans-serif'] = ['Microsoft YaHei', 'SimHei']
91
- else:
92
- plt.rcParams['font.sans-serif'] = ['Arial Unicode MS', 'DejaVu Sans']
93
- plt.rcParams['axes.unicode_minus'] = False
 
 
 
 
 
 
 
 
 
 
 
 
 
94
 
95
 
96
  class TourismDataAnalyzer:
 
25
  from scipy.cluster.hierarchy import dendrogram, linkage
26
  import networkx as nx
27
  from matplotlib.patches import Rectangle
28
+ import os
29
+ import urllib.request
30
+ from pathlib import Path
31
  warnings.filterwarnings('ignore')
32
 
33
  # 设置绘图样式
 
59
  ])
60
 
61
 
62
+ def download_chinese_font():
63
+ """自动下载中文字体到项目目录"""
64
+ font_dir = Path("fonts")
65
+ font_dir.mkdir(exist_ok=True)
66
+
67
+ font_path = font_dir / "SourceHanSansSC-Regular.otf"
68
+
69
+ if font_path.exists():
70
+ print(f"✅ 字体已存在: {font_path}")
71
+ return str(font_path)
72
+
73
+ print("📥 正在下载思源黑体...")
74
+
75
+ # 使用 GitHub 镜像源(更稳定)
76
+ font_urls = [
77
+ "https://github.com/adobe-fonts/source-han-sans/raw/release/OTF/SimplifiedChinese/SourceHanSansSC-Regular.otf",
78
+ "https://ghproxy.com/https://github.com/adobe-fonts/source-han-sans/raw/release/OTF/SimplifiedChinese/SourceHanSansSC-Regular.otf",
79
+ "https://cdn.jsdelivr.net/gh/adobe-fonts/source-han-sans@release/OTF/SimplifiedChinese/SourceHanSansSC-Regular.otf"
80
+ ]
81
+
82
+ for url in font_urls:
83
+ try:
84
+ print(f"尝试从 {url[:50]}... 下载")
85
+ urllib.request.urlretrieve(url, font_path)
86
+ print(f"✅ 字体下载成功: {font_path}")
87
+ return str(font_path)
88
+ except Exception as e:
89
+ print(f"⚠️ 下载失败: {e}")
90
+ continue
91
+
92
+ print("❌ 所有字体源下载失败")
93
+ return None
94
+
95
+
96
  def get_chinese_font():
97
+ """获取中文字体路径(优先下载)"""
98
+ # 1. 优先使用下载的字体
99
+ downloaded_font = download_chinese_font()
100
+ if downloaded_font and os.path.exists(downloaded_font):
101
+ return downloaded_font
102
+
103
+ # 2. 尝试系统字体(Debian Trixie)
104
+ system_fonts = [
105
+ '/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc',
106
+ '/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc',
107
+ '/usr/share/fonts/truetype/noto/NotoSansCJK-Regular.ttc',
108
+ ]
109
+
110
+ for font in system_fonts:
111
+ if os.path.exists(font):
112
+ print(f"✅ 找到系统字体: {font}")
113
+ return font
114
+
115
+ # 3. Windows/Mac 备用
116
  system = platform.system()
117
  if system == 'Windows':
118
+ win_fonts = ['C:/Windows/Fonts/msyh.ttc', 'C:/Windows/Fonts/simhei.ttf']
119
+ for font in win_fonts:
120
+ if os.path.exists(font):
121
+ return font
 
122
  elif system == 'Darwin':
123
+ mac_fonts = ['/System/Library/Fonts/STHeiti Light.ttc']
124
+ for font in mac_fonts:
125
+ if os.path.exists(font):
126
+ return font
127
+
128
+ print("⚠️ 未找到任何中文字体")
 
 
 
 
 
 
 
129
  return None
130
 
131
 
132
+ # 初始化字体
133
  CHINESE_FONT = get_chinese_font()
134
 
135
+ # 配置 Matplotlib
136
  if CHINESE_FONT:
137
+ import matplotlib.pyplot as plt
138
+ from matplotlib import font_manager
139
+
140
+ # 注册字体
141
+ font_manager.fontManager.addfont(CHINESE_FONT)
142
+ font_prop = font_manager.FontProperties(fname=CHINESE_FONT)
143
+
144
+ plt.rcParams['font.sans-serif'] = [font_prop.get_name()]
145
+ plt.rcParams['font.family'] = 'sans-serif'
146
+ plt.rcParams['axes.unicode_minus'] = False
147
+
148
+ print(f"✅ Matplotlib 已配置字体: {font_prop.get_name()}")
149
+ else:
150
+ import matplotlib.pyplot as plt
151
+
152
+ plt.rcParams['font.sans-serif'] = ['DejaVu Sans']
153
+ plt.rcParams['axes.unicode_minus'] = False
154
+ print("⚠️ 使用默认字体(可能不支持中文)")
155
 
156
 
157
  class TourismDataAnalyzer: