OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / mvp / viz / patch-senses.py

78 lines · python ref:
#!/usr/bin/env python3
"""Patch data.json with L1 sense counts from CogServer."""
import json
import socket

def query_cogserver(cmd):
    """Send Scheme command to CogServer, return response."""
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(('localhost', 17001))
    s.recv(1024)  # banner
    s.sendall(b'scm\n')
    s.recv(1024)  # scheme prompt
    s.sendall(cmd.encode() + b'\n')
    response = b''
    s.settimeout(10)
    try:
        while True:
            chunk = s.recv(65536)
            if not chunk:
                break
            response += chunk
            if b'guile>' in response:
                break
    except socket.timeout:
        pass
    s.close()
    return response.decode(errors='replace')

# Get sense counts as Scheme alist
print("Querying CogServer for L1 sense counts...")
resp = query_cogserver(
    '(let ((out (list))) '
    '(hash-for-each (lambda (w s) '
    '(set! out (cons (cons w (length s)) out))) '
    '*cl-l1-word-senses*) '
    '(format #t "~a~%" (length out)) '
    '(for-each (lambda (e) (format #t "~a\\t~a~%" (car e) (cdr e))) out))'
)

# Parse sense counts from response
sense_map = {}
lines = resp.strip().split('\n')
for line in lines:
    line = line.strip()
    if '\t' in line:
        parts = line.split('\t')
        if len(parts) == 2:
            try:
                word, count = parts[0], int(parts[1])
                sense_map[word] = count
            except ValueError:
                pass

print(f"Got {len(sense_map)} words with senses")
if sense_map:
    top = sorted(sense_map.items(), key=lambda x: x[1], reverse=True)[:15]
    for w, c in top:
        print(f"  {w}: {c} senses")

# Patch data.json
with open('data.json') as f:
    data = json.load(f)

patched = 0
for word, info in data['words'].items():
    if word in sense_map:
        info['senses'] = sense_map[word]
        patched += 1

# Update L1 stats
data['stats']['l1Senses'] = len(sense_map)

with open('data.json', 'w') as f:
    json.dump(data, f)

print(f"Patched {patched} words with sense data")
print(f"Total L1 senses: {len(sense_map)}")