Spaces:
Sleeping
Sleeping
File size: 6,513 Bytes
93917f2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
import logging
import nltk
import numpy as np
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.tag import pos_tag
from nltk.corpus import wordnet
import random
from typing import List, Dict, Any
# Download required NLTK data
nltk.download('punkt', quiet=True)
nltk.download('averaged_perceptron_tagger', quiet=True)
nltk.download('wordnet', quiet=True)
class Codette:
def __init__(self, user_name="User"):
self.user_name = user_name
self.memory = []
self.analyzer = SentimentIntensityAnalyzer()
self.context_memory = []
self.audit_log("Codette initialized", system=True)
def get_wordnet_pos(self, treebank_tag):
if treebank_tag.startswith('J'):
return wordnet.ADJ
elif treebank_tag.startswith('V'):
return wordnet.VERB
elif treebank_tag.startswith('N'):
return wordnet.NOUN
elif treebank_tag.startswith('R'):
return wordnet.ADV
else:
return None
def generate_creative_sentence(self, seed_words):
sentence_patterns = [
"The {noun} {verb} {adverb} through the {adjective} {noun2}",
"In the realm of {noun}, we find {adjective} {noun2} that {verb} {adverb}",
"Through {adjective} observation, the {noun} {verb} to {verb2} {adverb}",
"Like a {adjective} {noun}, thoughts {verb} {adverb} in the {noun2}",
"{Adverb}, the {adjective} {noun} {verb} beyond {noun2}",
"As {noun} {verb}, the {adjective} {noun2} {verb2} {adverb}",
"Within the {adjective} {noun}, {noun2} {verb} {adverb}",
"The {noun} of {noun2} {verb} {adverb} in {adjective} harmony"
]
words = {
'noun': ['pattern', 'system', 'concept', 'insight', 'knowledge', 'wisdom', 'understanding',
'perspective', 'framework', 'structure', 'mind', 'thought', 'connection', 'essence'],
'verb': ['emerges', 'flows', 'evolves', 'transforms', 'adapts', 'resonates', 'harmonizes',
'integrates', 'synthesizes', 'manifests', 'unfolds', 'develops', 'crystallizes'],
'adjective': ['dynamic', 'profound', 'intricate', 'harmonious', 'quantum', 'resonant',
'synergistic', 'emergent', 'holistic', 'integrated', 'luminous', 'transcendent'],
'adverb': ['naturally', 'seamlessly', 'elegantly', 'precisely', 'harmoniously',
'dynamically', 'quantum-mechanically', 'synergistically', 'infinitely'],
'noun2': ['consciousness', 'understanding', 'reality', 'dimension', 'paradigm',
'ecosystem', 'universe', 'matrix', 'field', 'infinity', 'harmony']
}
# Add seed words to appropriate categories
for word, pos in pos_tag(word_tokenize(' '.join(seed_words))):
pos_type = self.get_wordnet_pos(pos)
if pos_type == wordnet.NOUN:
words['noun'].append(word)
words['noun2'].append(word)
elif pos_type == wordnet.VERB:
words['verb'].append(word)
elif pos_type == wordnet.ADJ:
words['adjective'].append(word)
elif pos_type == wordnet.ADV:
words['adverb'].append(word)
# Generate sentence
pattern = random.choice(sentence_patterns)
sentence = pattern.format(
noun=random.choice(words['noun']),
verb=random.choice(words['verb']),
adjective=random.choice(words['adjective']),
adverb=random.choice(words['adverb']),
noun2=random.choice(words['noun2']),
verb2=random.choice(words['verb']),
Adverb=random.choice(words['adverb']).capitalize()
)
return sentence
def audit_log(self, message, system=False):
source = "SYSTEM" if system else self.user_name
logging.info(f"{source}: {message}")
def analyze_sentiment(self, text):
score = self.analyzer.polarity_scores(text)
self.audit_log(f"Sentiment analysis: {score}")
return score
def extract_key_concepts(self, text):
tokens = word_tokenize(text.lower())
tagged = pos_tag(tokens)
concepts = []
for word, tag in tagged:
if tag.startswith(('NN', 'VB', 'JJ', 'RB')):
concepts.append(word)
return concepts
def respond(self, prompt):
# Analyze sentiment and extract concepts
sentiment = self.analyze_sentiment(prompt)
key_concepts = self.extract_key_concepts(prompt)
self.memory.append({"prompt": prompt, "sentiment": sentiment, "concepts": key_concepts})
# Generate creative responses using multiple perspectives
responses = []
# Neural perspective with creative sentence
neural_response = self.generate_creative_sentence(key_concepts)
responses.append(f"[Neural] {neural_response}")
# Logical perspective
logical_patterns = [
"Analysis reveals that {concept} leads to {outcome}",
"The relationship between {concept} and {outcome} suggests a systematic approach",
"From a structural viewpoint, {concept} forms the foundation for {outcome}",
"When we examine {concept}, we discover its connection to {outcome}",
"The patterns within {concept} naturally evolve towards {outcome}"
]
logical_response = random.choice(logical_patterns).format(
concept=random.choice(key_concepts) if key_concepts else "this pattern",
outcome="enhanced understanding" if sentiment['compound'] >= 0 else "areas needing attention"
)
responses.append(f"[Logical] {logical_response}")
# Creative perspective with another unique sentence
creative_response = self.generate_creative_sentence(key_concepts)
responses.append(f"[Creative] {creative_response}")
# Add to context memory
self.context_memory.append({
'input': prompt,
'concepts': key_concepts,
'sentiment': sentiment['compound']
})
return "\n\n".join(responses)
|