|
|
import pickle
|
|
|
import sys
|
|
|
import os
|
|
|
import math
|
|
|
|
|
|
|
|
|
def cosine_similarity_pure(vec1, matrix):
|
|
|
|
|
|
best_score = -1
|
|
|
best_index = -1
|
|
|
query_indices = vec1.indices
|
|
|
query_data = vec1.data
|
|
|
query_map = dict(zip(query_indices, query_data))
|
|
|
|
|
|
num_rows = matrix.shape[0]
|
|
|
for i in range(num_rows):
|
|
|
row = matrix.getrow(i)
|
|
|
row_indices = row.indices
|
|
|
row_data = row.data
|
|
|
score = 0
|
|
|
for idx, val in zip(row_indices, row_data):
|
|
|
if idx in query_map:
|
|
|
score += val * query_map[idx]
|
|
|
if score > best_score:
|
|
|
best_score = score
|
|
|
best_index = i
|
|
|
return best_index
|
|
|
|
|
|
def load_brain():
|
|
|
try:
|
|
|
with open("ghost_brain.pkl", "rb") as f:
|
|
|
return pickle.load(f)
|
|
|
except FileNotFoundError:
|
|
|
print("❌ Error: 'ghost_brain.pkl' missing!")
|
|
|
input("Press Enter to exit...")
|
|
|
sys.exit()
|
|
|
|
|
|
def main():
|
|
|
print("---------------------------------------")
|
|
|
print(" 👻 GHOST CODEX (OFFLINE MODE) v1.1")
|
|
|
print("---------------------------------------")
|
|
|
|
|
|
print("Loading Neural Indices...", end="")
|
|
|
vectorizer, tfidf_matrix, codes = load_brain()
|
|
|
print(" DONE.")
|
|
|
print("\n[READY] Type 'exit' to quit.")
|
|
|
|
|
|
while True:
|
|
|
user_input = input("\n>> ").strip()
|
|
|
if user_input.lower() in ["exit", "quit"]:
|
|
|
break
|
|
|
if not user_input: continue
|
|
|
|
|
|
try:
|
|
|
|
|
|
user_vec = vectorizer.transform([user_input])
|
|
|
try:
|
|
|
from sklearn.metrics.pairwise import cosine_similarity
|
|
|
best_idx = cosine_similarity(user_vec, tfidf_matrix).argmax()
|
|
|
except ImportError:
|
|
|
best_idx = cosine_similarity_pure(user_vec, tfidf_matrix)
|
|
|
|
|
|
result_code = codes[best_idx]
|
|
|
|
|
|
|
|
|
print("\n[GENERATED SOLUTION]")
|
|
|
print("-" * 30)
|
|
|
print(result_code)
|
|
|
print("-" * 30)
|
|
|
|
|
|
|
|
|
filename = "solution.txt"
|
|
|
with open(filename, "w", encoding="utf-8") as f:
|
|
|
f.write(result_code)
|
|
|
|
|
|
print(f" (Opening {filename} in Notepad...)")
|
|
|
os.startfile(filename)
|
|
|
|
|
|
except Exception as e:
|
|
|
print(f"Error: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
main() |