|
1 |
| -Args: |
2 |
| -- graph (networkx.Graph): The existing graph. |
3 |
| -- word (str): The new word (acronym) to add. |
4 |
| -- relationships (list): A list of relationships (edges) with other words. |
5 |
| -""" |
6 |
| -# Add the new word as a node |
7 |
| -graph.add_node(word, encoded=encode_word(word)) |
| 1 | +import networkx as nx |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import nltk |
| 4 | +from nltk.corpus import wordnet as wn |
8 | 5 |
|
9 |
| -# Add relationships (edges) with other words |
10 |
| -for related_word in relationships: |
11 |
| - graph.add_edge(word, related_word) |
12 |
| -Args: |
13 |
| -- letter (str): The letter whose meaning needs to be generated. |
14 |
| -- surrounding_words (list): A list of words surrounding the target word. |
| 6 | +# Make sure to download WordNet if not already done |
| 7 | +nltk.download('wordnet') |
15 | 8 |
|
16 |
| -Returns: |
17 |
| -- str: A contextual meaning of the letter. |
18 |
| -""" |
19 |
| -# For simplicity, we will get the first synset of a word from WordNet |
20 |
| -# The goal here is to define rules or NLP techniques for contextual meanings |
21 |
| -meanings = [] |
| 9 | +def get_contextual_meaning(letter, surrounding_words): |
| 10 | + """ |
| 11 | + Generate a contextual meaning for a letter based on surrounding words. |
| 12 | + |
| 13 | + Args: |
| 14 | + - letter (str): The letter whose meaning needs to be generated. |
| 15 | + - surrounding_words (list): A list of words surrounding the target word. |
| 16 | + |
| 17 | + Returns: |
| 18 | + - str: A contextual meaning of the letter. |
| 19 | + """ |
| 20 | + meanings = [] |
| 21 | + |
| 22 | + # Iterate through the surrounding words to understand the context |
| 23 | + for word in surrounding_words: |
| 24 | + synsets = wn.synsets(word) |
| 25 | + if synsets: |
| 26 | + # Use the first synset's lemma names as potential meanings |
| 27 | + meanings.extend([lemma.name() for lemma in synsets[0].lemmas()]) |
| 28 | + |
| 29 | + # Here, we're simply returning the first meaning related to the letter |
| 30 | + # This could be expanded with domain-specific knowledge |
| 31 | + return meanings[0] if meanings else "unknown" |
22 | 32 |
|
23 |
| -# Iterate through the surrounding words to understand the context |
24 |
| -for word in surrounding_words: |
25 |
| - synsets = wn.synsets(word) |
26 |
| - if synsets: |
27 |
| - # Use the first synset's lemma names as potential meanings |
28 |
| - meanings.extend([lemma.name() for lemma in synsets[0].lemmas()]) |
| 33 | +def encode_word_with_context(word, surrounding_words): |
| 34 | + """ |
| 35 | + Encodes a word into a numeric vector based on the alphabet-to-number mapping, |
| 36 | + but modifies each letter's encoding based on surrounding context. |
| 37 | + |
| 38 | + Args: |
| 39 | + - word (str): The word to encode. |
| 40 | + - surrounding_words (list): List of surrounding words for contextual analysis. |
| 41 | + |
| 42 | + Returns: |
| 43 | + - List[int]: A list of integers representing the word with contextual adjustments. |
| 44 | + """ |
| 45 | + encoded_word = [] |
| 46 | + |
| 47 | + for char in word.upper(): |
| 48 | + if char.isalpha(): |
| 49 | + # Get the contextual meaning of the letter based on surrounding words |
| 50 | + meaning = get_contextual_meaning(char, surrounding_words) |
| 51 | + |
| 52 | + # Adjust the letter encoding based on the contextual meaning |
| 53 | + letter_encoding = (ord(char) - ord('A') + 1) # Basic encoding (1-26) |
| 54 | + |
| 55 | + # Here, we could adjust encoding based on the meaning, for now, we use the length of meaning |
| 56 | + # As a simple proxy, we modify the encoding based on the length of the meaning |
| 57 | + contextual_encoding = letter_encoding + len(meaning) |
| 58 | + encoded_word.append(contextual_encoding) |
| 59 | + else: |
| 60 | + encoded_word.append(0) # For non-alphabetic characters (e.g., spaces) |
| 61 | + |
| 62 | + return encoded_word |
29 | 63 |
|
30 |
| -# Here, we're simply returning the first meaning related to the letter |
31 |
| -# This could be expanded with domain-specific knowledge |
32 |
| -return meanings[0] if meanings else "unknown" |
33 |
| -def add_to_graph(graph, word, relationships): |
| 64 | +def add_to_graph(graph, word, relationships, surrounding_words): |
34 | 65 | """
|
35 | 66 | Adds a new word (acronym) to the graph and updates relationships.
|
36 | 67 |
|
37 | 68 | Args:
|
38 | 69 | - graph (networkx.Graph): The existing graph.
|
39 | 70 | - word (str): The new word (acronym) to add.
|
40 | 71 | - relationships (list): A list of relationships (edges) with other words.
|
| 72 | + - surrounding_words (list): List of surrounding words for contextual analysis. |
41 | 73 | """
|
42 |
| - # Add the new word as a node |
43 |
| - graph.add_node(word, encoded=encode_word(word)) |
| 74 | + # Add the new word as a node with context-aware encoding |
| 75 | + graph.add_node(word, encoded=encode_word_with_context(word, surrounding_words)) |
44 | 76 |
|
45 | 77 | # Add relationships (edges) with other words
|
46 | 78 | for related_word in relationships:
|
47 | 79 | graph.add_edge(word, related_word)
|
48 | 80 |
|
| 81 | +# Create an empty graph |
| 82 | +graph = nx.Graph() |
| 83 | + |
49 | 84 | # Example of adding a new word and relationships
|
50 |
| -add_to_graph(graph, "cloudy", ["weather", "humidity"]) |
| 85 | +add_to_graph(graph, "cloudy", ["weather", "humidity"], ["temperature", "rain", "storm"]) |
51 | 86 |
|
52 | 87 | # Visualize the updated graph
|
53 | 88 | nx.draw(graph, with_labels=True, node_size=2000, node_color="skyblue", font_size=12, font_weight="bold")
|
|
0 commit comments