infx 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. infx/__init__.py +207 -0
  2. infx/__main__.py +5 -0
  3. infx/batch.py +82 -0
  4. infx/cli.py +86 -0
  5. infx/colony.py +479 -0
  6. infx/core/__init__.py +35 -0
  7. infx/core/errors.py +23 -0
  8. infx/core/pipeline.py +65 -0
  9. infx/core/registry.py +58 -0
  10. infx/core/schema.py +175 -0
  11. infx/core/scoring.py +89 -0
  12. infx/core/text.py +98 -0
  13. infx/death.py +147 -0
  14. infx/deps.py +106 -0
  15. infx/grammar/__init__.py +67 -0
  16. infx/grammar/engine.py +178 -0
  17. infx/grammar/judge.py +53 -0
  18. infx/grammar/lexicon.py +187 -0
  19. infx/grammar/rules.py +140 -0
  20. infx/grammar/schema.py +84 -0
  21. infx/judgment/__init__.py +15 -0
  22. infx/judgment/base.py +51 -0
  23. infx/judgment/fact/__init__.py +5 -0
  24. infx/judgment/fact/detectors.py +182 -0
  25. infx/judgment/opinion/__init__.py +9 -0
  26. infx/judgment/opinion/cues.py +149 -0
  27. infx/knowledge.py +149 -0
  28. infx/look/__init__.py +72 -0
  29. infx/look/__main__.py +5 -0
  30. infx/look/backend.py +68 -0
  31. infx/look/cli.py +110 -0
  32. infx/look/evidence.py +76 -0
  33. infx/look/eye.py +216 -0
  34. infx/look/lmm.py +83 -0
  35. infx/look/sight.py +168 -0
  36. infx/look/table.py +84 -0
  37. infx/patterns.py +287 -0
  38. infx/py.typed +1 -0
  39. infx/quality.py +181 -0
  40. infx/report.py +108 -0
  41. infx/settle.py +114 -0
  42. infx/similarity.py +99 -0
  43. infx/spoken/__init__.py +12 -0
  44. infx/spoken/phrases.py +95 -0
  45. infx/spoken/render.py +121 -0
  46. infx/steps.py +268 -0
  47. infx/syntax/__init__.py +33 -0
  48. infx/syntax/clauses.py +70 -0
  49. infx/syntax/features.py +122 -0
  50. infx/syntax/scope.py +66 -0
  51. infx/table.py +116 -0
  52. infx/truth.py +303 -0
  53. infx-1.0.0.dist-info/METADATA +342 -0
  54. infx-1.0.0.dist-info/RECORD +58 -0
  55. infx-1.0.0.dist-info/WHEEL +5 -0
  56. infx-1.0.0.dist-info/entry_points.txt +3 -0
  57. infx-1.0.0.dist-info/licenses/LICENSE +21 -0
  58. infx-1.0.0.dist-info/top_level.txt +1 -0
