90 lines
3.4 KiB
Markdown
90 lines
3.4 KiB
Markdown
# sese-engine Go 重构版
|
||
|
||
Python 原版的 Go 语言重构,使用标准英文命名,单二进制部署。
|
||
|
||
## 目录结构
|
||
|
||
```
|
||
golang/
|
||
├── main.go # 主入口,goroutine 启动所有模块
|
||
├── go.mod
|
||
├── config/
|
||
│ └── config.go # 全局配置参数(对应 配置.py)
|
||
├── storage/
|
||
│ └── storage.go # bbolt 持久化层(对应 存储.py,替换 rimo_storage)
|
||
├── crawler/
|
||
│ ├── crawler.go # BFS 爬虫调度(对应 上网.py)
|
||
│ └── fetcher.go # HTTP 获取 + robots.txt + 限流(对应 虫.py)
|
||
├── parser/
|
||
│ └── parser.go # HTML 解析(对应 文.py)
|
||
├── analyzer/
|
||
│ └── analyzer.go # 分词 + 关键词权重(对应 分析.py + utils.py 分词部分)
|
||
│ 使用 gojieba(中文)+ gofasttext(语言检测)
|
||
├── harvester/
|
||
│ └── harvester.go # 索引写入服务,监听 :5000(对应 收获服务器.py)
|
||
├── search/
|
||
│ └── server.go # 搜索 API,监听 :80(对应 人服务器.py)
|
||
├── backlink/
|
||
│ └── backlink.go # 反向链接计算,每 48h 运行(对应 回.py)
|
||
└── info/
|
||
└── info.go # 繁荣表 / 调整表 / 屏蔽词加载(对应 信息.py)
|
||
```
|
||
|
||
## 依赖项
|
||
|
||
| Go 包 | 替代 Python 包 | 用途 |
|
||
|-------|--------------|------|
|
||
| `github.com/yanyiwu/gojieba` | `jieba` | 中文分词 |
|
||
| `github.com/nicholasgasior/gofasttext` | `fasttext` | 语言检测 |
|
||
| `go.etcd.io/bbolt` | `rimo_storage` | KV 存储 / 倒排索引 |
|
||
| `github.com/andybalholm/brotli` | `brotli` | 压缩 |
|
||
| `golang.org/x/net/html` | `lxml` | HTML 解析 |
|
||
| `golang.org/x/net/html/charset` | chardet | 编码检测 |
|
||
|
||
## 构建与运行
|
||
|
||
```bash
|
||
cd golang
|
||
|
||
# 下载依赖(需要 CGo 编译器,用于 gojieba / gofasttext)
|
||
go mod tidy
|
||
|
||
# 构建
|
||
go build -o sese-engine .
|
||
|
||
# 运行(在 sese-engine 项目根目录下)
|
||
cd ..
|
||
./golang/sese-engine \
|
||
--storage ./savedata \
|
||
--entry https://zh.wikipedia.org/ \
|
||
--fasttext ./lid.176.ftz \
|
||
--stopwords ./data/标点符号.json
|
||
```
|
||
|
||
一个进程启动所有模块:
|
||
- `:5000` — 收获服务器(爬虫推送关键词)
|
||
- `:80` — 搜索 API(`GET /search?q=关键词`)
|
||
- 后台 goroutine — BFS 爬虫
|
||
- 后台 goroutine — 每 48 小时反向链接计算
|
||
|
||
## 与 Python 版的主要差异
|
||
|
||
| 方面 | Python 版 | Go 版 |
|
||
|------|---------|-------|
|
||
| 并发 | GIL + 线程池(假并发) | goroutine 真并发 |
|
||
| 存储 | rimo_storage(自研)| bbolt(嵌入式 KV) |
|
||
| 部署 | 需要 Python 环境 | 单二进制,无运行时依赖 |
|
||
| 命名 | 全中文 | 标准英文 |
|
||
| 进程数 | 3~4 个进程 | 1 个进程多 goroutine |
|
||
| 编码检测 | requests 自动检测 | `golang.org/x/net/html/charset` |
|
||
| Prometheus | 可选 | 暂未集成(可后续添加) |
|
||
|
||
## 注意事项
|
||
|
||
1. **CGo 依赖**:gojieba 和 gofasttext 均需要 C/C++ 编译器(gcc/clang)。
|
||
Windows 下建议使用 MinGW 或 WSL。
|
||
2. **fasttext 模型**:`lid.176.ftz` 需要与 Python 版共用,路径通过 `--fasttext` 指定。
|
||
3. **数据迁移**:存储格式(bbolt JSON)与 Python 版(rimo_storage 二进制)不兼容,
|
||
需要全新爬取,或编写迁移脚本。
|
||
4. **stop words 文件**:复用 Python 版的 `data/标点符号.json`。
|