|
1 |
| -if not graph.has_node(word): |
2 |
| - graph.add_node(word, encoded=encode_word_with_context(word, surrounding_words)) |
3 |
| - |
4 |
| - import networkx as nx |
5 |
| -nltk.download('wordnet') |
6 |
| -def add_acronym_node_to_graph(graph, acronym, possible_meanings, context=None, relevance=0.5, related_nodes_weights=None): |
7 |
| - """ |
8 |
| - Adds an ambiguous acronym node to the graph with attributes and probabilistic edges. |
9 |
| - |
10 |
| - Args: |
11 |
| - - graph (networkx.Graph): The graph to modify. |
12 |
| - - acronym (str): The ambiguous acronym (e.g., 'OSA'). |
13 |
| - - possible_meanings (list[str]): List of potential interpretations. |
14 |
| - - context (str): Contextual clue or location where the acronym appeared. |
15 |
| - - relevance (float): Overall relevance score of this acronym to the graph's domain. |
16 |
| - - related_nodes_weights (dict): Dictionary of related nodes and their edge weights. |
17 |
| - """ |
18 |
| - graph.add_node(acronym, |
19 |
| - possible_meanings=possible_meanings, |
20 |
| - context=context, |
21 |
| - relevance=relevance, |
22 |
| - type='acronym') |
23 |
| - |
24 |
| - if related_nodes_weights: |
25 |
| - for node, weight in related_nodes_weights.items(): |
26 |
| - if node in graph.nodes: |
27 |
| - graph.add_edge(acronym, node, weight=weight) |
28 |
| - |
29 |
| - return graph |
30 |
| - # Initialize graph |
31 |
| -graph = nx.Graph() |
32 |
| -graph.add_nodes_from(["temperature", "humidity", "cloudy"]) |
| 1 | +jaris/ |
| 2 | +│ |
| 3 | +├── graph_core.py # GraphManager: adds nodes, edges, snapshots |
| 4 | +├── context_encoder.py # ContextEncoder: semantic + numeric encoding |
| 5 | +├── visualizer.py # Visualizer: Matplotlib + Pyvis graph views |
| 6 | +├── logger.py # Logger: history, acronym logs, version tracking |
| 7 | +├── feedback_loop.py # FeedbackManager: edge weight learning |
| 8 | +│ |
| 9 | +├── jaris_notebook.ipynb # Jupyter notebook: interactive brainspace |
| 10 | +├── demo_data/ # (Optional) Seed acronyms, example contexts |
| 11 | +└── README.md # Documentation overview |
33 | 12 | import networkx as nx
|
34 |
| -import matplotlib.pyplot as plt |
35 |
| -import nltk |
36 |
| -from nltk.corpus import wordnet as wn |
37 |
| - |
38 |
| -# Make sure to download WordNet if not already done |
39 |
| -nltk.download('wordnet') |
40 |
| - |
41 |
| -def get_contextual_meaning(letter, surrounding_words): |
42 |
| - """ |
43 |
| - Generate a contextual meaning for a letter based on surrounding words. |
44 |
| - |
45 |
| - Args: |
46 |
| - - letter (str): The letter whose meaning needs to be generated. |
47 |
| - - surrounding_words (list): A list of words surrounding the target word. |
48 |
| - |
49 |
| - Returns: |
50 |
| - - str: A contextual meaning of the letter. |
51 |
| - """ |
52 |
| - meanings = [] |
53 |
| - |
54 |
| - # Iterate through the surrounding words to understand the context |
55 |
| - for word in surrounding_words: |
56 |
| - synsets = wn.synsets(word) |
57 |
| - if synsets: |
58 |
| - # Use the first synset's lemma names as potential meanings |
59 |
| - meanings.extend([lemma.name() for lemma in synsets[0].lemmas()]) |
60 |
| - |
61 |
| - # Here, we're simply returning the first meaning related to the letter |
62 |
| - # This could be expanded with domain-specific knowledge |
63 |
| - return meanings[0] if meanings else "unknown" |
64 |
| - |
65 |
| -def encode_word_with_context(word, surrounding_words): |
66 |
| - """ |
67 |
| - Encodes a word into a numeric vector based on the alphabet-to-number mapping, |
68 |
| - but modifies each letter's encoding based on surrounding context. |
69 |
| - |
70 |
| - Args: |
71 |
| - - word (str): The word to encode. |
72 |
| - - surrounding_words (list): List of surrounding words for contextual analysis. |
73 |
| - |
74 |
| - Returns: |
75 |
| - - List[int]: A list of integers representing the word with contextual adjustments. |
76 |
| - """ |
77 |
| - encoded_word = [] |
78 |
| - |
79 |
| - for char in word.upper(): |
80 |
| - if char.isalpha(): |
81 |
| - # Get the contextual meaning of the letter based on surrounding words |
82 |
| - meaning = get_contextual_meaning(char, surrounding_words) |
83 |
| - |
84 |
| - # Adjust the letter encoding based on the contextual meaning |
85 |
| - letter_encoding = (ord(char) - ord('A') + 1) # Basic encoding (1-26) |
86 |
| - |
87 |
| - # Here, we could adjust encoding based on the meaning, for now, we use the length of meaning |
88 |
| - # As a simple proxy, we modify the encoding based on the length of the meaning |
89 |
| - contextual_encoding = letter_encoding + len(meaning) |
90 |
| - encoded_word.append(contextual_encoding) |
91 |
| - else: |
92 |
| - encoded_word.append(0) # For non-alphabetic characters (e.g., spaces) |
93 |
| - |
94 |
| - return encoded_word |
95 |
| - |
96 |
| -def add_to_graph(graph, word, relationships, surrounding_words): |
97 |
| - """ |
98 |
| - Adds a new word (acronym) to the graph and updates relationships. |
99 |
| - |
100 |
| - Args: |
101 |
| - - graph (networkx.Graph): The existing graph. |
102 |
| - - word (str): The new word (acronym) to add. |
103 |
| - - relationships (list): A list of relationships (edges) with other words. |
104 |
| - - surrounding_words (list): List of surrounding words for contextual analysis. |
105 |
| - """ |
106 |
| - # Add the new word as a node with context-aware encoding |
107 |
| - graph.add_node(word, encoded=encode_word_with_context(word, surrounding_words)) |
108 |
| - |
109 |
| - # Add relationships (edges) with other words |
110 |
| - for related_word in relationships: |
111 |
| - graph.add_edge(word, related_word) |
112 | 13 |
|
113 |
| -# Create an empty graph |
114 |
| -graph = nx.Graph() |
| 14 | +class GraphManager: |
| 15 | + def __init__(self): |
| 16 | + self.graph = nx.Graph() |
115 | 17 |
|
116 |
| -# Example of adding a new word and relationships |
117 |
| -add_to_graph(graph, "cloudy", ["weather", "humidity"], ["temperature", "rain", "storm"]) |
| 18 | + def add_acronym_node(self, acronym, meanings, context="", relevance=1.0, related_nodes_weights=None): |
| 19 | + if acronym not in self.graph: |
| 20 | + self.graph.add_node(acronym, type='acronym', meanings=meanings, context=context, relevance=relevance) |
118 | 21 |
|
119 |
| -# Visualize the updated graph |
120 |
| -nx.draw(graph, with_labels=True, node_size=2000, node_color="skyblue", font_size=12, font_weight="bold") |
121 |
| -plt.show() |
122 |
| -# Define OSA possible meanings |
123 |
| -osa_meanings = [ |
124 |
| - "Optical Spectrum Analyzer", |
125 |
| - "Outside Air", |
126 |
| - "Obstructive Sleep Apnea" |
127 |
| -] |
| 22 | + if related_nodes_weights: |
| 23 | + for related_node, weight in related_nodes_weights.items(): |
| 24 | + self.graph.add_edge(acronym, related_node, weight=weight) |
128 | 25 |
|
129 |
| -# Add OSA node with example relationships |
130 |
| -related_weights = { |
131 |
| - "temperature": 0.6, |
132 |
| - "humidity": 0.7, |
133 |
| - "cloudy": 0.3 |
134 |
| -} |
| 26 | + def add_edge(self, node1, node2, weight=1.0): |
| 27 | + self.graph.add_edge(node1, node2, weight=weight) |
135 | 28 |
|
136 |
| -graph = add_acronym_node_to_graph( |
137 |
| - graph, |
138 |
| - acronym="OSA", |
139 |
| - possible_meanings=osa_meanings, |
140 |
| - context="weather app interface", |
141 |
| - relevance=0.8, |
142 |
| - related_nodes_weights=related_weights |
143 |
| -) |
| 29 | + def get_neighbors(self, node): |
| 30 | + return list(self.graph.neighbors(node)) |
144 | 31 |
|
145 |
| -# Inspect |
146 |
| -print(graph.nodes["OSA"]) |
147 |
| -print(list(graph.edges("OSA", data=True))) |
148 |
| -feature_vector = { |
149 |
| - "OSA": 1, |
150 |
| - "OSA_is_outside_air": 1, |
151 |
| - "OSA_is_obstructive_sleep_apnea": 0, |
152 |
| - "temperature": 73, |
153 |
| - "humidity": 45, |
154 |
| - "context_contains": "weather app" |
155 |
| -}colors = ['red' if graph.nodes[node].get('type') == 'acronym' else 'skyblue' for node in graph.nodes] |
| 32 | + def get_node_data(self, node): |
| 33 | + return self.graph.nodes.get(node, {}) |
156 | 34 |
|
157 |
| -nx.draw(graph, with_labels=True, node_color=colors, node_size=2000, font_weight='bold') |
158 |
| -plt.show() |
| 35 | + def snapshot_graph(self, filename="graph_snapshot.gml"): |
| 36 | + nx.write_gml(self.graph, filename) |
0 commit comments