def __init__(self, router, uri, permissions=None, default_permissions=None): """ :param router: The router this role is defined on. :type router: obj :param uri: The URI of the role. :type uri: unicode :param permissions: A permissions configuration, e.g. a list of permission dicts like `{'uri': 'com.example.*', 'call': True}` :type permissions: list of dict :param default_permissions: The default permissions to apply when no other configured permission matches. The default permissions when not explicitly set is to deny all actions on all URIs! :type default_permissions: dict """ RouterRole.__init__(self, router, uri) assert (permissions is None or type(permissions) == list) if permissions: for p in permissions: assert (type(p) == dict) assert (default_permissions is None or type(default_permissions) == dict) # default permissions (used when nothing else is matching) # note: default permissions have their matching URI and match policy set to None! if default_permissions: self._default = RouterPermissions.from_dict(default_permissions) else: self._default = RouterPermissions(None, None, call=False, register=False, publish=False, subscribe=False, disclose_caller=False, disclose_publisher=False, cache=True) # Trie of explicitly configured permissions self._permissions = StringTrie() self._wild_permissions = StringTrie() # for "wildcard" URIs, there will be a ".." in them somewhere, # and so we want to match on the biggest prefix # (i.e. everything to the left of the first "..") for obj in permissions or []: perms = RouterPermissions.from_dict(obj) if '..' in perms.uri: trunc = perms.uri[:perms.uri.index('..')] self._wild_permissions[trunc] = perms else: self._permissions[perms.uri] = perms
def init(): app.meditems = StringTrie() drugs = ['advil', 'tylenol'] app.conitems = StringTrie() conditions = ['high cholesterol', 'fatness'] for d in drugs: app.meditems[d] = d for c in conditions: app.conitems[c] = c
def init(): app.p = Predictor() app.meditems = StringTrie() drugs = app.p.get_drugs() app.conitems = StringTrie() conditions = app.p.get_conditions() for d in drugs: app.meditems[d] = d for c in conditions: app.conitems[c] = c
def test_empty_tree(self): """ Test trie ctor, and that is doesn't match on "any" prefix. """ t = StringTrie() for key in ['', 'f', 'foo', 'foobar']: with self.assertRaises(KeyError): t.longest_prefix_value(key)
def __init__(self, router_session_factory, config, reactor): self._router_session_factory = router_session_factory self._router_factory = router_session_factory._routerFactory self._options = config.get('options', {}) self._realm = self._options.get('realm', None) self._reactor = reactor self._payload_mapping = StringTrie() for topic, pmap in self._options.get('payload_mapping', {}).items(): self._set_payload_format(topic, pmap)
def test_longest_prefix_1(self): """ Test that keys are detected as prefix of themselfes. """ t = StringTrie() test_keys = ['f', 'foo', 'foobar', 'baz'] for key in test_keys: t[key] = key for key in test_keys: self.assertEqual(t.longest_prefix_value(key), key)
def prefixSearch(arr, modelName): # create empty trie trie = StringTrie() # traverse through list of strings # to insert it in trie. Here value of # key is itself key because at last # we need to return for key in arr: trie[key] = key save_object(trie, modelName)
def __init__(self, default_key=None): """ Create a new key ring to hold public and private keys mapped from an URI space. """ assert (default_key is None or isinstance(default_key, Key) or type(default_key == six.text_type)) self._uri_to_key = StringTrie() if type(default_key) == six.text_type: default_key = Key(originator_priv=default_key, responder_priv=default_key) self._default_key = default_key
def test_longest_prefix_3(self): """ Test non-matching prefix lookups. """ t = StringTrie() for key in ['x', 'fop', 'foobar']: t[key] = key for key in ['y', 'yfoo', 'fox', 'fooba']: with self.assertRaises(KeyError): t.longest_prefix_value(key)
def test_contains(self): """ Test the contains operator. """ t = StringTrie() test_keys = ['', 'f', 'foo', 'foobar', 'baz'] for key in test_keys: t[key] = key for key in test_keys: self.assertTrue(key in t) for key in ['x', 'fb', 'foob', 'fooba', 'bazz']: self.assertFalse(key in t)
def is_valid(valid_list, letters): # create empty trie trie = StringTrie() # traverse through list of string to insert it in trie. # Here value of key is itself key because at last we need to retun for key in valid_list: trie[key] = key # values(search) method returns list of values of keys # which contains search pattern as prefix return trie.values(letters)
def prefixSearch(arr, prefix): trie = StringTrie() # traverse through list of strings # to insert it in trie. Here value of # key is itself key because at last # we need to return for key in arr: trie[key] = key length = len(prefix) + 1 for x in range(2, length): answer = trie.values(prefix[:x]) answer.sort() print(answer)
def build_trie_and_execute(map_to_search, prefix_term): trie = StringTrie() for key in map_to_search: trie[key] = key prefix_matches = trie.values(url_id) if len(prefix_matches) > 0: first_match = prefix_matches[0] index = map_to_search[first_match][0] open_url_in_browser(links[index], get_params()) return True return False
def test_longest_prefix_4(self): """ Test that a trie with an empty string as a key contained matches a non-empty prefix matching lookup. """ self.skip = True # stored_key = 'x' # this works (and of course it should!) stored_key = '' # this blows up! (and it _should_ work) test_key = 'xyz' t = StringTrie() t[stored_key] = stored_key self.assertTrue(stored_key in t) self.assertTrue(test_key.startswith(stored_key)) self.assertEqual(t.longest_prefix_value(test_key), stored_key)
def prefix_search(arr, prefix): # create empty trie trie = StringTrie() # traverse through list of strings # to insert it in trie. Here value of # key is itself key because at last # we need to return for key in arr: trie[key] = key # values(search) method returns list # of values of keys which contains # search pattern as prefix return trie.values(prefix)
def __init__(self, router, uri, permissions=None, default_permissions=None): """ :param router: The router this role is defined on. :type router: obj :param uri: The URI of the role. :type uri: unicode :param permissions: A permissions configuration, e.g. a list of permission dicts like `{'uri': 'com.example.*', 'call': True}` :type permissions: list of dict :param default_permissions: The default permissions to apply when no other configured permission matches. The default permissions when not explicitly set is to deny all actions on all URIs! :type default_permissions: dict """ RouterRole.__init__(self, router, uri) assert (permissions is None or type(permissions) == list) if permissions: for p in permissions: assert (type(p) == dict) assert (default_permissions is None or type(default_permissions) == dict) # default permissions (used when nothing else is matching) # note: default permissions have their matching URI and match policy set to None! if default_permissions: self._default = RouterPermissions.from_dict(default_permissions) else: self._default = RouterPermissions(None, None, call=False, register=False, publish=False, subscribe=False, disclose_caller=False, disclose_publisher=False, cache=True) # Trie of explicitly configured permissions self._permissions = StringTrie() for obj in permissions or []: perms = RouterPermissions.from_dict(obj) self._permissions[perms.uri] = perms
def __init__(self, ordered=False): # flag indicating whether observers should be maintained in a SortedSet # or a regular set (unordered) self._ordered = ordered # map: URI => ExactUriObservation self._observations_exact = {} # map: URI => PrefixUriObservation self._observations_prefix = StringTrie() # map: URI => WildcardUriObservation self._observations_wildcard = WildcardTrieMatcher() # map: observation ID => UriObservation self._observation_id_to_observation = {}
def __init__(self, router, uri, permissions=None, default_permissions=None, debug=False): """ Ctor. :param uri: The URI of the role. :type uri: str :param permissions: A permissions configuration, e.g. a list of permission dicts like `{'uri': 'com.example.*', 'call': True}` :type permissions: list :param debug: Enable debug logging. :type debug: bool """ RouterRole.__init__(self, router, uri, debug) self.permissions = permissions or [] self._urimap = StringTrie() self._default = default_permissions or RouterPermissions( '', True, False, False, False, False) for p in self.permissions: uri = p['uri'] if len(uri) > 0 and uri[-1] == '*': match_by_prefix = True uri = uri[:-1] else: match_by_prefix = False perms = RouterPermissions(uri, match_by_prefix, call=p.get('call', False), register=p.get('register', False), publish=p.get('publish', False), subscribe=p.get('subscribe', False)) if len(uri) > 0: self._urimap[uri] = perms else: self._default = perms
def __init__(self, ordered=False): # flag indicating whether observers should be maintained in a SortedSet # or a regular set (unordered) self._ordered = ordered # map: URI => ExactUriObservation self._observations_exact = {} # map: URI => PrefixUriObservation self._observations_prefix = StringTrie() # map: URI => WildcardUriObservation if True: # use a Trie-based implementation (supposed to be faster, but # otherwise compatible to the naive implementation below) self._observations_wildcard = WildcardTrieMatcher() else: self._observations_wildcard = WildcardMatcher() # map: observation ID => UriObservation self._observation_id_to_observation = {}
def test_longest_prefix_2(self): """ Test matching prefix lookups. """ t = StringTrie() test_keys = ['f', 'foo', 'foobar'] for key in test_keys: t[key] = key test_keys = { 'foobarbaz': 'foobar', 'foobaz': 'foo', 'fool': 'foo', 'foo': 'foo', 'fob': 'f', 'fo': 'f', 'fx': 'f', 'f': 'f', } for key in test_keys: self.assertEqual(t.longest_prefix_value(key), test_keys[key])
from pytrie import StringTrie import logging logger = logging.getLogger() """ prefixes = [] with open("resources/prefixes.txt", "r") as f: for line in f: prefixes.append(line.strip()) """ prefixes = StringTrie() with open("resources/prefixes.txt", "r") as f: for line in f: line = line.strip() prefixes[line] = line def get_business_sector(number): response = requests.get(f"https://challenge-business-sector-api.meza.talkdeskstg.com/sector/{number}") return response.json().get("sector") def standardize_number(number_string): tmp_number = number_string.replace(" ", "") if tmp_number.startswith("+"): tmp_number = tmp_number[1:]
def initial_tags(self): return {"build_tag": StringTrie(), "test_tag": StringTrie()}
def __init__(self): self.trie = None self.trie = StringTrie()
print(msg) print('--------------------------------------------------------------') # Make dict set; wordsList=[] f.close() f=open('dict.txt','r'); while(True): l=f.readline() if(len(l)==0): break wordsList+=[l[0:len(l)-1].lower()]; words=set(wordsList); # create empty trie trie=StringTrie() # traverse through list of strings # to insert it in trie. Here value of # key is itself key because at last # we need to return for key in words: trie[key] = key def prefixSearch(trie,prefix): # values(search) method returns list # of values of keys which contains # search pattern as prefix return trie.values(prefix) #Combination genation and decoding pre=['','','','','','','','','','']
print(i) print("\n-----------------------------------------\n") dict = [] f = open("word.txt", 'r') while True: inn = f.readline() if (len(inn) == 0): break dict += [inn[0:len(inn) - 1]] f.close() dictList = {} dictList = set(dict) tri = StringTrie() for i in dict: tri[i] = i def checkDict(msg2): cnt = 0 preck = ['', '', '', '', '', '', '', '', '', ''] for i in range(0, len(msg2)): msg = msg2[i] msg3 = msg.lower() msg3 = prepart[i] + msg3 #wordarr = re.split(',|\.|\?|-|!| |\(|\)', msg3)
def update_trie(self, word_list): self.trie = StringTrie() for word in word_list: word = word.lower() self.trie[word] = word
popular_words = set( [w.upper() for w in (popular_long_words + popular_medium_words)]) # download from: https://www.google.com/search?q=SOWPODS+github words = open('SOWPODS.txt').read().splitlines() print(f"loaded {len(words)} words") words = [word for word in words if letterset >= set(word)] print(f"subsetted to {len(words)} words: {words[0:20]}...") # optimization minlen = int(os.environ.get("MINLEN", "4")) words = [word for word in words if len(word) >= minlen] words_trie = StringTrie() for word in words: words_trie[word] = word words_set = set(words) possible_next_letters = {} for side in board_array: other_letters = letterset - set(side) print(f"{side}: {other_letters}") for ltr in side: possible_next_letters[ltr] = other_letters def word_tree(prefix): last_ltr = prefix[-1] next_ltrs = possible_next_letters[last_ltr]
"""Script that loads a trie of French words, to be used in other scripts""" import os import logging from pytrie import StringTrie path = "French-Dictionary-master/dictionary" files = os.listdir(path) dico = StringTrie() for file in files: logging.info(f"Loading {file}") with open(path + "/" + file) as f: mots = [line.split(";")[0].strip() for line in f.readlines()] for mot in mots: dico[mot] = mot logging.info("Dico loaded")
# Author: John Maheswaran import csv # used to read csv files from pytrie import StringTrie # used to match prefixes efficiently. # A trie allows you to match prefixes efficiently instead of the naive approach of looking # through all the strings and checking one by one, if we used a list instead of a trie import locale # used for formatting the currency in prices products = dict( ) # a dict is a hashmap representing the products, allows constant time lookup of product price # informaton based on the product id rungup = set( ) # this stores the products that we have rung up and allows constant insertion time trie = StringTrie( ) # a trie that allows us to look up all the strings matching a # prefix in faster than O(n) time where n is the number of strings # set the locate for currency formatting locale.setlocale(locale.LC_ALL, '') # first we will get the csv file name from the user print( "Please enter the csv product data filename or press ENTER to use the default (productdata.csv)" ) filename = input() if len(filename) == 0: filename = 'productdata.csv' # use a try block in case opening the csv file fails try:
def __init__(self, itemCollection): self.container = StringTrie() for item in itemCollection: self.container[Geohash.encode(item.Location.Latitude, item.Location.Longitude)] = item