neurological-regex / src /LinguisticUnderstanding.hs
SNAPKITTYWEST's picture
chore: push neurological-regex from SNAPKITTYWEST GitHub
0c6f0f2 verified
Raw History Blame Contribute Delete
2.03 kB
{-# LANGUAGE OverloadedStrings #-}
-- | SALC-enhanced linguistic understanding pipeline.
-- Tokenize → encode → SALC attend → constraint solve → output.
-- Author: Ahmad Ali Parr · Trust: Bel Esprit D'Accord Irrevocable Trust
module LinguisticUnderstanding
( understandUtterance
, LinguisticState(..)
) where
import qualified Data.Vector as V
import NeurologicalRegex (Token(..), Embedding, Graph(..), Constraint(..),
solveConstraints, salcAttention)
import LinguisticTokenizer (LToken(..), tokenizeText, defaultVocab)
import LinguisticEncoder (encodeLinguisticFeatures, combineWithBERT)
import qualified Data.Map.Strict as M
import Data.Char (isAlpha, toLower)
data LinguisticState = LinguisticState
{ lstTokens :: [Token]
, lstEmbeddings :: [Embedding]
, lstAttended :: [Embedding]
, lstGraph :: Graph
, lstValid :: Bool
} deriving (Show)
-- | Full linguistic pipeline: text → LinguisticState
understandUtterance :: String -> LinguisticState
understandUtterance text =
let -- 1. Tokenize
ws = words (map toLower (filter (\c -> isAlpha c || c == ' ') text))
charTok = map (Tok . head) ws -- one token per word (first char key)
-- 2. Linguistic features
lingEmb = encodeLinguisticFeatures ws
-- 3. Combined embeddings (BERT + linguistic)
combined = combineWithBERT charTok lingEmb
-- 4. SALC attention
attended = salcAttention 16 combined
-- 5. Build constraint graph (each token is a node; chain edges)
nodeMap = M.fromList (zip charTok attended)
edgeMap = M.fromList $ zipWith (\a b -> (a,[b])) charTok (drop 1 charTok)
g = Graph nodeMap edgeMap
-- 6. Resonance invariant: total embedding mass
total = sum $ concatMap V.toList (M.elems nodeMap)
cs = [ResonanceInvariant total]
valid = solveConstraints g cs
in LinguisticState charTok combined attended g valid