_api_extract.nx source
↩ module page · 50 lines · 2918 B
1// _api_extract.nx -- SOVEREIGN structured extraction over an API JSON response.
2// The apifetch.js TOOL did HTTP transport; this drives the TEAM's json.nx pull-parser to extract
3// fields MECHANICALLY (no semantic LLM residual -- structured data is the mechanistic ideal).
4// Extracts meta.count + the first result's display_name + publication_year from the OpenAlex response.
5import "json.nx"
6
7func ax_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func ax_wn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(1,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
9// exact-match a json key slice src[start..start+len) against a NUL-terminated target
10func ax_key_is(src: *u8, start: i64, len: i64, target: *u8) -> i64 {
11 var i: i64 = 0
12 while i < len { if src[start+i] != target[i] { return 0 } i = i + 1 }
13 if target[len] != (0 as u8) { return 0 }
14 return 1
15}
16
17func main() -> i64 {
18 let buf: *u8 = sys_mmap(131072)
19 let fd: i64 = sys_openat_rd("knowledge/fetched/openalex_webdev.json" as *u8)
20 if fd < 0 { ax_w("no api json\n" as *u8); sys_exit(1); return 1 }
21 var n: i64 = 0
22 var r: i64 = sys_read(fd, buf, 131071)
23 while r > 0 { n = n + r; if n >= 131071 { r = 0 } else { r = sys_read(fd, (buf as i64 + n) as *u8, 131071 - n) } }
24 sys_close(fd)
25 let p: *JsonParser = json_new(buf, n)
26 var ev: i64 = json_next(p)
27 var want: i64 = 0
28 var got_count: i64 = 0
29 var got_title: i64 = 0
30 var got_year: i64 = 0
31 ax_w("NISHI-JSON-EXTRACT (sovereign json.nx pull-parser) over the API response:\n" as *u8)
32 while ev != JSON_END {
33 if ev == JSON_KEY {
34 if got_count == 0 { if ax_key_is(p.src, p.str_start, p.str_len, "count" as *u8) == 1 { want = 1 } }
35 if got_title == 0 { if ax_key_is(p.src, p.str_start, p.str_len, "display_name" as *u8) == 1 { want = 2 } }
36 if got_year == 0 { if ax_key_is(p.src, p.str_start, p.str_len, "publication_year" as *u8) == 1 { want = 3 } }
37 }
38 if ev == JSON_INT {
39 if want == 1 { ax_w(" works_matching (meta.count) = " as *u8); ax_wn(p.int_val); ax_w("\n" as *u8); got_count = 1; want = 0 }
40 if want == 3 { ax_w(" first_result.publication_year = " as *u8); ax_wn(p.int_val); ax_w("\n" as *u8); got_year = 1; want = 0 }
41 }
42 if ev == JSON_STRING {
43 if want == 2 { ax_w(" first_result.display_name = \"" as *u8); sys_write(1, (p.src as i64 + p.str_start) as *u8, p.str_len); ax_w("\"\n" as *u8); got_title = 1; want = 0 }
44 }
45 ev = json_next(p)
46 }
47 ax_w(" (structured fields extracted MECHANICALLY -- zero semantic LLM residual = the API advantage)\n" as *u8)
48 sys_exit(0)
49 return 0
50}