infx/__init__.py ADDED
@@ -0,0 +1,207 @@
1
+ """infx —— 推理力模块。
2
+
3
+ 库名 `inference`,import 名 `infx`。
4
+
5
+ 它做四件事,对应计划的 19 条:
6
+
7
+ 1. **猜与死**(`colony` / `death`):假设单元一轮一轮猜,猜错累计到死亡线就死;
8
+ 坐标永不变,内容可死,死后留指纹,复活回原位。
9
+ 2. **判真伪**(`truth`):单源是观点,多源独立一致才是真实;会自动提问并请外部裁判。
10
+ 3. **学步骤**(`steps`):任意长、可嵌套、可条件跳转的步骤序列,从轨迹里学、还能进化。
11
+ 4. **看图片**(`infx.look`):LMM 看懂、RNN 让眼睛会动会怕、并指出下一个该看的点。
12
+
13
+ 另外 `settle` 负责"猜中之后的三求",`knowledge` 负责沉淀,`table` 用 pandas 管结果。
14
+
15
+ ```python
16
+ import infx
17
+
18
+ # 猜与死
19
+ colony = infx.Colony(8, 2, size=48, backend="numpy")
20
+ report = colony.solve(X, y, max_rounds=60)
21
+
22
+ # 猜中之后:再求值 → 再追求 → 再求知
23
+ infx.settle(colony, X, y, X_val=X[:20], y_val=y[:20], graph=infx.Graph())
24
+
25
+ # 看图片
26
+ infx.look.next_point(image) # 下一个该看的点
27
+ infx.look.Eye().look(image) # RNN 眼睛:会动、会预测、会怕
28
+
29
+ # 结果用表格
30
+ infx.Store().add(report).frame
31
+ ```
32
+ """
33
+
34
+ from __future__ import annotations
35
+
36
+ __version__ = "1.0.0"
37
+
38
+ # ---- 猜与死 ---------------------------------------------------------------
39
+ from . import batch, deps, grammar, look, patterns, report, similarity, syntax
40
+ from .colony import Colony, Neuron, Prediction, RoundResult, SolveReport
41
+ from .death import DeathPolicy, LifeState, Tombstone, make_residue
42
+ from .knowledge import Edge, Fact, Graph
43
+ from .patterns import KINDS, EvolvingPool, pool, special
44
+ from .settle import Settlement, settle
45
+ from .steps import Context, Event, Policy, Sequence, Step, Trace
46
+ from .steps import Sequence as StepSequence
47
+ from .table import Store, neurons_frame, rounds_frame, verdicts_frame
48
+ from .truth import KeywordJudge, Question, Questioner, Resolver, Source, cross_validate
49
+ from .truth import Judge as SourceJudge
50
+ from .truth import Label as TruthLabel
51
+ from .truth import Verdict as TruthVerdict
52
+
53
+ # ---- 语法与句法(新增)----------------------------------------------------
54
+ from .batch import bulk_judge, judge_jsonl
55
+ from .grammar import GrammarJudge, judges_with_grammar
56
+ from .report import grammar_report, syntax_report
57
+ from .report import markdown as markdown_report
58
+ from .report import text as text_report
59
+ from .similarity import cosine, dedupe, jaccard, most_similar
60
+ from .syntax import SyntaxProfile, analyze as analyze_syntax
61
+ from .quality import evaluate as evaluate_quality
62
+ from .quality import report as quality_report
63
+ from . import quality
64
+
65
+ # ---- 判真伪(文本事实/观点)-----------------------------------------------
66
+ from .core import (
67
+ Assessment,
68
+ Claim,
69
+ Cue,
70
+ Label,
71
+ Verdict,
72
+ assess,
73
+ available_judges,
74
+ judge_claim,
75
+ )
76
+ from .spoken import speak
77
+
78
+ # ---- 看图片 ---------------------------------------------------------------
79
+ from .look import Eye, GlanceTrail, Point, Sight, explain_sight, judge_with_sight
80
+ from .look import CallableLMM, OfflineLMM, backend_name as sight_backend
81
+ from .look import default_lmm, next_point, scan, visual_cue
82
+ from .look import load_lmm
83
+
84
+
85
+ def judge(text: str) -> Verdict:
86
+ """把**整段**文本当成一条陈述来判(不分句)。
87
+
88
+ 与 `assess` 的区别:`assess` 会逐句判定再汇总;`judge` 不分句,
89
+ 适合你已经知道输入就是一句话的场景(比如校验单条文案)。
90
+ """
91
+ from .core.pipeline import judge_claim as _judge_claim
92
+ from .core.schema import Claim as _Claim
93
+ from .core.text import normalize as _normalize
94
+
95
+ clean = _normalize(text)
96
+ return _judge_claim(_Claim(text=clean, start=0, end=len(clean)))
97
+
98
+
99
+ def look_at(image, **kwargs) -> Sight:
100
+ """`scan` 的别名:看一张图,返回注视序列与下一个点。"""
101
+ return scan(image, **kwargs)
102
+
103
+
104
+ __all__ = [
105
+ "__version__",
106
+ # 依赖
107
+ "deps",
108
+ # 猜与死
109
+ "Colony",
110
+ "Neuron",
111
+ "Prediction",
112
+ "RoundResult",
113
+ "SolveReport",
114
+ "DeathPolicy",
115
+ "LifeState",
116
+ "Tombstone",
117
+ "make_residue",
118
+ # 图案
119
+ "patterns",
120
+ "special",
121
+ "pool",
122
+ "KINDS",
123
+ "EvolvingPool",
124
+ # 步骤
125
+ "Step",
126
+ "Sequence",
127
+ "StepSequence",
128
+ "Context",
129
+ "Policy",
130
+ "Trace",
131
+ "Event",
132
+ # 真伪
133
+ "Source",
134
+ "TruthVerdict",
135
+ "TruthLabel",
136
+ "cross_validate",
137
+ "KeywordJudge",
138
+ "SourceJudge",
139
+ "Questioner",
140
+ "Question",
141
+ "Resolver",
142
+ # 沉淀
143
+ "Graph",
144
+ "Fact",
145
+ "Edge",
146
+ "settle",
147
+ "Settlement",
148
+ # 表格
149
+ "Store",
150
+ "rounds_frame",
151
+ "neurons_frame",
152
+ "verdicts_frame",
153
+ # 文本判定
154
+ "assess",
155
+ "judge",
156
+ "judge_claim",
157
+ "speak",
158
+ "available_judges",
159
+ "Label",
160
+ "Claim",
161
+ "Cue",
162
+ "Verdict",
163
+ "Assessment",
164
+ # 看图片
165
+ "look",
166
+ "look_at",
167
+ "next_point",
168
+ "scan",
169
+ "explain_sight",
170
+ "judge_with_sight",
171
+ "visual_cue",
172
+ "sight_backend",
173
+ "Sight",
174
+ "Point",
175
+ "Eye",
176
+ "GlanceTrail",
177
+ "OfflineLMM",
178
+ "CallableLMM",
179
+ "load_lmm",
180
+ "default_lmm",
181
+ # 语法与句法
182
+ "grammar",
183
+ "syntax",
184
+ "GrammarJudge",
185
+ "judges_with_grammar",
186
+ "SyntaxProfile",
187
+ "analyze_syntax",
188
+ # 批处理与相似度
189
+ "batch",
190
+ "bulk_judge",
191
+ "judge_jsonl",
192
+ "similarity",
193
+ "jaccard",
194
+ "cosine",
195
+ "dedupe",
196
+ "most_similar",
197
+ # 报告
198
+ "report",
199
+ "markdown_report",
200
+ "text_report",
201
+ "syntax_report",
202
+ "grammar_report",
203
+ # 质量评估
204
+ "quality",
205
+ "evaluate_quality",
206
+ "quality_report",
207
+ ]
infx/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ """支持 `python -m infx "文本"`。"""
2
+
3
+ from .cli import main
4
+
5
+ raise SystemExit(main())
infx/batch.py ADDED
@@ -0,0 +1,82 @@
1
+ """批量处理:列表、JSONL、文件。
2
+
3
+ 单条判定是 `assess`;这里解决"几千条怎么办":
4
+ - `bulk_judge` 列表进、记录出
5
+ - `judge_jsonl` 文件进、文件出(每行一条,UTF-8)
6
+
7
+ 不并发。理由是判定是纯 CPU 的毫秒级操作,加线程池的复杂度大于收益;
8
+ 真要并行,调用方用 `multiprocessing` 一行就能包住 `bulk_judge`。
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ import json
14
+ from pathlib import Path
15
+ from typing import Any, Iterable, Sequence
16
+
17
+ from .core.errors import InferenceError
18
+ from .core.pipeline import assess
19
+
20
+
21
+ def bulk_judge(
22
+ texts: Iterable[str],
23
+ *,
24
+ judges: Sequence[Any] | None = None,
25
+ with_syntax: bool = False,
26
+ keep_verdicts: bool = True,
27
+ ) -> list[dict[str, Any]]:
28
+ """批量判定。返回每条一行(内含 `to_dict()` 的结果)。"""
29
+ from .syntax import analyze
30
+
31
+ out: list[dict[str, Any]] = []
32
+ for i, text in enumerate(texts):
33
+ row: dict[str, Any] = {"index": i, "text": text}
34
+ try:
35
+ result = assess(text, judges=judges) if judges else assess(text)
36
+ except InferenceError as exc:
37
+ row.update({"ok": False, "error": str(exc)})
38
+ out.append(row)
39
+ continue
40
+ payload = result.to_dict()
41
+ row.update({"ok": True, "label": payload["label"], "confidence": payload["confidence"]})
42
+ if keep_verdicts:
43
+ row["verdicts"] = payload["verdicts"]
44
+ if with_syntax:
45
+ row["syntax"] = analyze(text).to_dict()
46
+ out.append(row)
47
+ return out
48
+
49
+
50
+ def judge_jsonl(
51
+ source: str | Path,
52
+ target: str | Path | None = None,
53
+ *,
54
+ text_key: str = "text",
55
+ judges: Sequence[Any] | None = None,
56
+ with_syntax: bool = False,
57
+ ) -> list[dict[str, Any]]:
58
+ """读 JSONL → 判定 → (可选)写 JSONL。返回记录列表。"""
59
+ src = Path(source)
60
+ rows: list[dict[str, Any]] = []
61
+ texts: list[str] = []
62
+ with src.open("r", encoding="utf-8") as fh:
63
+ for line in fh:
64
+ line = line.strip()
65
+ if not line:
66
+ continue
67
+ obj = json.loads(line)
68
+ rows.append(obj)
69
+ texts.append(str(obj.get(text_key, "")))
70
+
71
+ judged = bulk_judge(texts, judges=judges, with_syntax=with_syntax)
72
+ merged = [{**row, "judgment": j} for row, j in zip(rows, judged)]
73
+
74
+ if target is not None:
75
+ dst = Path(target)
76
+ with dst.open("w", encoding="utf-8") as fh:
77
+ for row in merged:
78
+ fh.write(json.dumps(row, ensure_ascii=False) + "\n")
79
+ return merged
80
+
81
+
82
+ __all__ = ["bulk_judge", "judge_jsonl"]
infx/cli.py ADDED
@@ -0,0 +1,86 @@
1
+ """命令行入口:`inference "一句话"` / `python -m inference`。
2
+
3
+ 退出码约定(便于接 CI):
4
+ 0 正常
5
+ 1 命中了 `--fail-on` 指定的标签
6
+ 2 输入有问题(太短、文件不存在等)
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import argparse
12
+ import json
13
+ import sys
14
+ from pathlib import Path
15
+
16
+ from . import __version__, assess, speak
17
+ from .core.errors import InferenceError
18
+
19
+
20
+ def build_parser() -> argparse.ArgumentParser:
21
+ p = argparse.ArgumentParser(
22
+ prog="inference",
23
+ description="判定文本是事实还是观点,并把结论说清楚",
24
+ )
25
+ p.add_argument("text", nargs="?", help="要判定的文本;省略则从 --file 或 stdin 读")
26
+ p.add_argument("-f", "--file", help="从文件读取(UTF-8)")
27
+ p.add_argument(
28
+ "-s", "--style", default="brief", choices=["brief", "report"],
29
+ help="表述风格:brief 一句话(默认)/ report 完整报告",
30
+ )
31
+ p.add_argument("--json", action="store_true", help="输出 JSON(含逐句与线索)")
32
+ p.add_argument("--lang", choices=["zh", "en"], help="输出语言;省略则跟随文本")
33
+ p.add_argument(
34
+ "--fail-on", choices=["fact", "opinion", "mixed", "unknown"],
35
+ help="当整体判定等于该标签时以退出码 1 结束(CI 用)",
36
+ )
37
+ p.add_argument(
38
+ "--grammar", action="store_true",
39
+ help="启用语法规则库(几百条规则)作为额外判定器",
40
+ )
41
+ p.add_argument("--version", action="version", version=f"inference {__version__}")
42
+ return p
43
+
44
+
45
+ def _read_input(args: argparse.Namespace) -> str:
46
+ if args.text:
47
+ return args.text
48
+ if args.file:
49
+ return Path(args.file).read_text(encoding="utf-8")
50
+ if not sys.stdin.isatty():
51
+ return sys.stdin.read()
52
+ raise SystemExit("✘ 没有输入。给一段文本、用 --file,或通过管道传入。")
53
+
54
+
55
+ def main(argv: list[str] | None = None) -> int:
56
+ try: # Windows 控制台默认 cp936,强制 UTF-8 以免中文报错
57
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
58
+ except Exception:
59
+ pass
60
+
61
+ args = build_parser().parse_args(argv)
62
+ text = _read_input(args)
63
+
64
+ try:
65
+ if args.grammar:
66
+ from .grammar import judges_with_grammar
67
+
68
+ result = assess(text, judges=judges_with_grammar())
69
+ else:
70
+ result = assess(text)
71
+ except InferenceError as exc:
72
+ print(f"✘ {exc}", file=sys.stderr)
73
+ return 2
74
+
75
+ if args.json:
76
+ print(json.dumps(result.to_dict(), ensure_ascii=False, indent=2))
77
+ else:
78
+ print(speak(result, style=args.style, lang=args.lang))
79
+
80
+ if args.fail_on and result.label.value == args.fail_on:
81
+ return 1
82
+ return 0
83
+
84
+
85
+ if __name__ == "__main__": # pragma: no cover
86
+ raise SystemExit(main())