Initial toolkit: scripts, references, skills, and term database
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
# CodeGraph data files
|
||||
# These are local to each machine and should not be committed
|
||||
|
||||
# Database
|
||||
*.db
|
||||
*.db-wal
|
||||
*.db-shm
|
||||
|
||||
# Cache
|
||||
cache/
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
# Hook markers
|
||||
.dirty
|
||||
@@ -0,0 +1,4 @@
|
||||
.git
|
||||
**/__pycache__
|
||||
**/__pypackages__
|
||||
fly.toml
|
||||
@@ -0,0 +1,2 @@
|
||||
__pycache__/
|
||||
__pypackages__/
|
||||
@@ -0,0 +1,11 @@
|
||||
FROM python:3.14-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN pip install --no-cache-dir flask duckdb gunicorn
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 8910
|
||||
|
||||
CMD ["gunicorn", "-b", "0.0.0.0:8910", "-w", "1", "--timeout", "30", "server:app"]
|
||||
Executable
+2
@@ -0,0 +1,2 @@
|
||||
#!/usr/bin/fish
|
||||
fly deploy -y --depot=false
|
||||
@@ -0,0 +1,20 @@
|
||||
# fly.toml app configuration file generated for buddhist-terms-search on 2026-06-09T13:38:18+08:00
|
||||
#
|
||||
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
|
||||
#
|
||||
|
||||
app = 'buddhist-terms-search'
|
||||
primary_region = 'sin'
|
||||
|
||||
[http_service]
|
||||
internal_port = 8910
|
||||
force_https = true
|
||||
auto_stop_machines = 'stop'
|
||||
auto_start_machines = true
|
||||
min_machines_running = 0
|
||||
processes = ['app']
|
||||
|
||||
[[vm]]
|
||||
memory = '256mb'
|
||||
cpus = 1
|
||||
memory_mb = 256
|
||||
Executable
+124
@@ -0,0 +1,124 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Full-text search over MPI term database. Queries unified_terms_flat via DuckDB LIKE.
|
||||
|
||||
Module usage:
|
||||
from search import search_terms
|
||||
results = search_terms("空性")
|
||||
results = search_terms("空性", limit=5, loc="心经", src="佛教术语")
|
||||
# returns list of dicts: {zh, en, loc, source}
|
||||
|
||||
CLI usage:
|
||||
python search.py <query> [limit]
|
||||
python search.py 空性 loc:心经 src:公案
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import duckdb
|
||||
|
||||
DB = os.path.join(os.path.dirname(os.path.abspath(__file__)), "termlib.duckdb")
|
||||
|
||||
|
||||
def _connect():
|
||||
return duckdb.connect(DB, read_only=True)
|
||||
|
||||
|
||||
def _search_rows(con, query, loc=None, src=None, limit=None):
|
||||
terms = query.split() if query else []
|
||||
clauses = []
|
||||
params = []
|
||||
|
||||
for t in terms:
|
||||
like = f"%{t}%"
|
||||
clauses.append("(zh LIKE ? OR en LIKE ?)")
|
||||
params.extend([like, like])
|
||||
|
||||
where = " AND ".join(clauses) if clauses else "1=1"
|
||||
|
||||
if loc:
|
||||
where += " AND loc LIKE ?"
|
||||
params.append(f"%{loc}%")
|
||||
if src:
|
||||
where += " AND source = ?"
|
||||
params.append(src)
|
||||
|
||||
sql = f"SELECT zh, en, loc, source FROM unified_terms_flat WHERE {where}"
|
||||
if limit is not None:
|
||||
sql += " LIMIT ?"
|
||||
params.append(limit)
|
||||
|
||||
return con.execute(sql, params).fetchall()
|
||||
|
||||
|
||||
def search(query, loc=None, src=None, limit=20):
|
||||
"""Search the terms database. Returns list of {zh, en, loc, source} dicts.
|
||||
|
||||
query: str — space-separated search terms (AND logic)
|
||||
loc: str — filter by loc column (LIKE match)
|
||||
src: str — filter by source column (exact match)
|
||||
limit: int — max results (default 20)
|
||||
"""
|
||||
con = _connect()
|
||||
try:
|
||||
rows = _search_rows(con, query, loc=loc, src=src, limit=limit)
|
||||
return [
|
||||
{"zh": zh, "en": en, "loc": loc_val or "", "source": src_val}
|
||||
for zh, en, loc_val, src_val in rows
|
||||
]
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: terms-search <query> [limit]")
|
||||
print(" queries: '空性', 'emptiness 中观'")
|
||||
print()
|
||||
print("Special prefixes (can be standalone or combined with query):")
|
||||
print(" loc:<source> — filter by loc (e.g. loc:心经)")
|
||||
print(" src:<table> — filter by source table (e.g. src:佛教术语)")
|
||||
print()
|
||||
print("Examples:")
|
||||
print(" terms-search 空性")
|
||||
print(" terms-search 'suffering 苦' loc:心经")
|
||||
print(" terms-search src:公案")
|
||||
sys.exit(1)
|
||||
|
||||
raw = sys.argv[1]
|
||||
limit = int(sys.argv[2]) if len(sys.argv) > 2 else 20
|
||||
|
||||
loc_filter = None
|
||||
src_filter = None
|
||||
query_parts = []
|
||||
|
||||
for token in raw.split():
|
||||
if token.startswith("loc:"):
|
||||
loc_filter = token[4:]
|
||||
elif token.startswith("src:"):
|
||||
src_filter = token[4:]
|
||||
else:
|
||||
query_parts.append(token)
|
||||
|
||||
query_str = " ".join(query_parts)
|
||||
|
||||
if not query_str and not loc_filter and not src_filter:
|
||||
print("No search terms or filters. Usage: terms-search <query> [limit]")
|
||||
return
|
||||
|
||||
results = search(query_str, loc=loc_filter, src=src_filter, limit=limit)
|
||||
|
||||
if not results:
|
||||
print(f"No results for: {raw}")
|
||||
return
|
||||
|
||||
print(f"Results: {len(results)}")
|
||||
print()
|
||||
for r in results:
|
||||
print(f"zh: {r['zh']}")
|
||||
print(f"en: {r['en']}")
|
||||
print(f"loc: {r['loc'] or '-'} | src: {r['source']}")
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
import duckdb
|
||||
from flask import Flask, request, jsonify, render_template
|
||||
import search as s
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
def do_search(q, loc, src, limit):
|
||||
with duckdb.connect(s.DB, read_only=True) as con:
|
||||
rows = s.search(con, q, loc, src, limit)
|
||||
return [{'zh': r[0], 'en': r[1], 'loc': r[2] or None, 'source': r[3]} for r in rows]
|
||||
|
||||
def do_sources():
|
||||
with duckdb.connect(s.DB, read_only=True) as con:
|
||||
rows = con.execute(
|
||||
'SELECT source, COUNT(*) AS cnt FROM unified_terms_flat GROUP BY source ORDER BY cnt DESC'
|
||||
).fetchall()
|
||||
return [{'source': r[0], 'count': r[1]} for r in rows]
|
||||
|
||||
@app.route('/search')
|
||||
def search():
|
||||
try:
|
||||
q = request.args.get('q', '')
|
||||
loc = request.args.get('loc')
|
||||
src = request.args.get('src')
|
||||
limit = request.args.get('limit', type=int)
|
||||
results = do_search(q, loc, src, limit)
|
||||
return jsonify({'count': len(results), 'results': results})
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@app.route('/sources')
|
||||
def sources():
|
||||
try:
|
||||
sources = do_sources()
|
||||
return jsonify(sources)
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@app.route('/sources.html')
|
||||
def sources_html():
|
||||
try:
|
||||
sources = do_sources()
|
||||
total = sum(s['count'] for s in sources)
|
||||
return render_template('sources.html', sources=sources, total=total)
|
||||
except Exception as e:
|
||||
return render_template('sources.html', sources=[], total=0, error=str(e))
|
||||
|
||||
@app.route('/')
|
||||
def ui():
|
||||
q = request.args.get('q', '')
|
||||
loc = request.args.get('loc', '')
|
||||
src = request.args.get('src', '')
|
||||
limit = request.args.get('limit', '')
|
||||
searched = bool(request.args)
|
||||
results = []
|
||||
count = 0
|
||||
error = None
|
||||
if searched and (q or loc or src):
|
||||
try:
|
||||
lim = int(limit) if limit else None
|
||||
results = do_search(q, loc or None, src or None, lim)
|
||||
count = len(results)
|
||||
except Exception as e:
|
||||
error = str(e)
|
||||
return render_template('index.html', q=q, loc=loc, src=src, limit=limit,
|
||||
searched=searched, results=results, count=count, error=error)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(port=8910)
|
||||
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8">
|
||||
<title>静心学堂中英文翻译对照表</title>
|
||||
<h1>静心学堂(非官方)中英文翻译对照表</h1>
|
||||
<p><a href="/">search</a> | <a href="/sources.html">sources</a></p>
|
||||
<form method="get">
|
||||
<input name="q" value="{{ q }}" placeholder="query (e.g. 空性 emptiness)">
|
||||
<input name="loc" value="{{ loc }}" placeholder="loc filter">
|
||||
<input name="src" value="{{ src }}" placeholder="src filter">
|
||||
<input name="limit" value="{{ limit }}" size="4" placeholder="limit">
|
||||
<button type="submit">Search</button>
|
||||
</form>
|
||||
|
||||
{% if searched %}
|
||||
{% if error %}<p>Error: {{ error }}</p>
|
||||
{% else %}
|
||||
<p>{{ count }} results</p>
|
||||
<table border="1" cellpadding="4" cellspacing="0">
|
||||
<tr><th>ZH</th><th>EN</th><th>LOC</th><th>SRC</th></tr>
|
||||
{% for r in results %}
|
||||
<tr>
|
||||
<td>{{ r.zh }}</td>
|
||||
<td>{{ r.en }}</td>
|
||||
<td>{{ r.loc or '' }}</td>
|
||||
<td>{{ r.source }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
@@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<meta charset="utf-8">
|
||||
<title>Sources — 静心学堂翻译对照表</title>
|
||||
<h1>Sources</h1>
|
||||
<p><a href="/">search</a> | <a href="/sources.html">sources</a></p>
|
||||
|
||||
{% if error %}<p>Error: {{ error }}</p>
|
||||
{% else %}
|
||||
<table border="1" cellpadding="4" cellspacing="0">
|
||||
<tr><th>Source</th><th>Rows</th></tr>
|
||||
{% for s in sources %}
|
||||
<tr>
|
||||
<td><a href="/?src={{ s.source }}">{{ s.source }}</a></td>
|
||||
<td>{{ s.count }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
<tr><th>Total</th><th>{{ total }}</th></tr>
|
||||
</table>
|
||||
{% endif %}
|
||||
Binary file not shown.
Reference in New Issue
Block a user