OverviewFilesCommitsIssuesDocsReleasesPeers

hypercore-kb / opencog-kb / language-learning / layers / atomspace-search.scm

426 lines · scm ref:
;
; atomspace-search.scm
;
; How to store structures in AtomSpace and SEARCH for
; disambiguating context when vagueness is high.
;
; Key insight: REVERSE PMI QUERY
;   Forward: context → which sense?
;   Reverse: ambiguous word → what context would disambiguate?
;

(use-modules (opencog) (opencog exec))
(use-modules (srfi srfi-1))
(use-modules (ice-9 format))

(display "═══════════════════════════════════════════════════════════════════\n")
(display "   ATOMSPACE STORAGE & CONTEXT SEARCH                              \n")
(display "═══════════════════════════════════════════════════════════════════\n\n")

;; ═══════════════════════════════════════════════════════════════════
;; PART 1: STORING STRUCTURES IN ATOMSPACE
;; ═══════════════════════════════════════════════════════════════════

(display "PART 1: Storing Structures in AtomSpace\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

;; 1.1 Word Senses
(display "1.1 Word Senses (polysemy)\n\n")

;; Define word senses as ConceptNodes
(define bank-financial (Concept "bank#financial"))
(define bank-geographic (Concept "bank#geographic"))
(define bank-word (Word "bank"))

;; Link word to its senses with prior probabilities
(Evaluation
  (Predicate "has-sense")
  (List bank-word bank-financial (Number 0.75)))

(Evaluation
  (Predicate "has-sense")
  (List bank-word bank-geographic (Number 0.20)))

(display "  (Evaluation (Predicate \"has-sense\")\n")
(display "    (List (Word \"bank\") (Concept \"bank#financial\") (Number 0.75)))\n\n")

;; 1.2 Frame membership
(display "1.2 Frame Membership\n\n")

(define financial-frame (Concept "FINANCIAL-TRANSACTION"))
(define geographic-frame (Concept "GEOGRAPHIC-WATER"))

;; Words belong to frames
(Member (Word "money") financial-frame)
(Member (Word "deposit") financial-frame)
(Member (Word "withdraw") financial-frame)
(Member (Word "account") financial-frame)
(Member (Word "ATM") financial-frame)
(Member bank-financial financial-frame)

(Member (Word "river") geographic-frame)
(Member (Word "water") geographic-frame)
(Member (Word "shore") geographic-frame)
(Member (Word "swim") geographic-frame)
(Member bank-geographic geographic-frame)

(display "  (Member (Word \"money\") (Concept \"FINANCIAL-TRANSACTION\"))\n")
(display "  (Member (Word \"river\") (Concept \"GEOGRAPHIC-WATER\"))\n")
(display "  (Member (Concept \"bank#financial\") (Concept \"FINANCIAL-TRANSACTION\"))\n\n")

;; 1.3 PMI values between words and senses
(display "1.3 PMI Values (word-sense associations)\n\n")

;; High PMI = strong association
(Evaluation
  (Predicate "pmi")
  (List (Word "money") bank-financial (Number 5.2)))

(Evaluation
  (Predicate "pmi")
  (List (Word "deposit") bank-financial (Number 4.8)))

(Evaluation
  (Predicate "pmi")
  (List (Word "river") bank-geographic (Number 4.5)))

(Evaluation
  (Predicate "pmi")
  (List (Word "swim") bank-geographic (Number 3.9)))

;; Low/negative PMI = weak association
(Evaluation
  (Predicate "pmi")
  (List (Word "money") bank-geographic (Number -2.0)))

(Evaluation
  (Predicate "pmi")
  (List (Word "river") bank-financial (Number -1.5)))

(display "  (Evaluation (Predicate \"pmi\")\n")
(display "    (List (Word \"money\") (Concept \"bank#financial\") (Number 5.2)))\n")
(display "  (Evaluation (Predicate \"pmi\")\n")
(display "    (List (Word \"river\") (Concept \"bank#geographic\") (Number 4.5)))\n\n")

;; 1.4 Agent priors
(display "1.4 Agent-Type Priors\n\n")

(define penguin-type (Concept "agent-type#penguin"))
(define human-type (Concept "agent-type#human"))

(Evaluation
  (Predicate "typical-action")
  (List penguin-type (Word "swim") (Number 0.85)))

(Evaluation
  (Predicate "typical-location")
  (List penguin-type geographic-frame (Number 0.90)))

(Evaluation
  (Predicate "typical-location")
  (List penguin-type financial-frame (Number 0.001)))

(Evaluation
  (Predicate "typical-action")
  (List human-type (Word "walk") (Number 0.95)))

(Evaluation
  (Predicate "typical-location")
  (List human-type financial-frame (Number 0.70)))

(display "  (Evaluation (Predicate \"typical-location\")\n")
(display "    (List (Concept \"agent-type#penguin\")\n")
(display "          (Concept \"GEOGRAPHIC-WATER\") (Number 0.90)))\n\n")

;; ═══════════════════════════════════════════════════════════════════
;; PART 2: THE REVERSE PMI QUERY - Finding Discriminating Context
;; ═══════════════════════════════════════════════════════════════════

(display "═══════════════════════════════════════════════════════════════════\n")
(display "PART 2: Reverse PMI Query - What Would Disambiguate?\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

(display "THE KEY INSIGHT:\n\n")

(display "  Forward query:  context + ambiguous_word → sense\n")
(display "  Reverse query:  ambiguous_word → discriminating_context\n\n")

(display "  For 'bank', we want words where:\n")
(display "    PMI(word, bank#financial) >> PMI(word, bank#geographic)\n")
(display "  OR\n")
(display "    PMI(word, bank#geographic) >> PMI(word, bank#financial)\n\n")

(display "  These are DISCRIMINATING WORDS - they tell the senses apart.\n\n")

;; Define discriminating power
;; D(word) = |PMI(word, sense1) - PMI(word, sense2)|

(display "DISCRIMINATING POWER:\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

(display "  Word      │ PMI(fin) │ PMI(geo) │ D-power │ Points to\n")
(display "  ──────────┼──────────┼──────────┼─────────┼───────────\n")
(display "  money     │   5.2    │  -2.0    │   7.2   │ FINANCIAL\n")
(display "  deposit   │   4.8    │  -1.5    │   6.3   │ FINANCIAL\n")
(display "  ATM       │   5.5    │  -3.0    │   8.5   │ FINANCIAL\n")
(display "  river     │  -1.5    │   4.5    │   6.0   │ GEOGRAPHIC\n")
(display "  swim      │  -2.0    │   3.9    │   5.9   │ GEOGRAPHIC\n")
(display "  water     │  -0.5    │   4.2    │   4.7   │ GEOGRAPHIC\n")
(display "  walked    │   0.5    │   0.3    │   0.2   │ (neutral)\n")
(display "  went      │   0.2    │   0.1    │   0.1   │ (neutral)\n\n")

;; ═══════════════════════════════════════════════════════════════════
;; PART 3: THE SEARCH ALGORITHM
;; ═══════════════════════════════════════════════════════════════════

(display "═══════════════════════════════════════════════════════════════════\n")
(display "PART 3: The Context Search Algorithm\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

(display "ALGORITHM: search-for-disambiguation\n\n")

(display "  INPUT: sentence with ambiguous word W\n")
(display "  OUTPUT: disambiguated sense OR request for more context\n\n")

(display "  Step 1: IDENTIFY ambiguity\n")
(display "    - Parse sentence, find W has multiple senses\n")
(display "    - Compute vagueness score\n")
(display "    - If vagueness > threshold → need to search\n\n")

(display "  Step 2: QUERY for discriminating words\n")
(display "    - For each sense S of W:\n")
(display "        Find words with high PMI to S\n")
(display "    - Compute discriminating power D for each\n")
(display "    - Rank by D (highest = most useful for disambiguation)\n\n")

(display "  Step 3: SEARCH context for discriminating words\n")
(display "    - Look in surrounding sentences (window = ±3)\n")
(display "    - Look in document metadata (title, topic)\n")
(display "    - Look in conversation history\n\n")

(display "  Step 4: AGGREGATE evidence from found words\n")
(display "    - For each discriminating word found:\n")
(display "        Add PMI(word, sense) to sense's score\n")
(display "    - Compare scores for each sense\n\n")

(display "  Step 5: DECIDE\n")
(display "    - If one sense dominates → return that sense\n")
(display "    - If still ambiguous → generate clarifying question\n\n")

;; ═══════════════════════════════════════════════════════════════════
;; PART 4: ATOMSPACE PATTERN MATCHING FOR SEARCH
;; ═══════════════════════════════════════════════════════════════════

(display "═══════════════════════════════════════════════════════════════════\n")
(display "PART 4: AtomSpace Pattern Matching for Search\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

(display "Query 1: Find all senses of 'bank'\n")
(display "──────────────────────────────────────\n\n")

(display "  (Get\n")
(display "    (TypedVariable (Variable \"$sense\") (Type 'Concept))\n")
(display "    (Evaluation\n")
(display "      (Predicate \"has-sense\")\n")
(display "      (List (Word \"bank\") (Variable \"$sense\") (Variable \"$prob\"))))\n\n")

;; Actually execute this query
(define senses-query
  (Get
    (VariableList
      (TypedVariable (Variable "$sense") (Type 'ConceptNode))
      (Variable "$prob"))
    (Evaluation
      (Predicate "has-sense")
      (List (Word "bank") (Variable "$sense") (Variable "$prob")))))

(display "  Result: ")
(display (cog-execute! senses-query))
(newline)(newline)

(display "Query 2: Find words with high PMI to bank#financial\n")
(display "──────────────────────────────────────────────────────\n\n")

(display "  (Get\n")
(display "    (TypedVariable (Variable \"$word\") (Type 'Word))\n")
(display "    (And\n")
(display "      (Evaluation (Predicate \"pmi\")\n")
(display "        (List (Variable \"$word\") (Concept \"bank#financial\")\n")
(display "              (Variable \"$pmi\")))\n")
(display "      (GreaterThan (Variable \"$pmi\") (Number 3.0))))\n\n")

(display "  This finds all words strongly associated with FINANCIAL sense.\n\n")

(display "Query 3: Find discriminating words (high D-power)\n")
(display "──────────────────────────────────────────────────────\n\n")

(display "  For each word W in vocabulary:\n")
(display "    pmi_fin = lookup PMI(W, bank#financial)\n")
(display "    pmi_geo = lookup PMI(W, bank#geographic)\n")
(display "    D = |pmi_fin - pmi_geo|\n")
(display "    If D > threshold: W is discriminating\n\n")

(display "  Implementation in Atomese:\n")
(display "  (Filter\n")
(display "    (Rule\n")
(display "      (Variable \"$word\")\n")
(display "      (GreaterThan\n")
(display "        (Abs (Minus\n")
(display "          (ValueOf ... pmi to sense1)\n")
(display "          (ValueOf ... pmi to sense2)))\n")
(display "        (Number 3.0))\n")
(display "      (Variable \"$word\"))  ; return the word\n")
(display "    all-words)\n\n")

;; ═══════════════════════════════════════════════════════════════════
;; PART 5: WORKED EXAMPLE
;; ═══════════════════════════════════════════════════════════════════

(display "═══════════════════════════════════════════════════════════════════\n")
(display "PART 5: Worked Example\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

(display "INPUT: 'I went to the bank.'\n")
(display "       Vagueness = 0.50 (HIGH) → need to search\n\n")

(display "STEP 2: Query for discriminating words\n")
(display "────────────────────────────────────────\n")
(display "  Top discriminators for 'bank':\n")
(display "    1. ATM (D=8.5) → FINANCIAL\n")
(display "    2. money (D=7.2) → FINANCIAL\n")
(display "    3. deposit (D=6.3) → FINANCIAL\n")
(display "    4. river (D=6.0) → GEOGRAPHIC\n")
(display "    5. swim (D=5.9) → GEOGRAPHIC\n")
(display "    6. water (D=4.7) → GEOGRAPHIC\n\n")

(display "STEP 3: Search surrounding context\n")
(display "────────────────────────────────────────\n")
(display "  Previous sentences:\n")
(display "    'We had been hiking along the river all morning.'\n")
(display "    'The sun was getting hot.'\n\n")
(display "  Next sentences:\n")
(display "    'I dipped my feet in the water.'\n")
(display "    'It was refreshingly cool.'\n\n")
(display "  FOUND DISCRIMINATORS:\n")
(display "    ✓ 'river' (D=6.0, → GEOGRAPHIC)\n")
(display "    ✓ 'water' (D=4.7, → GEOGRAPHIC)\n")
(display "    ✗ 'money' - not found\n")
(display "    ✗ 'ATM' - not found\n\n")

(display "STEP 4: Aggregate evidence\n")
(display "────────────────────────────────────────\n")
(display "  FINANCIAL evidence: 0\n")
(display "  GEOGRAPHIC evidence: PMI(river,geo) + PMI(water,geo)\n")
(display "                     = 4.5 + 4.2 = 8.7\n\n")

(display "STEP 5: Decide\n")
(display "────────────────────────────────────────\n")
(display "  GEOGRAPHIC (8.7) >> FINANCIAL (0)\n\n")

(display "  ┌─────────────────────────────────────────────────────────────┐\n")
(display "  │  RESULT: bank = GEOGRAPHIC (river bank)                    │\n")
(display "  │                                                             │\n")
(display "  │  Context search found 'river' and 'water' nearby,          │\n")
(display "  │  which strongly discriminate toward GEOGRAPHIC sense.       │\n")
(display "  └─────────────────────────────────────────────────────────────┘\n\n")

;; ═══════════════════════════════════════════════════════════════════
;; PART 6: WHAT IF SEARCH FAILS?
;; ═══════════════════════════════════════════════════════════════════

(display "═══════════════════════════════════════════════════════════════════\n")
(display "PART 6: What If Context Search Fails?\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

(display "SCENARIO: No discriminating words found in context\n\n")

(display "  INPUT: 'I went to the bank.'\n")
(display "  CONTEXT: 'It was Tuesday. I went to the bank. Then I went home.'\n\n")
(display "  Search result: No discriminating words found!\n\n")

(display "OPTIONS:\n\n")

(display "  Option A: Use base frequency prior\n")
(display "  ──────────────────────────────────────\n")
(display "    P(FINANCIAL) = 0.75, P(GEOGRAPHIC) = 0.20\n")
(display "    → Guess FINANCIAL (with low confidence)\n")
(display "    → Flag uncertainty for downstream processing\n\n")

(display "  Option B: Generate clarifying question\n")
(display "  ──────────────────────────────────────\n")
(display "    Template: 'When you mentioned [WORD], did you mean\n")
(display "               [SENSE_A description] or [SENSE_B description]?'\n\n")
(display "    Generated: 'When you mentioned the bank, did you mean\n")
(display "                a financial institution or a riverbank?'\n\n")

(display "  Option C: Maintain ambiguity\n")
(display "  ──────────────────────────────────────\n")
(display "    Store both interpretations:\n")
(display "    (Or\n")
(display "      (Interpretation sentence bank#financial 0.75)\n")
(display "      (Interpretation sentence bank#geographic 0.20))\n\n")
(display "    Resolve later when more context arrives.\n\n")

(display "  Option D: Search broader context\n")
(display "  ──────────────────────────────────────\n")
(display "    - Document title/topic\n")
(display "    - User profile (do they usually talk about finance?)\n")
(display "    - Time context (is it tax season?)\n")
(display "    - Location context (are they near a river?)\n\n")

;; ═══════════════════════════════════════════════════════════════════
;; PART 7: THE FULL SEARCH PIPELINE IN ATOMESE
;; ═══════════════════════════════════════════════════════════════════

(display "═══════════════════════════════════════════════════════════════════\n")
(display "PART 7: Full Search Pipeline (Pseudocode Atomese)\n")
(display "─────────────────────────────────────────────────────────────────\n\n")

(display "; 1. Store current sentence for analysis\n")
(display "(State (Anchor \"current-sentence\")\n")
(display "  (Sentence \"I went to the bank.\"))\n\n")

(display "; 2. Store surrounding context\n")
(display "(State (Anchor \"context-window\")\n")
(display "  (List\n")
(display "    (Sentence \"We had been hiking along the river.\")\n")
(display "    (Sentence \"I went to the bank.\")\n")
(display "    (Sentence \"I dipped my feet in the water.\")))\n\n")

(display "; 3. Define disambiguation rule\n")
(display "(Define (DefinedSchema \"disambiguate\")\n")
(display "  (Lambda (Variable \"$word\")\n")
(display "    (SequentialAnd\n")
(display "      ; Get senses of word\n")
(display "      (Put (State (Anchor \"senses\")\n")
(display "        (Get ... senses of $word)))\n")
(display "      \n")
(display "      ; Get discriminating words for each sense\n")
(display "      (Put (State (Anchor \"discriminators\")\n")
(display "        (Get ... high D-power words)))\n")
(display "      \n")
(display "      ; Search context for discriminators\n")
(display "      (Put (State (Anchor \"found\")\n")
(display "        (Filter\n")
(display "          (Member (Variable \"$w\") context-words)\n")
(display "          discriminators)))\n")
(display "      \n")
(display "      ; Aggregate evidence\n")
(display "      (Put (State (Anchor \"evidence\")\n")
(display "        (Fold + 0 (Map pmi-lookup found))))\n")
(display "      \n")
(display "      ; Return best sense or ask-for-clarification\n")
(display "      (Cond\n")
(display "        ((GreaterThan evidence threshold)\n")
(display "          (ArgMax evidence senses))\n")
(display "        (True\n")
(display "          (ExecutionOutput\n")
(display "            (DefinedSchema \"generate-question\")\n")
(display "            (Variable \"$word\")))))))\n\n")

(display "; 4. Execute disambiguation\n")
(display "(cog-execute!\n")
(display "  (ExecutionOutput\n")
(display "    (DefinedSchema \"disambiguate\")\n")
(display "    (Word \"bank\")))\n\n")