File size: 2,654 Bytes
7b3d075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pickle
import sys
import os  # <--- Added this to control Windows
import math

# ... (Keep the cosine_similarity_pure function as it is) ...
def cosine_similarity_pure(vec1, matrix):
    # (Keep your existing math logic here)
    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:
            # Match
            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 to terminal
            print("\n[GENERATED SOLUTION]")
            print("-" * 30)
            print(result_code)
            print("-" * 30)
            
            # Save AND Open
            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)  # <--- THIS IS THE MAGIC LINE

        except Exception as e:
            print(f"Error: {e}")

if __name__ == "__main__":
    main()