Best Python code snippet using dbt-osmosis_python
tree.py
Source:tree.py
...41 if index > node.size or index < 0:42 raise IndexError(f"tree index {index} out of range")43 left_subtree_size: int = Tree.subtree_size(node, 'left')44 if index < left_subtree_size:45 return Tree.get_by_index(node.get_relation('left'), index)46 elif index == left_subtree_size:47 return node48 else:49 assert node.has_relation('right')50 return Tree.get_by_index(node.get_relation('right'), index - (left_subtree_size + 1))51 @staticmethod52 def get_index_of(node: Node[T], root: Node[T] = None) -> int:53 index: int = Tree.subtree_size(node, 'left')54 while node.has_relation('parent') and node != root:55 if Tree.is_right_child(node):56 index += 1 + Tree.subtree_size(node.get_relation('parent'), 'left')57 node = node.get_relation('parent')58 return index 59 @staticmethod60 def get_leftmost(subtree_root: Node[T]) -> Node[T]:61 """62 Gets the leftmost node in the tree rooted at subtree_root.63 May return subtree_root itself. If subtree_root not provided,64 searches entire tree.65 subtree_root: the root of the subtree to search66 returns : the leftmost node67 """68 return Tree._get_furthest_in_direction('left', subtree_root)69 @staticmethod70 def get_next_left(node: Node[T]) -> Optional[Node[T]]:71 """72 Gets the rightmost node that is left of the node 'node'73 If these were numbers, then it would be the greatest number 74 less than node.value75 node: the node immediately to the right of the returned node76 returns : the next value to the left, root if not found77 """78 return Tree._get_next_in_direction_('left', node)79 @staticmethod80 def get_next_left_parent(node: Node[T]) -> Optional[Node[T]]:81 """82 Gets the ancestor in 'node's ancestor tree that is the rightmost83 but not further right than 'node'.84 Alternately put, it gets the next left node in the tree ignoring node's85 children86 :param node: the reference node to search near87 :return: the found ancestor node, None if all ancestors are left-children88 """89 return Tree._get_next_parent_in_direction_('left', node)90 @staticmethod91 def get_next_right(node: Node[T]) -> Optional[Node[T]]:92 """93 Gets the lefttmost node that is right of the node 'node'94 If these were numbers, then it would be the lowest number 95 greater than node.value96 node: the node immediately to the left of the returned node97 returns : the next value to the right, root if not found98 """99 return Tree._get_next_in_direction_('right', node)100 def get_next_right_parent(node: Node[T]) -> Optional[Node[T]]:101 """102 Gets the ancestor in 'node's ancestor tree that is the leftmost103 but not further left than 'node'.104 Alternately put, it gets the next right node in the tree ignoring node's105 children106 :param node: the reference node to search near107 :return: the found ancestor node, None if all ancestors are right-children108 """109 return Tree._get_next_parent_in_direction_('right', node)110 @staticmethod111 def get_rightmost(subtree_root: Node[T]) -> Node[T]:112 """113 Gets the rightmost node in the tree rooted at subtree_root.114 May return subtree_root itself. If subtree_root not provided,115 searches entire tree.116 subtree_root: the root of the subtree to search117 returns : the rightmost node118 """119 return Tree._get_furthest_in_direction_('right', subtree_root)120 @staticmethod121 def get_root(node: Node[T]) -> Node[T]:122 """123 Gets the root of the tree containing node 'node'124 Parameters125 ----------126 node: Node[T]127 the reference node in the tree where we're searching for 128 the root129 Returns130 -------131 root: Node[T]132 the root node133 """134 while node.has_relation('parent'):135 node = node.get_relation('parent')136 return node137 @staticmethod138 def is_left_of(self, left: Node[T], right: Node[T]) -> bool:139 return Tree._is_in_direction_of_(left, right, 'left')140 @staticmethod141 def is_right_of(right: Node[T], left: Node[T]) -> bool:142 return Tree._is_in_direction_of_(right, left, 'right')143 @staticmethod144 def is_left_child(node: Node[T]) -> bool:145 return Tree._is_child_in_direction_(node, 'left')146 @staticmethod147 def is_right_child(node: Node[T]) -> bool:148 return Tree._is_child_in_direction_(node, 'right')149 @staticmethod150 def is_ancestor_of(ancestor: Node[T], node: Node[T], bound: Node[T] = None) -> bool:151 """152 node: Node, the potential child153 ancestor: Node, the potential ancestor154 bound: Node, will not check any ancestors higher than bound155 returns whether or not ancestor is an ancestor of node more recently than bound156 NOTE: a node is it's own ancestor157 """158 while True:159 if node == ancestor:160 return True161 if node == bound or not node.has_relation('parent'):162 return False163 node = node.get_relation('parent')164 @staticmethod165 def list_subtree_nodes(root: Node[T], direction: str) -> List[Node[T]]:166 direction = Tree._parse_direction_(direction)167 return Tree.list_nodes(root.get_relation(direction)) if root.has_relation(direction) else []168 @staticmethod169 def list_nodes(root: Node[T]) -> List[Node[T]]:170 return Tree.list_subtree_nodes(root, 'left') + [root] + Tree.list_subtree_nodes(root, 'right')171 @staticmethod172 def mirror_add(existing_node: Node[T], original_root: Node[T], new_root: Node[V], new_value: V) -> Node[V]:173 assert Tree.is_ancestor_of(original_root, existing_node)174 index: int = Tree.get_index_of(existing_node, original_root)175 assert new_root.size >= index #can be equal, because size of original_root should be > than new_root176 if index == 0:177 parent: Node[V] = Tree.get_leftmost(new_root)178 return Tree.add_left(new_value, parent)179 else:180 parent: Node[V] = Tree.get_by_index(new_root, index-1) #get the node immediately to the left181 return Tree.add_right(new_value, parent)182 @staticmethod183 def mirror_get(existing_node: Node[T], original_root: Node[T], new_root: Node[V]) -> Node[V]:184 assert Tree.is_ancestor_of(original_root, existing_node)185 index: int = Tree.get_index_of(existing_node, original_root)186 assert new_root.size > index 187 return Tree.get_by_index(new_root, index)188 @staticmethod189 def subtree_size(node: Node[T], direction: str) -> int:190 direction = Tree._parse_direction_(direction)191 if node.has_relation(direction):192 return node.get_relation(direction).size193 else:194 return 0195 @staticmethod196 def _add_direction_(value: T, direction: str, node: Node[T]) -> Node[T]:197 """198 Adds the value in a new node that is directly to the left or right of parent199 for instance, after calling _add_direction_(direction='left'), the new node will200 be the rightmost value to the left of 'node', not necessarily a direct child201 value: T, the new value to add to the tree202 direction: left or right, where the new value is in relation to 'node'203 'node': the 'node' to add to the left or right of, doesn't need to have free spots204 returns : the newly created node205 """206 other: str = Tree._opposite_direction_(direction)207 if not node.has_relation(direction): #if you can add directly to left or right, do so208 return node.add_relation(value, direction)209 else:210 node = node.get_relation(direction) #otherwise, go one over211 while node.has_relation(other): #go as far as you can back212 node = node.get_relation(other)213 result: Node[T] = node.add_relation(value, other) #and add214 Tree._propagate_tree_size_(result) #update tree size215 return result216 @staticmethod217 def _get_furthest_in_direction_(direction: str, subtree_root: Node[T]) -> Node[T]:218 """219 Gets the node that is furthest to in the given direction 220 in the subtree rooted at 'subtree_root'. May return subtree_root221 itself if necessary. If subtree_root not provided, uses222 the root of the entire tree.223 direction : ['left', 'right'], the direction to search in224 subtree_root: the root of the subtree to search225 returns : the node that is furthest in the given direction226 """227 while subtree_root.has_relation(direction):228 subtree_root = subtree_root.get_relation(direction)229 return subtree_root230 @staticmethod231 def _get_next_in_direction_(232 direction: str,233 node: Node[T]) -> Optional[Node[T]]:234 """235 Gets the next node over in the tree236 For instance, if this were a binary search tree of numbers,237 and _get_direction_('left') were called, it would return the node238 corresponding to the highest number smaller than 'node'.239 If not found, root is returned240 direction : ['left', 'right'] : which direction to search in241 node: the node to search from the left or right of242 returns : the next node found after searching in that direction243 """244 other: str = Tree._opposite_direction_(direction)245 #Case 1: Search down the tree246 #Example : direction = left, we want the rightmost element of the247 # left subtree248 if node.has_relation(direction):249 node = node.get_relation(direction)250 while node.has_relation(other):251 node = node.get_relation(other)252 return node 253 #Case 2: Search up the tree254 #Example: direction = left, we want the first ancestor255 #Where we are in the right subtree of that ancestor256 else:257 while node != None:258 parent: Node[T] = node.get_relation('parent')259 if parent is not None and parent.get_relation(other) == node:260 return parent261 else:262 node = parent263 return None264 @staticmethod265 def _is_child_in_direction_(node: Node[T], direction: str) -> bool:266 return node.has_relation('parent') and \267 node.get_relation('parent').has_relation(direction) and \268 node.get_relation('parent').get_relation(direction) == node269 @staticmethod270 def _is_in_direction_of_(other: Node[T], reference: Node[T], direction: str) -> bool:271 assert Tree.get_root(other) == Tree.get_root(reference)272 direction = Tree._parse_direction_(direction)273 # both nodes in the same tree274 if other == reference:275 return False276 current: Node[T] = reference277 while current is not None:278 current = current.get_relation(direction)279 if current == other:280 return True281 # we got to the end of the tree and didn't find it282 return False283 @staticmethod284 def _opposite_direction_(direction: str) -> str:285 direction = Tree._parse_direction_(direction)286 return 'left' if direction == 'right' else 'right'287 @staticmethod288 def _parse_direction_(direction: str) -> str:289 direction = direction.lower()290 assert direction in ['left', 'right']291 return direction 292 @staticmethod293 def _propagate_tree_size_(node: Node[T]) -> None:294 while node.has_relation('parent'):295 node = node.get_relation('parent')296 node.size += 1297 @staticmethod298 def _get_next_parent_in_direction_(299 direction: str,300 node: Node[T]) -> Optional[Node[T]]:301 other_dir: str = Tree._opposite_direction_(direction)302 if not node.has_relation('parent'):303 return None304 if Tree._is_child_in_direction_(node, other_dir):305 return node.get_relation('parent')306 return Tree._get_next_parent_in_direction_(307 direction,...
relation_miner.py
Source:relation_miner.py
...43 subject_bagofwords = set()44 action_bagofwords = set()45 for node in dep_tree[0]:46 # print(node)47 self.get_relation(node, 'dobj', what_bagofwords, where_bagofwords)48 # if node[0] == 'dobj':49 # action_bagofwords.add(verb+" "+obj)50 self.get_relation(node, 'nsubj',51 what_bagofwords,52 subject_bagofwords)53 self.get_relation(node, 'nmod:on',54 what_bagofwords,55 where_attribute_bagofwords)56 self.get_relation(node, 'nmod:in',57 where_attribute_bagofwords,58 where_attribute_bagofwords)59 self.get_relation(node, 'advcl:to',60 what_bagofwords,61 why_bagofwords)62 self.get_relation(node, 'compound',63 where_bagofwords,64 where_bagofwords)65 self.get_relation(node, 'nsubjpass',66 where_bagofwords,67 where_bagofwords)68 self.get_relation(node, 'nmod:agent',69 where_bagofwords,70 where_bagofwords)71 self.get_relation(node, 'nmod:from',72 where_bagofwords,73 where_bagofwords)74 self.get_relation(node, 'nmod:to',75 where_bagofwords,76 where_bagofwords)77 self.get_relation(node, 'nmod:with',78 where_bagofwords,79 where_bagofwords)80 self.get_relation(node, 'nmod:via',81 where_bagofwords,82 where_bagofwords)83 self.get_relation(node, 'nmod:over',84 where_bagofwords,85 where_bagofwords)86 self.get_relation(node, 'nmod:for',87 where_bagofwords,88 where_bagofwords)89 self.get_relation(node, 'nmod:via',90 where_bagofwords,91 where_bagofwords)92 self.get_relation(node, 'nmod:through',93 where_bagofwords,94 where_bagofwords)95 self.get_relation(node, 'nmod:using',96 where_bagofwords,97 where_bagofwords)98 self.get_relation(node, 'nmod:into',99 where_bagofwords,100 where_bagofwords)101 # what_bafofwords.append(verb)102 # where_bagofwords.append(obj)103 extracted_words['what'] = helper.remove_stopwords(what_bagofwords)104 extracted_words['where'] = helper.remove_stopwords(where_bagofwords)105 extracted_words['where_attribute'] = helper.remove_stopwords(where_attribute_bagofwords)106 extracted_words['why'] = helper.remove_stopwords(why_bagofwords)107 extracted_words['when'] = helper.remove_stopwords(when_bagofwords)108 extracted_words['how'] = helper.remove_stopwords(how_bagofwords)109 extracted_words['subject'] = helper.remove_stopwords(subject_bagofwords)110 extracted_words['action'] = helper.remove_stopwords(action_bagofwords)111 extracted_words['text'] = sentence112 return extracted_words113 def get_relation(self, node, relation_type, *argv):114 # print(node)115 if node[0] == relation_type:116 k = 1117 for arg in argv:118 # print(arg)119 arg.add(node[k])120 k += 1121 # print(arg)122 # print(node[1], node[2])123 return node[1], node[2]124 def list_important_info(self, text):125 dep_parse_tree = self.depparse(text)126 # print(dep_parse_tree)127 important_dict = self.get_important_relations(dep_parse_tree, text)...
conceptnet.py
Source:conceptnet.py
...12 self.data[arg2] = {}13 self.data[arg2][arg1] = r14 cnt += 115 print('Load %d triples from %s' % (cnt, path))16 def get_relation(self, w1, w2):17 if is_stopword(w1) or is_stopword(w2):18 return '<NULL>'19 w1 = '_'.join(w1.lower().split())20 w2 = '_'.join(w2.lower().split())21 if not w1 in self.data:22 return '<NULL>'23 return self.data[w1].get(w2, '<NULL>')24 def p_q_relation(self, passage, query):25 passage = [w.lower() for w in passage]26 query = [w.lower() for w in query]27 query = set(query) | set([' '.join(query[i:(i+2)]) for i in range(len(query))])28 query = set([q for q in query if not is_stopword(q)])29 ret = ['<NULL>' for _ in passage]30 for i in range(len(passage)):31 for q in query:32 r = self.get_relation(passage[i], q)33 if r != '<NULL>':34 ret[i] = r35 break36 r = self.get_relation(' '.join(passage[i:(i+2)]), q)37 if r != '<NULL>':38 ret[i] = r39 break40 return ret41concept_net = ConceptNet()42if __name__ == '__main__':43 net = ConceptNet()44 print(net.get_relation('positive', 'negative'))45 print(net.get_relation('under water', 'above water'))46 while True:47 w1, w2 = input('Please input two words: ').strip().split()[:2]...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!