code
string
signature
string
docstring
string
loss_without_docstring
float64
loss_with_docstring
float64
factor
float64
def wrapped_callback(context_p, device_p, event, _): assert addressof(context_p.contents) == addressof( self.__context_p.contents), (context_p, self.__context_p) device = USBDevice( self, device_p, # pylint: disable...
def hotplugRegisterCallback( self, callback, # pylint: disable=undefined-variable events=HOTPLUG_EVENT_DEVICE_ARRIVED | HOTPLUG_EVENT_DEVICE_LEFT, flags=HOTPLUG_ENUMERATE, vendor_id=HOTPLUG_MATCH_ANY, product_id=HOTPLUG_MATCH_ANY, dev_c...
Registers an hotplug callback. On success, returns an opaque value which can be passed to hotplugDeregisterCallback. Callback must accept the following positional arguments: - this USBContext instance - an USBDevice instance If device has left, configuration descriptors...
2.893618
3.05529
0.947085
del self.__hotplug_callback_dict[handle] libusb1.libusb_hotplug_deregister_callback(self.__context_p, handle)
def hotplugDeregisterCallback(self, handle)
Deregisters an hotplug callback. handle (opaque) Return value of a former hotplugRegisterCallback call.
5.232378
6.202556
0.843584
arr = create_string_buffer(length) return i2c_msg( addr=address, flags=I2C_M_RD, len=length, buf=arr)
def read(address, length)
Prepares an i2c read transaction. :param address: Slave address. :type: address: int :param length: Number of bytes to read. :type: length: int :return: New :py:class:`i2c_msg` instance for read operation. :rtype: :py:class:`i2c_msg`
6.289278
5.839747
1.076978
if sys.version_info.major >= 3: if type(buf) is str: buf = bytes(map(ord, buf)) else: buf = bytes(buf) else: if type(buf) is not str: buf = ''.join([chr(x) for x in buf]) arr = create_string_buffer(buf, ...
def write(address, buf)
Prepares an i2c write transaction. :param address: Slave address. :type address: int :param buf: Bytes to write. Either list of values or str. :type buf: list :return: New :py:class:`i2c_msg` instance for write operation. :rtype: :py:class:`i2c_msg`
3.174531
2.961807
1.071822
n_msg = len(i2c_msg_instances) msg_array = (i2c_msg * n_msg)(*i2c_msg_instances) return i2c_rdwr_ioctl_data( msgs=msg_array, nmsgs=n_msg )
def create(*i2c_msg_instances)
Factory method for creating a i2c_rdwr_ioctl_data struct that can be called with ``ioctl(fd, I2C_RDWR, data)``. :param i2c_msg_instances: Up to 42 i2c_msg instances :rtype: i2c_rdwr_ioctl_data
4.014223
2.922711
1.373459
self.fd = os.open("/dev/i2c-{}".format(bus), os.O_RDWR) self.funcs = self._get_funcs()
def open(self, bus)
Open a given i2c bus. :param bus: i2c bus number (e.g. 0 or 1) :type bus: int
4.004024
4.238814
0.944609
if self.fd: os.close(self.fd) self.fd = None
def close(self)
Close the i2c connection.
3.765283
3.264997
1.153227
force = force if force is not None else self.force if self.address != address or self._force_last != force: if force is True: ioctl(self.fd, I2C_SLAVE_FORCE, address) else: ioctl(self.fd, I2C_SLAVE, address) self.address = addr...
def _set_address(self, address, force=None)
Set i2c slave address to use for subsequent calls. :param address: :type address: int :param force: :type force: Boolean
2.986325
2.853611
1.046507
f = c_uint32() ioctl(self.fd, I2C_FUNCS, f) return f.value
def _get_funcs(self)
Returns a 32-bit value stating supported I2C functions. :rtype: int
8.86743
4.828413
1.83651
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_WRITE, command=0, size=I2C_SMBUS_QUICK) ioctl(self.fd, I2C_SMBUS, msg)
def write_quick(self, i2c_addr, force=None)
Perform quick transaction. Throws IOError if unsuccessful. :param i2c_addr: i2c address :type i2c_addr: int :param force: :type force: Boolean
5.247968
5.919812
0.886509
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_READ, command=0, size=I2C_SMBUS_BYTE ) ioctl(self.fd, I2C_SMBUS, msg) return msg.data.contents.byte
def read_byte(self, i2c_addr, force=None)
Read a single byte from a device. :rtype: int :param i2c_addr: i2c address :type i2c_addr: int :param force: :type force: Boolean :return: Read byte value
5.489842
6.485455
0.846485
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_WRITE, command=value, size=I2C_SMBUS_BYTE ) ioctl(self.fd, I2C_SMBUS, msg)
def write_byte(self, i2c_addr, value, force=None)
Write a single byte to a device. :param i2c_addr: i2c address :type i2c_addr: int :param value: value to write :type value: int :param force: :type force: Boolean
5.229446
5.983099
0.874036
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_READ, command=register, size=I2C_SMBUS_BYTE_DATA ) ioctl(self.fd, I2C_SMBUS, msg) return msg.data.contents.byte
def read_byte_data(self, i2c_addr, register, force=None)
Read a single byte from a designated register. :param i2c_addr: i2c address :type i2c_addr: int :param register: Register to read :type register: int :param force: :type force: Boolean :return: Read byte value :rtype: int
5.558371
6.560809
0.847208
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_WRITE, command=register, size=I2C_SMBUS_BYTE_DATA ) msg.data.contents.byte = value ioctl(self.fd, I2C_SMBUS, msg)
def write_byte_data(self, i2c_addr, register, value, force=None)
Write a byte to a given register. :param i2c_addr: i2c address :type i2c_addr: int :param register: Register to write to :type register: int :param value: Byte value to transmit :type value: int :param force: :type force: Boolean :rtype: None
5.429119
6.132092
0.885362
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_READ, command=register, size=I2C_SMBUS_WORD_DATA ) ioctl(self.fd, I2C_SMBUS, msg) return msg.data.contents.word
def read_word_data(self, i2c_addr, register, force=None)
Read a single word (2 bytes) from a given register. :param i2c_addr: i2c address :type i2c_addr: int :param register: Register to read :type register: int :param force: :type force: Boolean :return: 2-byte word :rtype: int
5.610752
6.671994
0.840941
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_WRITE, command=register, size=I2C_SMBUS_PROC_CALL ) msg.data.contents.word = value ioctl(self.fd, I2C_SMBUS, msg) return msg.data.contents.word
def process_call(self, i2c_addr, register, value, force=None)
Executes a SMBus Process Call, sending a 16-bit value and receiving a 16-bit response :param i2c_addr: i2c address :type i2c_addr: int :param register: Register to read/write to :type register: int :param value: Word value to transmit :type value: int :param forc...
5.174578
5.214519
0.99234
self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_READ, command=register, size=I2C_SMBUS_BLOCK_DATA ) ioctl(self.fd, I2C_SMBUS, msg) length = msg.data.contents.block[0] return msg.data.contents.block[1:le...
def read_block_data(self, i2c_addr, register, force=None)
Read a block of up to 32-bytes from a given register. :param i2c_addr: i2c address :type i2c_addr: int :param register: Start register :type register: int :param force: :type force: Boolean :return: List of bytes :rtype: list
4.666921
5.31718
0.877706
length = len(data) if length > I2C_SMBUS_BLOCK_MAX: raise ValueError("Data length cannot exceed %d bytes" % I2C_SMBUS_BLOCK_MAX) self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_WRITE, command=register, size...
def write_block_data(self, i2c_addr, register, data, force=None)
Write a block of byte data to a given register. :param i2c_addr: i2c address :type i2c_addr: int :param register: Start register :type register: int :param data: List of bytes :type data: list :param force: :type force: Boolean :rtype: None
3.286627
3.411308
0.963451
if length > I2C_SMBUS_BLOCK_MAX: raise ValueError("Desired block length over %d bytes" % I2C_SMBUS_BLOCK_MAX) self._set_address(i2c_addr, force=force) msg = i2c_smbus_ioctl_data.create( read_write=I2C_SMBUS_READ, command=register, size=I2C_SMBUS_I2C_BLOCK_DATA ...
def read_i2c_block_data(self, i2c_addr, register, length, force=None)
Read a block of byte data from a given register. :param i2c_addr: i2c address :type i2c_addr: int :param register: Start register :type register: int :param length: Desired block length :type length: int :param force: :type force: Boolean :return:...
4.298564
4.432775
0.969723
ioctl_data = i2c_rdwr_ioctl_data.create(*i2c_msgs) ioctl(self.fd, I2C_RDWR, ioctl_data)
def i2c_rdwr(self, *i2c_msgs)
Combine a series of i2c read and write operations in a single transaction (with repeated start bits but no stop bits in between). This method takes i2c_msg instances as input, which must be created first with :py:meth:`i2c_msg.read` or :py:meth:`i2c_msg.write`. :param i2c_msgs: One or ...
3.908613
4.150551
0.941709
cells = list(cellmap.values()) rows = 0 cols = 0 for cell in cells: if sheet is None or cell.sheet == sheet: rows = max(rows, int(cell.row)) cols = max(cols, int(col2num(cell.col))) return (rows, cols)
def max_dimension(cellmap, sheet = None)
This function calculates the maximum dimension of the workbook or optionally the worksheet. It returns a tupple of two integers, the first being the rows and the second being the columns. :param cellmap: all the cells that should be used to calculate the maximum. :param sheet: (optionally) a string with t...
2.971114
2.97627
0.998267
if isinstance(array, (float, int)): rows = 1 # special case for A1:A1 type ranges which for some reason only return an int/float elif array is None: rows = 1 # some A1:A1 ranges return None (issue with ref cell) else: rows = len(array.values) return rows
def rows(array)
Function to find the number of rows in an array. Excel reference: https://support.office.com/en-ie/article/rows-function-b592593e-3fc2-47f2-bec1-bda493811597 :param array: the array of which the rows should be counted. :return: the number of rows.
9.43884
8.898951
1.060669
if isinstance(values, Range): values = values.values if guess is not None and guess != 0: raise ValueError('guess value for excellib.irr() is %s and not 0' % guess) else: try: return np.irr(values) except Exception as e: return ExcelError('#NUM!'...
def irr(values, guess = None)
Function to calculate the internal rate of return (IRR) using payments and periodic dates. It resembles the excel function IRR(). Excel reference: https://support.office.com/en-us/article/IRR-function-64925eaa-9988-495b-b290-3ad0c163c1bc :param values: the payments of which at least one has to be negative...
6.062767
6.546112
0.926163
if isinstance(values, Range): values = values.values if isinstance(dates, Range): dates = dates.values if guess is not None and guess != 0: raise ValueError('guess value for excellib.irr() is %s and not 0' % guess) else: try: return scipy.optimize.newt...
def xirr(values, dates, guess=0)
Function to calculate the internal rate of return (IRR) using payments and non-periodic dates. It resembles the excel function XIRR(). Excel reference: https://support.office.com/en-ie/article/xirr-function-de1242ec-6477-445b-b11b-a303ad9adc9d :param values: the payments of which at least one has to be ne...
3.897136
3.736866
1.042889
values = values.values if isinstance(dates, Range): dates = dates.values if len(values) != len(dates): return ExcelError('#NUM!', '`values` range must be the same length as `dates` range in XNPV, %s != %s' % (len(values), len(dates))) if lim_rate and rate < 0: return Excel...
def xnpv(rate, values, dates, lim_rate = True): # Excel reference: https://support.office.com/en-us/article/XNPV-function-1b42bbf6-370f-4532-a0eb-d67c16b664b7 if isinstance(values, Range)
Function to calculate the net present value (NPV) using payments and non-periodic dates. It resembles the excel function XPNV(). :param rate: the discount rate. :param values: the payments of which at least one has to be negative. :param dates: the dates as excel dates (e.g. 43571 for 16/04/2019). :ret...
3.346855
3.849259
0.86948
reference_date = datetime.datetime.today().date() days_since_epoch = reference_date - EXCEL_EPOCH # why +2 ? # 1 based from 1900-01-01 # I think it is "inclusive" / to the _end_ of the day. # https://support.office.com/en-us/article/date-function-e36c0c8c-4104-49da-ab83-82328b832349 ret...
def today()
Note: Excel stores dates as sequential serial numbers so that they can be used in calculations. January 1, 1900 is serial number 1, and January 1, 2008 is serial number 39448 because it is 39,447 days after January 1, 1900. You will need to change the number format (Format Cells) in order to display a proper d...
8.462631
8.446766
1.001878
sio = BytesIO(data) sio.seek(pos + 22) # size of 'ZIP end of central directory record' sio.truncate() sio.seek(0) return sio f.seek(0) return f
def repair_central_directory(zipFile, is_file_instance): # source: https://bitbucket.org/openpyxl/openpyxl/src/93604327bce7aac5e8270674579af76d390e09c0/openpyxl/reader/excel.py?at=default&fileviewer=file-view-default ''' trims trailing data from the central directory code taken from http://stackoverflow.com/a/7...
trims trailing data from the central directory code taken from http://stackoverflow.com/a/7457686/570216, courtesy of Uri Cohen
5.851758
4.994287
1.17169
return float(value) return int(value)
def _cast_number(value): # source: https://bitbucket.org/openpyxl/openpyxl/src/93604327bce7aac5e8270674579af76d390e09c0/openpyxl/cell/read_only.py?at=default&fileviewer=file-view-default "Convert numbers as string to an int or float" m = FLOAT_REGEX.search(value) if m is not None
Convert numbers as string to an int or float
13.411893
4.563338
2.939053
xml_source = archive.read(ARC_WORKBOOK_RELS) tree = fromstring(xml_source) for element in safe_iterator(tree, '{%s}Relationship' % PKG_REL_NS): rId = element.get('Id') pth = element.get("Target") typ = element.get('Type') # normalise path if pth.startswith("/xl")...
def read_rels(archive)
Read relationships for a workbook
3.739829
3.459215
1.081121
xml_source = archive.read(ARC_CONTENT_TYPES) root = fromstring(xml_source) contents_root = root.findall('{%s}Override' % CONTYPES_NS) for type in contents_root: yield type.get('ContentType'), type.get('PartName')
def read_content_types(archive)
Read content types.
5.308389
4.960901
1.070045
xml_source = archive.read(ARC_WORKBOOK) tree = fromstring(xml_source) for element in safe_iterator(tree, '{%s}sheet' % SHEET_MAIN_NS): attrib = element.attrib attrib['id'] = attrib["{%s}id" % REL_NS] del attrib["{%s}id" % REL_NS] if attrib['id']: yield attrib
def read_sheets(archive)
Read worksheet titles and ids for a workbook
4.430213
4.02737
1.100026
# content types has a list of paths but no titles # workbook has a list of titles and relIds but no paths # workbook_rels has a list of relIds and paths but no titles # rels = {'id':{'title':'', 'path':''} } content_types = read_content_types(archive) valid_sheets = dict((path, ct) for ct, ...
def detect_worksheets(archive)
Return a list of worksheets
5.9243
5.896298
1.004749
strings = [] src = _get_xml_iter(xml_source) for _, node in iterparse(src): if node.tag == '{%s}si' % SHEET_MAIN_NS: text = Text.from_tree(node).content text = text.replace('x005F_', '') strings.append(text) node.clear() return IndexedList...
def read_string_table(xml_source)
Read in all shared strings in the table
6.13244
5.376578
1.140584
if not hasattr(xml_source, 'read'): try: xml_source = xml_source.encode("utf-8") except (AttributeError, UnicodeDecodeError): pass return BytesIO(xml_source) else: try: xml_source.seek(0) except: # could be a zipfile ...
def _get_xml_iter(xml_source)
Possible inputs: strings, bytes, members of zipfile, temporary file Always return a file like object
2.77604
2.476501
1.120953
if t.ttype == "operand": if t.tsubtype in ["range", "named_range", "pointer"] : # print 'Creating Node', t.tvalue, t.tsubtype return RangeNode(t, ref, debug = debug) else: return OperandNode(t) elif t.ttype == "function": return FunctionNode(t, re...
def create_node(t, ref = None, debug = False)
Simple factory function
3.404227
3.427168
0.993306
#use a directed graph to store the tree G = DiGraph() stack = [] for n in expression: # Since the graph does not maintain the order of adding nodes/edges # add an extra attribute 'pos' so we can always sort to the correct order if isinstance(n,OperatorNode): if...
def build_ast(expression, debug = False)
build an AST from an Excel formula expression in reverse polish notation
3.695835
3.612296
1.023126
if cell.formula: debug = False # if 'OFFSET' in cell.formula or 'INDEX' in cell.formula: # debug = True # if debug: # print 'FORMULA', cell.formula ref = parse_cell_address(cell.address()) if not cell.is_named_range else None sheet = cell.sheet ...
def cell2code(cell, named_ranges)
Generate python code for the given cell
5.454636
5.566112
0.979972
if self._changes: return self._changes.pop() action_file = None if self._changed: self._changed = False action_file = self._action_file or True # TODO: Hack (see above) return action_file, None
def examine(self)
Called by LiveReloadHandler's poll_tasks method. If a boolean true value is returned, then the waiters (browsers) are reloaded.
10.380184
9.840245
1.05487
if action is None: action = _set_changed event_handler = _WatchdogHandler(self, action) self._observer.schedule(event_handler, path=path, recursive=True)
def watch(self, path, action, *args, **kwargs)
Called by the Server instance when a new watch task is requested.
5.380968
5.965077
0.902079
fmt_re = re.compile(r'([^<]+) <([^>]+)>') authors = local('git shortlog -s -e -n | cut -f 2-', capture=True) with open('AUTHORS', 'w') as fh: fh.write('Project contributors\n') fh.write('====================\n\n') for line in authors.splitlines(): match = fmt_re.matc...
def authors()
Updates the AUTHORS file with a list of committers from GIT.
3.643766
3.250793
1.120885
if not is_working_tree_clean(): print('Your working tree is not clean. Refusing to create a release.') return print('Rebuilding the AUTHORS file to check for modifications...') authors() if not is_working_tree_clean(): print('Your working tree is not clean after the AUTHO...
def release()
Create a new release and upload it to PyPI.
3.93269
3.825412
1.028044
if self.is_terminal: if index == len(iterable) or \ (gather and index < len(iterable) and iterable[index] == ' '): # only gather on word break) confidence = float(len(self.key) - edit_distance) / float(max(len(self.key), index)) if confid...
def lookup(self, iterable, index=0, gather=False, edit_distance=0, max_edit_distance=0, match_threshold=0.0, matched_length=0)
TODO: Implement trie lookup with edit distance Args: iterable(list?): key used to find what is requested this could be a generator. index(int): index of what is requested gather(bool): of weather to gather or not edit_distance(int): the distance -...
2.245424
2.31127
0.971511
if index == len(iterable): self.is_terminal = True self.key = iterable self.weight = weight if data: self.data.add(data) else: if iterable[index] not in self.children: self.children[iterable[index]] = Tr...
def insert(self, iterable, index=0, data=None, weight=1.0)
Insert new node into tree Args: iterable(hashable): key used to find in the future. data(object): data associated with the key index(int): an index used for insertion. weight(float): the wait given for the item added.
2.860552
2.803782
1.020247
if index == len(iterable): if self.is_terminal: if data: self.data.remove(data) if len(self.data) == 0: self.is_terminal = False else: self.data.clear() se...
def remove(self, iterable, data=None, index=0)
Remove an element from the trie Args iterable(hashable): key used to find what is to be removed data(object): data associated with the key index(int): index of what is to me removed Returns: bool: True: if it was removed False: if it ...
2.322487
2.268574
1.023765
for result in self.lookup(iterable, gather=True): yield result
def gather(self, iterable)
Calls the lookup with gather True Passing iterable and yields the result.
13.173537
3.79892
3.467706
for result in self.root.lookup(iterable, gather=gather, edit_distance=0, max_edit_distance=self.max_edit_distance, match_threshold=self.match_thres...
def lookup(self, iterable, gather=False)
Call the lookup on the root node with the given parameters. Args iterable(index or key): Used to retrive nodes from tree gather(bool): this is passed down to the root node lookup Notes: max_edit_distance and match_threshold come from the init
4.35936
3.406296
1.279795
self.root.insert(iterable, index=0, data=data, weight=1.0)
def insert(self, iterable, data=None, weight=1.0)
Used to insert into he root node Args iterable(hashable): index or key used to identify data(object): data to be paired with the key
5.539924
5.327207
1.03993
return self.root.remove(iterable, data=data)
def remove(self, iterable, data=None)
Used to remove from the root node Args: iterable(hashable): index or key used to identify item to remove data: data to be paired with the key
8.40935
10.643232
0.790112
if len(p) == 0 and len(x) == 0: yield r return for vertex in p[:]: r_new = r[::] r_new.append(vertex) p_new = [val for val in p if val in graph.get_neighbors_of(vertex)] # p intersects N(vertex) x_new = [val for val in x if val in graph.get_neighbors_of(verte...
def bronk(r, p, x, graph)
This is used to fine cliques and remove them from graph Args: graph (graph): this is the graph of verticies to search for cliques p (list): this is a list of the verticies to search r (list): used by bronk for the search x (list): used by bronk for the search Yields...
2.618146
2.760106
0.948567
start_token = tag.get('start_token') entity = tag.get('entities', [])[entity_index] return str(start_token) + '-' + entity.get('key') + '-' + str(entity.get('confidence'))
def graph_key_from_tag(tag, entity_index)
Returns a key from a tag entity Args: tag (tag) : this is the tag selected to get the key from entity_index (int) : this is the index of the tagged entity Returns: str : String representing the key for the given tagged entity.
4.354475
4.392239
0.991402
neighbors_of_a = self.adjacency_lists.get(a) if not neighbors_of_a: neighbors_of_a = set() self.adjacency_lists[a] = neighbors_of_a neighbors_of_a.add(b) neighbors_of_b = self.adjacency_lists.get(b) if not neighbors_of_b: neighbors_o...
def add_edge(self, a, b)
Used to add edges to the graph. 'a' and 'b' are vertexes and if 'a' or 'b' doesn't exisit then the vertex is created Args: a (hash): is one vertex of the edge b (hash): is another vertext of the edge
1.463075
1.610925
0.90822
if isinstance(data, list) and len(data) > 0: self.nodes.append(data) else: self.nodes.append([data])
def append(self, data)
Appends items or lists to the Lattice Args: data (item,list) : The Item or List to be added to the Lattice
3.109026
3.535071
0.879481
if index < len(self.nodes): for entity in self.nodes[index]: for next_result in self.traverse(index=index+1): if isinstance(entity, list): yield entity + next_result else: yield [entity] ...
def traverse(self, index=0)
This is used to produce a list of lists where each each item in that list is a diffrent combination of items from the lists within with every combination of such values. Args: index (int) : the index at witch to start the list. Note this is used only in the function ...
3.08635
3.522023
0.8763
graph = SimpleGraph() for tag_index in xrange(len(tags)): for entity_index in xrange(len(tags[tag_index].get('entities'))): a_entity_name = graph_key_from_tag(tags[tag_index], entity_index) tokens = self.tokenizer.tokenize(tags[tag_index].get('entitie...
def _build_graph(self, tags)
Builds a graph from the entities included in the tags. Note this is used internally. Args: tags (list): A list of the tags to include in graph Returns: graph : this is the resulting graph of the tagged entities.
2.771345
2.823544
0.981513
entities = {} graph = self._build_graph(tags) # name entities for tag in tags: for entity_index in xrange(len(tag.get('entities'))): node_name = graph_key_from_tag(tag, entity_index) if not node_name in entities: e...
def _sub_expand(self, tags)
This called by expand to find cliques Args: tags (list): a list of the tags used to get cliques Yields: list : list of sorted tags by start_token this is a clique
3.066272
2.954199
1.037937
lattice = Lattice() overlapping_spans = [] def end_token_index(): return max([t.get('end_token') for t in overlapping_spans]) for i in xrange(len(tags)): tag = tags[i] if len(overlapping_spans) > 0 and end_token_index() >= tag.get('start_to...
def expand(self, tags, clique_scoring_func=None)
This is the main function to expand tags into cliques Args: tags (list): a list of tags to find the cliques. clique_scoring_func (func): a function that returns a float value for the clique Returns: list : a list of cliques
2.240865
2.298437
0.974952
for start_idx in xrange(len(tokens)): for end_idx in xrange(start_idx + 1, len(tokens) + 1): yield ' '.join(tokens[start_idx:end_idx]), start_idx
def _iterate_subsequences(self, tokens)
Using regex invokes this function, which significantly impacts performance of adapt. it is an N! operation. Args: tokens(list): list of tokens for Yield results. Yields: str: ?
2.20794
2.680198
0.823797
tokens = self.tokenizer.tokenize(utterance) entities = [] if len(self.regex_entities) > 0: for part, idx in self._iterate_subsequences(tokens): local_trie = Trie() for regex_entity in self.regex_entities: match = regex_enti...
def tag(self, utterance, context_trie=None)
Tag known entities within the utterance. Args: utterance(str): a string of natural language text context_trie(trie): optional, a trie containing only entities from context for this request Returns: dictionary, with the following keys match(str): the p...
2.538715
2.480811
1.023341
best_intent = None best_tags = None context_as_entities = [{'entities': [c]} for c in context] for intent in self.intent_parsers: i, tags = intent.validate_with_tags(parse_result.get('tags') + context_as_entities, parse_result.get('confidence')) if not be...
def __best_intent(self, parse_result, context=[])
Decide the best intent Args: parse_result(list): results used to match the best intent. context(list): ? Returns: best_intent, best_tags: best_intent : The best intent for given results best_tags : The Tags for result
3.437992
3.699106
0.929411
tags_keys = set([t['key'] for t in parse_result['tags'] if t['from_context']]) result_context = [c for c in context if c['key'] not in tags_keys] return result_context
def __get_unused_context(self, parse_result, context)
Used to get unused context from context. Any keys not in parse_result Args: parse_results(list): parsed results used to identify what keys in the context are used. context(list): this is the context used to match with parsed results keys missing ...
4.387712
5.329299
0.823319
parser = Parser(self.tokenizer, self.tagger) parser.on('tagged_entities', (lambda result: self.emit("tagged_entities", result))) context = [] if context_manager: context = context_manager.get_context() for result in pars...
def determine_intent(self, utterance, num_results=1, include_tags=False, context_manager=None)
Given an utterance, provide a valid intent. Args: utterance(str): an ascii or unicode string representing natural language speech include_tags(list): includes the parsed tags (including position and confidence) as part of result context_manager(list): a conte...
4.194077
4.499226
0.932178
if alias_of: self.trie.insert(entity_value.lower(), data=(alias_of, entity_type)) else: self.trie.insert(entity_value.lower(), data=(entity_value, entity_type)) self.trie.insert(entity_type.lower(), data=(entity_type, 'Concept'))
def register_entity(self, entity_value, entity_type, alias_of=None)
Register an entity to be tagged in potential parse results Args: entity_value(str): the value/proper name of an entity instance (Ex: "The Big Bang Theory") entity_type(str): the type/tag of an entity instance (Ex: "Television Show")
2.936268
3.020257
0.972191
if regex_str and regex_str not in self._regex_strings: self._regex_strings.add(regex_str) self.regular_expressions_entities.append(re.compile(regex_str, re.IGNORECASE))
def register_regex_entity(self, regex_str)
A regular expression making use of python named group expressions. Example: (?P<Artist>.*) regex_str(str): a string representing a regular expression as defined above
3.103546
3.413008
0.909329
if hasattr(intent_parser, 'validate') and callable(intent_parser.validate): self.intent_parsers.append(intent_parser) else: raise ValueError("%s is not an intent parser" % str(intent_parser))
def register_intent_parser(self, intent_parser)
"Enforce" the intent parser interface at registration time. Args: intent_parser(intent): Intent to be registered. Raises: ValueError: on invalid intent
2.613722
2.960719
0.8828
domain = 0 if domain not in self.domains: self.register_domain(domain=domain) return self.domains[domain].tokenizer
def tokenizer(self)
A property to link into IntentEngine's tokenizer. Warning: this is only for backwards compatiblility and should not be used if you intend on using domains. Return: the domains tokenizer from its IntentEngine
6.922761
6.418225
1.07861
domain = 0 if domain not in self.domains: self.register_domain(domain=domain) return self.domains[domain].trie
def trie(self)
A property to link into IntentEngine's trie. warning:: this is only for backwards compatiblility and should not be used if you intend on using domains. Return: the domains trie from its IntentEngine
7.064995
6.226223
1.134716
domain = 0 if domain not in self.domains: self.register_domain(domain=domain) return self.domains[domain].tagger
def tagger(self)
A property to link into IntentEngine's intent_parsers. Warning: this is only for backwards compatiblility and should not be used if you intend on using domains. Return: the domains intent_parsers from its IntentEngine
6.518908
6.391646
1.019911
domain = 0 if domain not in self.domains: self.register_domain(domain=domain) return self.domains[domain].intent_parsers
def intent_parsers(self)
A property to link into IntentEngine's intent_parsers. Warning: this is only for backwards compatiblility and should not be used if you intend on using domains. Returns: the domains intent_parsers from its IntentEngine
6.297359
5.634366
1.11767
domain = 0 if domain not in self.domains: self.register_domain(domain=domain) return self.domains[domain]._regex_strings
def _regex_strings(self)
A property to link into IntentEngine's _regex_strings. Warning: this is only for backwards compatiblility and should not be used if you intend on using domains. Returns: the domains _regex_strings from its IntentEngine
7.907797
5.970089
1.324569
domain = 0 if domain not in self.domains: self.register_domain(domain=domain) return self.domains[domain].regular_expressions_entities
def regular_expressions_entities(self)
A property to link into IntentEngine's regular_expressions_entities. Warning: this is only for backwards compatiblility and should not be used if you intend on using domains. Returns: the domains regular_expression_entities from its IntentEngine
5.872503
5.025619
1.168514
self.domains[domain] = IntentDeterminationEngine( tokenizer=tokenizer, trie=trie)
def register_domain(self, domain=0, tokenizer=None, trie=None)
Register a domain with the intent engine. Args: tokenizer(tokenizer): The tokenizer you wish to use. trie(Trie): the Trie() you wish to use. domain(str): a string representing the domain you wish to add
9.347504
7.738903
1.207859
if domain not in self.domains: self.register_domain(domain=domain) self.domains[domain].register_entity(entity_value=entity_value, entity_type=entity_type, alias_of=alias_of)
def register_entity(self, entity_value, entity_type, alias_of=None, domain=0)
Register an entity to be tagged in potential parse results. Args: entity_value(str): the value/proper name of an entity instance (Ex: "The Big Bang Theory") entity_type(str): the type/tag of an entity instance (Ex: "Television Show") domain(str): a string rep...
2.33278
2.618221
0.890979
if domain not in self.domains: self.register_domain(domain=domain) self.domains[domain].register_regex_entity(regex_str=regex_str)
def register_regex_entity(self, regex_str, domain=0)
A regular expression making use of python named group expressions. Example: (?P<Artist>.*) Args: regex_str(str): a string representing a regular expression as defined above domain(str): a string representing the domain you wish to add the entity to
3.008236
3.24566
0.926849
intents = [] for domain in self.domains: gen = self.domains[domain].determine_intent(utterance=utterance, num_results=1) for intent in gen: intents.append(intent) heapq.nlargest( ...
def determine_intent(self, utterance, num_results=1)
Given an utterance, provide a valid intent. utterance(str): an ascii or unicode string representing natural language speech num_results(int): a maximum number of results to be returned. Returns: A generator the yields dictionaries.
3.757137
3.806791
0.986956
if domain not in self.domains: self.register_domain(domain=domain) self.domains[domain].register_intent_parser( intent_parser=intent_parser)
def register_intent_parser(self, intent_parser, domain=0)
Register a intent parser with a domain. Args: intent_parser(intent): The intent parser you wish to register. domain(str): a string representing the domain you wish register the intent parser to.
3.246459
3.249063
0.999199
s = string s = re.sub('\t', " ", s) s = re.sub("(" + regex_separator + ")", " \g<1> ", s) s = re.sub("([^0-9]),", "\g<1> , ", s) s = re.sub(",([^0-9])", " , \g<1>", s) s = re.sub("^(')", "\g<1> ", s) s = re.sub("(" + regex_not_letter_number + ")'", "\g<1>...
def tokenize(self, string)
Used to parce a string into tokens This function is to take in a string and return a list of tokens Args: string(str): This is a string of words or a sentance to be parsed into tokens Returns: list: a list of tokens from the string passed in. Notes: ...
2.744619
2.845888
0.964416
start = time.time() context_trie = None if context and isinstance(context, list): # sort by confidence in ascending order, so # highest confidence for an entity is last. # see comment on TrieNode ctor context.sort(key=lambda x: x.get('conf...
def parse(self, utterance, context=None, N=1)
Used to find tags within utterance with a given confidence Args: utterance(str): conversational piece given by the user context(list): a list of entities N(int): number of results Returns: yield an object with the following fields utterance(str): the valu...
3.47548
3.354514
1.036061
result = len(query.keys()) > 0 for key in query.keys(): result = result and query[key] == self.metadata.get(key) return result
def metadata_matches(self, query={})
Returns key matches to metadata This will check every key in query for a matching key in metadata returning true if every key is in metadata. query without keys return false. Args: query(object): metadata for matching Returns: bool: Tru...
3.733379
4.098514
0.910911
self.entities.append(tag) for k in metadata.keys(): if k not in self.metadata: self.metadata[k] = k
def merge_context(self, tag, metadata)
merge into contextManagerFrame new entity and metadata. Appends tag as new entity and adds keys in metadata to keys in self.metadata. Args: tag(str): entity to be added to self.entities metadata(object): metadata containes keys to be added to self.metadata
4.423356
3.177904
1.39191
top_frame = self.frame_stack[0] if len(self.frame_stack) > 0 else None if top_frame and top_frame.metadata_matches(metadata): top_frame.merge_context(entity, metadata) else: frame = ContextManagerFrame(entities=[entity], metadata=metadata.copy()) self...
def inject_context(self, entity, metadata={})
Args: entity(object): format {'data': 'Entity tag as <str>', 'key': 'entity proper name as <str>', 'confidence': <float>' } metadata(object): dict, arbitrary metadata about the entity being added
3.279224
3.40365
0.963443
if not max_frames or max_frames > len(self.frame_stack): max_frames = len(self.frame_stack) missing_entities = list(missing_entities) context = [] for i in xrange(max_frames): frame_entities = [entity.copy() for entity in self.frame_stack[i].entities] ...
def get_context(self, max_frames=None, missing_entities=[])
Constructs a list of entities from the context. Args: max_frames(int): maximum number of frames to look back missing_entities(list of str): a list or set of tag names, as strings Returns: list: a list of entities
3.493015
3.538323
0.987195
for tag in tags: for entity in tag.get('entities'): for v, t in entity.get('data'): if t.lower() == entity_type.lower() and tag.get('start_token', 0) > after_index: return tag, v, entity.get('confidence') return None, None, None
def find_first_tag(tags, entity_type, after_index=-1)
Searches tags for entity type after given index Args: tags(list): a list of tags with entity types to be compaired too entity_type entity_type(str): This is he entity type to be looking for in tags after_index(int): the start token must be greaterthan this. Returns: ( tag, v, c...
3.852424
3.300703
1.167153
if len(lists) == 0: yield [] else: for el in lists[0]: for next_list in choose_1_from_each(lists[1:]): yield [el] + next_list
def choose_1_from_each(lists)
Takes a list of lists and returns a list of lists with one item from each list. This new list should be the length of each list multiplied by the others. 18 for an list with lists of 3, 2 and 3. Also the lenght of each sub list should be same as the length of lists passed in. Args: lists(lis...
2.331907
3.009236
0.774917
if len(tags) < len(at_least_one): return None for possible_resolution in choose_1_from_each(at_least_one): resolution = {} pr = possible_resolution[:] for entity_type in pr: last_end_index = -1 if entity_type in resolution: last_end_in...
def resolve_one_of(tags, at_least_one)
This searches tags for Entites in at_least_one and returns any match Args: tags(list): List of tags with Entities to search for Entities at_least_one(list): List of Entities to find in tags Returns: object: returns None if no match is found but returns any match as an object
3.767255
3.915563
0.962124
intent, tags = self.validate_with_tags(tags, confidence) return intent
def validate(self, tags, confidence)
Using this method removes tags from the result of validate_with_tags Returns: intent(intent): Resuts from validate_with_tags
9.536583
7.887312
1.209104
result = {'intent_type': self.name} intent_confidence = 0.0 local_tags = tags[:] used_tags = [] for require_type, attribute_name in self.requires: required_tag, canonical_form, confidence = find_first_tag(local_tags, require_type) if not required...
def validate_with_tags(self, tags, confidence)
Validate weather tags has required entites for this intent to fire Args: tags(list): Tags and Entities used for validation confidence(float): ? Returns: intent, tags: Returns intent and tags used by the intent on falure to meat required entities then...
2.905178
2.947702
0.985574
if not attribute_name: attribute_name = entity_type self.requires += [(entity_type, attribute_name)] return self
def require(self, entity_type, attribute_name=None)
The intent parser should require an entity of the provided type. Args: entity_type(str): an entity type attribute_name(str): the name of the attribute on the parsed intent. Defaults to match entity_type. Returns: self: to continue modifications.
3.49835
4.102166
0.852806
if not attribute_name: attribute_name = entity_type self.optional += [(entity_type, attribute_name)] return self
def optionally(self, entity_type, attribute_name=None)
Parsed intents from this parser can optionally include an entity of the provided type. Args: entity_type(str): an entity type attribute_name(str): the name of the attribute on the parsed intent. Defaults to match entity_type. Returns: self: to continue modifications...
3.62459
4.0535
0.894188
return Intent(self.name, self.requires, self.at_least_one, self.optional)
def build(self)
Constructs an intent from the builder's specifications. :return: an Intent instance.
12.18961
8.963562
1.359907
# note subsets have an unusual encoding query = .format(s=subset, g=self.graph_name) bindings = run_sparql(query) return [r['c']['value'] for r in bindings]
def extract_subset(self, subset)
Find all nodes in a subset. We assume the oboInOwl encoding of subsets, and subset IDs are IRIs
13.395891
11.137477
1.202776
# note subsets have an unusual encoding query = .format(g=self.graph_name) bindings = run_sparql(query) return [r['s']['value'] for r in bindings]
def subsets(self)
Find all subsets for an ontology
16.469627
13.452962
1.224238
# TODO: DRY with sparql_ontol_utils searchterm = searchterm.replace('%','.*') namedGraph = get_named_graph(self.handle) query = .format(pred=pred, s=searchterm, g=namedGraph) bindings = run_sparql(query) return [r['c']['value'] for r in bindings]
def _search(self, searchterm, pred, **args)
Search for things using labels
7.482769
7.244126
1.032943
if inject_prefixes is None: inject_prefixes = [] namedGraph = get_named_graph(self.handle) cols = [] select_val = None if select is None or select=='*': if not single_column: cols=None select_val='*' else: ...
def sparql(self, select='*', body=None, inject_prefixes=None, single_column=False)
Execute a SPARQL query. The query is specified using `select` and `body` parameters. The argument for the Named Graph is injected into the query. The select parameter should be either '*' or a list of vars (not prefixed with '?'). - If '*' is passed, then the result is a list of dict...
3.187829
3.232516
0.986176
results = search_associations(objects=idlist, subject_taxon=taxon, subject_category=subject_category, select_fields=[M.SUBJECT, M.OBJECT_CLOSURE], facet_fields=[], ...
def term_matrix(idlist, subject_category, taxon, **kwargs)
Intersection between annotated objects P1 not(P1) F1 0 5 not(F1) 6 0
3.147474
3.242021
0.970837
all_ancestors = self.ontology.ancestors(go_term, reflexive=True) subont = self.ontology.subontology(all_ancestors) return subont.ancestors(go_term, relations)
def get_ancestors_through_subont(self, go_term, relations)
Returns the ancestors from the relation filtered GO subontology of go_term's ancestors. subontology() primarily used here for speed when specifying relations to traverse. Point of this is to first get a smaller graph (all ancestors of go_term regardless of relation) and then filter relations on that in...
3.374693
3.064155
1.101345
bp_root = "GO:0008150" if go_term == bp_root: return True ancestors = self.get_isa_closure(go_term) if bp_root in ancestors: return True else: return False
def is_biological_process(self, go_term)
Returns True is go_term has is_a, part_of ancestor of biological process GO:0008150
3.211869
2.723389
1.179365
mf_root = "GO:0003674" if go_term == mf_root: return True ancestors = self.get_isa_closure(go_term) if mf_root in ancestors: return True else: return False
def is_molecular_function(self, go_term)
Returns True is go_term has is_a, part_of ancestor of molecular function GO:0003674
3.407125
2.816953
1.209507
cc_root = "GO:0005575" if go_term == cc_root: return True ancestors = self.get_isa_closure(go_term) if cc_root in ancestors: return True else: return False
def is_cellular_component(self, go_term)
Returns True is go_term has is_a, part_of ancestor of cellular component GO:0005575
3.207734
2.611197
1.228453
if not go_term.startswith("GO:"): return None else: # Check ancestors for root terms if self.is_molecular_function(go_term): return 'F' elif self.is_cellular_component(go_term): return 'C' elif self.is_b...
def go_aspect(self, go_term)
For GO terms, returns F, C, or P corresponding to its aspect
2.907332
2.648968
1.097534
response = self._get_response("graph/neighbors", format="json", **params) return response.json()
def _neighbors_graph(self, **params) -> Dict
Get neighbors of a node parameters are directly passed through to SciGraph: e.g. depth, relationshipType
7.295149
6.217294
1.173364