Nodes
This class is used to get the nodes of the word graph.
Methods:
Name | Description |
---|---|
__init__ |
str, dataset: List[str], level: int, k: int, c: int, word2vec_model_path: str, mlm_model_path: str, mlm_model_type: str = 'roberta') The constructor of the Nodes class. |
get_similar_nodes |
str) -> List[str] This method is used to get the similar nodes of a word. |
get_context_nodes |
str) -> List[str] This method is used to get the context nodes of a word. |
get_node_features |
Dict[str, List[str]]) This method is used to get the features of the nodes of the word graph. |
Source code in semantics/graphs/temporal_graph.py
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
__init__(target_word, dataset, level, k, c, word2vec_model, mlm_model)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_word |
str
|
the word to get the nodes for |
required |
dataset |
List[str]
|
the sentences to get the nodes from |
required |
level |
int
|
the level of the graph to get |
required |
k |
int
|
the number of similar nodes to get for each occurrence of the target word |
required |
c |
int
|
the number of context nodes to get for the target word |
required |
word2vec_model |
Word2VecInference
|
the word2vec model's Inference class |
required |
mlm_model |
(RobertaInference, BertInference)
|
the MLM model's Inference class |
required |
Source code in semantics/graphs/temporal_graph.py
get_context_nodes(word)
This method is used to get the context nodes of a word using the word2vec model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
word |
str
|
the word to get the context nodes for |
required |
Returns:
Name | Type | Description |
---|---|---|
context_nodes |
List[str]
|
the list of context nodes of the word |
Source code in semantics/graphs/temporal_graph.py
get_node_features(nodes)
This method is used to get the features of the nodes of the word graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nodes |
Dict[str, List[str]]
|
the nodes of the word graph |
required |
Returns:
Name | Type | Description |
---|---|---|
index |
Dict[str, Dict[int, str]]
|
the index of the nodes of the word graph. The index contains the 'index_to_key' and 'key_to_index' mapping dictionaries. Example: in the index_to_key dictionary {0: target_word}, and in the key_to_index dictionary {target_word: 0}. |
node_features |
ndarray
|
the features of the nodes of the word graph of shape (num_nodes, 3) where num_nodes is the number of nodes in the graph. The features are:
|
embeddings |
ndarray
|
the embeddings of the nodes of the word graph from the MLM model, of shape (num_nodes, 768). |
Examples:
>>> word2vec = Word2VecInference('word2vec.model')
>>> mlm = RobertaInference('MLM_roberta')
>>> n = Nodes(target_word='sentence', dataset=['this is a sentence', 'this is another sentence'], level=3, k=2, c=2, word2vec_model = word2vec, mlm_model = mlm)
>>> nodes = n.get_nodes()
>>> index, node_features, embeddings = n.get_node_features(nodes)
>>> print(index)
{'index_to_key': {0: 'sentence', 1: 'this', 2: 'is', 3: 'a', 4: 'another'}, 'key_to_index': {'sentence': 0, 'this': 1, 'is': 2, 'a': 3, 'another': 4}
>>> print(node_features)
[[0, 0, 2], [1, 1, 2], [1, 1, 2], [1, 1, 2], [2, 1, 2]]
>>> print(embeddings.shape)
(5, 768)
Source code in semantics/graphs/temporal_graph.py
get_nodes()
This method is used to get the nodes of the word graph (similar nodes, context nodes, and target node).
Returns:
Name | Type | Description |
---|---|---|
nodes |
Dict[str, List[str]]
|
the nodes of the word graph |
Source code in semantics/graphs/temporal_graph.py
get_similar_nodes(word, keep_k=50)
This method is used to get the similar nodes of a word using the MLM model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
word |
str
|
the word to get the similar nodes for |
required |
keep_k |
int
|
the number of similar nodes to keep for each occurrence of the word. Default: 50. |
50
|
Returns:
Name | Type | Description |
---|---|---|
similar_nodes |
List[str]
|
the list of similar nodes of the word |