;
; common-sense-discovery.scm
;
; Common sense isn't innate - humans LEARN it by observing patterns.
; The same counting method discovers common sense from text.
;
; What IS common sense? Expectations about:
; - What objects do
; - What agents do
; - What causes what
; - What's typical vs surprising
;
; AUDIT NOTE (2026-02-03): The six extraction categories
; (AGENT-ACTION, OBJECT-PROPERTY, CAUSE-EFFECT, SEQUENCE,
; LOCATION, CAPABILITY) are analytical labels for human readers.
; The algorithm itself discovers patterns along whatever dimensions
; the data provides -- it doesn't pre-label categories.
;
; In this version, we COMPUTE associations from the corpus using
; the shared counting infrastructure. The categories are shown as
; interpretive lenses on what the clusters reveal, not as inputs
; to the algorithm.
;
(use-modules (srfi srfi-1))
(use-modules (srfi srfi-69))
(use-modules (ice-9 format))
;; Load shared infrastructure
(load "../lib/counting.scm")
(display "═══════════════════════════════════════════════════════════════════\n")
(display " COMMON SENSE DISCOVERY FROM CORPUS \n")
(display " (Patterns computed, categories are interpretive labels) \n")
(display "═══════════════════════════════════════════════════════════════════\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; WHAT IS COMMON SENSE?
;; ═══════════════════════════════════════════════════════════════════
(display "WHAT IS COMMON SENSE?\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " Common sense is a collection of EXPECTATIONS:\n\n")
(display " - Objects have typical PROPERTIES\n")
(display " 'water is wet', 'fire is hot'\n\n")
(display " - Agents have typical CAPABILITIES\n")
(display " 'fish swim', 'birds fly', 'humans use tools'\n\n")
(display " - Actions have typical PRECONDITIONS and EFFECTS\n")
(display " 'eating reduces hunger', 'swimming makes you wet'\n\n")
(display " - Events have typical SEQUENCES\n")
(display " 'cook -> eat -> clean', 'work -> get paid -> spend'\n\n")
(display " How do humans learn this? BY COUNTING PATTERNS.\n")
(display " Same method as language learning!\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; CORPUS FOR COMMON SENSE EXTRACTION
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "CORPUS FOR COMMON SENSE EXTRACTION\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(define corpus '(
;; Agent-action patterns
"the fish swam in the river"
"the bird flew to the tree"
"the dog ran to the park"
"the cat climbed the fence"
"the human walked to the store"
"the chef cooked the meal"
"the customer paid for the food"
"the fisherman caught a fish"
;; Object-property patterns
"the water was cold"
"the fire was hot"
"the ice was frozen"
"the river was deep"
"the bank was muddy"
"the food was delicious"
"the money was valuable"
;; Cause-effect patterns
"he was hungry so he ate"
"she was tired so she slept"
"it rained so the ground was wet"
"he studied so he passed the test"
"the fire burned so the wood was gone"
"she saved money so she could buy a house"
;; Sequence patterns
"first he cooked then he ate"
"she woke up then she got dressed"
"they worked then they got paid"
"he caught a fish then he cooked it"
"she went to the bank then she withdrew money"
"the ice melted then the water flowed"
;; Location patterns
"fish live in water"
"birds live in trees"
"money is kept in banks"
"food is stored in refrigerators"
"people work in offices"
"fishermen work near rivers"
;; Capability patterns
"fish can swim but cannot walk"
"birds can fly but cannot swim deep"
"humans can walk and swim and think"
"fire can burn but cannot move itself"
"water can flow and freeze and evaporate"
))
(format #t " Corpus: ~a sentences describing world patterns\n\n" (length corpus))
(for-each (lambda (s) (format #t " ~a\n" s)) (take corpus 8))
(display " ...\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; COMPUTE STATISTICS FROM CORPUS
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "COMPUTING STATISTICS (all from corpus)\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(define counts (count-cooccurrences corpus 2))
(define word-counts (car counts))
(define pair-counts (cdr counts))
;; Discover clusters
(define all-clusters
(cluster-by-pmi-similarity word-counts pair-counts 0.08))
(define multi-clusters
(filter (lambda (c) (> (length c) 1)) all-clusters))
(define named-clusters
(name-clusters-by-medoid multi-clusters word-counts pair-counts))
(display " Discovered clusters:\n\n")
(display-clusters named-clusters)
(newline)
;; ═══════════════════════════════════════════════════════════════════
;; WHAT THE CLUSTERS REVEAL (interpretive analysis)
;;
;; The algorithm discovers co-occurrence clusters. We can INTERPRET
;; these clusters through different lenses (agent-action, object-
;; property, etc.) but the algorithm doesn't use those labels.
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "WHAT THE CLUSTERS REVEAL\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " The clusters contain multiple types of common sense:\n\n")
;; Show top PMI associations for key words
(define (show-top-associations word n)
(let* ((vec (pmi-vector word-counts pair-counts word))
(top (take vec (min n (length vec)))))
(format #t " '~a' most associated with: " word)
(format #t "~a\n"
(string-join
(map (lambda (pair)
(format #f "~a(~,1f)" (car pair) (cdr pair)))
top)
", "))))
(display " LENS 1: Agent-Action patterns (what does X typically do?)\n\n")
(for-each
(lambda (w) (show-top-associations w 4))
'("fish" "bird" "dog" "chef" "human"))
(newline)
(display " These associations ARE common sense:\n")
(display " 'fish' + 'swam' has high PMI -> fish typically swim\n")
(display " 'fish' + 'walked' has zero PMI -> fish don't walk\n")
(display " Computed, not assumed.\n\n")
(display " LENS 2: Object-Property patterns (what is X like?)\n\n")
(for-each
(lambda (w) (show-top-associations w 4))
'("water" "fire" "ice" "food" "money"))
(newline)
(display " These associations encode properties:\n")
(display " 'water' + 'cold' has high PMI -> water is typically cold\n")
(display " 'fire' + 'hot' has high PMI -> fire is typically hot\n\n")
(display " LENS 3: Cause-Effect patterns\n\n")
;; Words connected by 'so' in the corpus
(for-each
(lambda (w) (show-top-associations w 4))
'("hungry" "tired" "rained"))
(newline)
(display " 'hungry' + 'ate' has high PMI -> hunger causes eating\n")
(display " 'rained' + 'wet' has high PMI -> rain causes wetness\n\n")
(display " LENS 4: Location patterns (where is X found?)\n\n")
(for-each
(lambda (w) (show-top-associations w 4))
'("river" "trees" "banks"))
(newline)
(display " 'fish' + 'river' has high PMI -> fish are found in rivers\n")
(display " 'fisherman' + 'river' has high PMI -> fishermen are near rivers\n\n")
(display " IMPORTANT: These 'lenses' are for human interpretation.\n")
(display " The algorithm sees ONE thing: co-occurrence patterns.\n")
(display " Agent-action, object-property, cause-effect, location\n")
(display " are all just co-occurrence patterns at different scales.\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; HOW THIS HELPS DISAMBIGUATION
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "HOW COMMON SENSE HELPS DISAMBIGUATION (COMPUTED)\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " Sentence: 'The fisherman went to the bank'\n\n")
(display " Common sense knowledge (from computed associations):\n\n")
;; Show fisherman's associations
(display " Associations of 'fisherman':\n")
(show-top-associations "fisherman" 5)
(newline)
;; Show cluster evidence
(let ((evidence (compute-frame-evidence
"the fisherman went to the bank"
named-clusters word-counts pair-counts)))
(display " Cluster evidence for this sentence:\n")
(display-evidence evidence)
(newline)
(let* ((best (if (null? evidence) (cons "UNKNOWN" 0.0) (car evidence)))
(vagueness (compute-vagueness evidence)))
(format #t " Best cluster: [~a] (vagueness: ~,3f)\n\n" (car best) vagueness)))
(display " The 'fisherman' word activates water/river cluster associations\n")
(display " because fisherman co-occurs with river, water, fish in the corpus.\n")
(display " This IS common sense -- discovered from counting.\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; ANOMALY DETECTION via SURPRISE
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "ANOMALY DETECTION: Surprise = Low Pairwise PMI\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " Normal sentences: content words have high average pairwise PMI.\n")
(display " Anomalous sentences: word pairs are rarely/never seen together.\n")
(display " Unknown word pairs receive a penalty (they're absent from corpus).\n\n")
(define test-sentences '(
("the fish swam in the river" "normal")
("the bird flew to the tree" "normal")
("the chef cooked the meal" "normal")
("the fish walked to the store" "anomalous")
("the fire was cold" "anomalous")
("the fisherman went to the bank" "ambiguous")
))
(for-each
(lambda (test)
(let* ((sent (car test))
(expected (cadr test))
(result (detect-anomaly sent word-counts pair-counts))
(label (car result))
(avg-pmi (cdr result))
(coherence (sentence-coherence sent word-counts pair-counts))
(n-pairs (cdr coherence)))
(format #t " '~a'\n" sent)
(format #t " Avg pairwise PMI: ~,2f (~a pairs) -> ~a [expected: ~a]\n\n"
avg-pmi n-pairs label expected)))
test-sentences)
(display " The detection uses PAIRWISE PMI between content words:\n")
(display " 'fish' + 'swam' -> high PMI (co-occur in corpus)\n")
(display " 'fish' + 'walked' -> zero PMI, penalized -> low score\n")
(display " 'fire' + 'cold' -> weak/no association -> low score\n")
(display " Word pairs never seen together get a penalty, dragging\n")
(display " the average below zero = SURPRISING.\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; COMMON SENSE AS PREDICTION
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "COMMON SENSE = PREDICTION BASED ON COUNTED PATTERNS\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " Common sense lets you PREDICT:\n\n")
(display " Given 'The fisherman went to the bank', predict:\n")
(display " - What will he do next? (words with high PMI to fisherman cluster)\n")
(let ((vec (pmi-vector word-counts pair-counts "fisherman")))
(format #t " - Top associations: ~a\n"
(string-join
(map (lambda (p) (format #f "~a" (car p)))
(take vec (min 5 (length vec))))
", ")))
(newline)
(display " Given 'The customer went to the bank', predict:\n")
(display " - What will she do next? (words with high PMI to customer cluster)\n")
(let ((vec (pmi-vector word-counts pair-counts "customer")))
(format #t " - Top associations: ~a\n"
(string-join
(map (lambda (p) (format #f "~a" (car p)))
(take vec (min 5 (length vec))))
", ")))
(newline)
(display " The prediction comes from COUNTING what usually co-occurs:\n")
(display " P(next-word | context-cluster) learned from corpus\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; HOW HUMANS LEARN COMMON SENSE
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "HOW HUMANS LEARN COMMON SENSE\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " Humans learn common sense the SAME WAY:\n\n")
(display " 1. OBSERVATION\n")
(display " Child sees: fish swim, fish swim, fish swim...\n")
(display " Child NEVER sees: fish walk, fish fly\n\n")
(display " 2. COUNTING (implicit)\n")
(display " Brain tracks: fish + swim = many times\n")
(display " Brain tracks: fish + walk = zero times\n")
(display " This builds expectations\n\n")
(display " 3. PREDICTION\n")
(display " New situation: 'There's a fish'\n")
(display " Brain predicts: it will probably swim\n")
(display " Brain does NOT predict: it will walk\n\n")
(display " 4. SURPRISE WHEN WRONG\n")
(display " If fish somehow walks -> VERY surprising\n")
(display " Surprise = -log(probability)\n")
(display " Low probability -> high surprise\n\n")
(display " 5. UPDATE\n")
(display " If walking fish observed, update counts\n")
(display " Next time, slightly less surprising\n")
(display " But still rare, so still unusual\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; COMMON SENSE BEFORE TOOL USE
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "COMMON SENSE BEFORE TOOL USE\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " WITHOUT common sense:\n")
(display " Agent sees: 'The fish walked to the bank'\n")
(display " Agent thinks: 'bank' is ambiguous, need to search\n")
(display " Agent is confused: both senses appear in results\n\n")
(display " WITH common sense (from corpus statistics):\n")
(display " Agent sees: 'The fish walked to the bank'\n")
(display " Agent computes: PMI(fish, walked) is very low or zero\n")
(display " Agent concludes: This sentence is ANOMALOUS\n")
(display " Agent actions:\n")
(display " - Flag as unusual (possible error, metaphor, fiction)\n")
(display " - OR interpret 'walked' metaphorically\n")
(display " - OR ask for clarification\n")
(display " No search needed - common sense detected the problem\n\n")
(display " Common sense REDUCES the need for external queries.\n\n")
;; ═══════════════════════════════════════════════════════════════════
;; SUMMARY
;; ═══════════════════════════════════════════════════════════════════
(display "═══════════════════════════════════════════════════════════════════\n")
(display "SUMMARY: Common Sense Discovery\n")
(display "─────────────────────────────────────────────────────────────────\n\n")
(display " Common sense is NOT magical or innate.\n")
(display " It's LEARNED by counting patterns:\n\n")
(display " - What words co-occur -> expectations about the world\n")
(display " - High PMI = expected combination (common sense)\n")
(display " - Low PMI = unexpected (anomaly, metaphor, error)\n")
(display " - Clusters = categories of similar-behaving things\n\n")
(display " Same method as language learning:\n")
(display " COUNT -> CLUSTER -> STRUCTURE -> USE FOR PREDICTION\n\n")
(display " Human-interpretive categories like 'agent-action' or\n")
(display " 'cause-effect' are LENSES on the same underlying\n")
(display " co-occurrence patterns. The algorithm doesn't need them.\n\n")
(display " The agent needs common sense BEFORE tool use:\n")
(display " - To know WHAT to search for\n")
(display " - To know WHEN search is needed\n")
(display " - To evaluate search results\n")
(display " - To detect anomalies without searching\n\n")