def __init__(self, url, home_url, name="Website"): """ Instantiates class. """ if not isinstance(url, str): message.logError("Given URL is not a string instance.", "CWebsite::__init__") ut.exit(0) if not isinstance(home_url, str): message.logError("Given home URL is not a string instance.", "CWebsite::__init__") ut.exit(0) # To make us look like a legitimate browser headers = requests.utils.default_headers() headers.update({ 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0' }) response = requests.get(url, headers=headers) self.m_url = url self.m_home_url = home_url self.m_websoup = BeautifulSoup(response.text, "html.parser") self.m_name = name
def getRegTags(self, reg_tag_names): ''' Will return a list of tags with class name equal to one of the names in the list of :param:`class_names`, or is equal to the string :param:`class_names` if :param:`class_names` is a string. :param class_names: A list of string class names to look for, or a string class name to look for. ''' if isinstance(reg_tag_names, list): for reg_tag_name in reg_tag_names: if not isinstance(reg_tag_name, str): message.logError( "Given paramer tag_names must be a list containing only string instances, " + "or must be a string instance.") ut.exit(0) ret = self.m_websoup.findAll( True, {"data-regression-tag": reg_tag_names}) elif isinstance(reg_tag_names, str): ret = self.m_websoup.find_all( attrs={"data-regression-tag": reg_tag_names}) else: message.logError( "Given paramer class_names must be a list containing only string instances, " + "or must be a string instance.") ut.exit(0) return [CTag(tag) for tag in ret]
def getEventsList(location="postcode:BS4 1NL", radius_distance=20, date=None, genre=None): if isinstance(genre, str): genre = genre.lower() if genre not in [g.lower() for g in GENRE_SYNONYMS.keys()]: message.logError("Unknown Genre", "events::getEventsList") ut.exit(0) params = { "location": location, "radius_distance": radius_distance, "distance_unit": "mi", "date": date, "genre": genre } params = {k: v for k, v in params.items() if v is not None} if "genre" in params.keys(): if isinstance(params["genre"], list): new_g = [GENRE_SYNONYMS[g.capitalize()] for g in params["genre"]] new_g = [item for sublist in new_g for item in sublist] params["genre"] = new_g else: new_g = GENRE_SYNONYMS[params["genre"]] params["genre"] = new_g all_params = _generateParams(params) all_events = [] all_ids = [] for p in all_params: evs = _getEvents(p) for ev in evs: if ev["id"] not in all_ids: all_events.append(ev) all_ids.append(ev["id"]) return all_events
def getClasses(self, class_names): """ Will return a list of tags with class name equal to one of the names in the list of :param:`class_names`, or is equal to the string :param:`class_names` if :param:`class_names` is a string. :param class_names: A list of string class names to look for, or a string class name to look for. """ if isinstance(class_names, list): for class_name in class_names: if not isinstance(class_name, str): message.logError( "Given paramer class_names must be a list " "containing only string instances, " "or must be a string instance.") ut.exit(0) ret = self.m_tag.findAll(True, {"class": class_names}) elif isinstance(class_names, str): ret = self.m_tag.findAll(True, {"class": [class_names]}) else: message.logError( "Given paramer class_names must be a list containing " "only string instances, " "or must be a string instance.") ut.exit(0) return [CTag(tag) for tag in ret if not isinstance(tag, Comment)]
def _restrictEventsListByGenre(self, genres): """ Will remove members of :param`self._events_list` which do not satisfy the given genres. :param genres: List of genres. Must only contain genres which are keys of the GENRE_SYNONYMS dict. """ if not isinstance(genres, list): message.logError("List of genres must be a list.", "CEventEngine::_restrictEventsListByGenre") ut.exit(0) ok_genres = [] for genre in genres: if genre not in GENRE_SYNONYMS.keys(): message.logError( "Unknown genre (it's not in the genre synonym dictionary).", "CEventEngine::_restrictEventsListByGenre") ut.exit(0) ok_genres += GENRE_SYNONYMS[genre] new_ents = [] for ent in self._events_list: if GENRE_KEY in ent.keys(): for genre in ent[GENRE_KEY]: if genre.lower() in ok_genres: new_ents.append(ent) break self._events_list = new_ents self._debugInfo()
def start(self): ''' Start the message loop. ''' initial_message = self.get_initial_message() message.logDebug("Starting terminal frontend message loop.", "CTerminalFrontend::start") message.logDebug("Sending message '" + initial_message + "'", "CTerminalFrontend::start") self.sendMessage(initial_message) self._running = True while self._running: ui = input("> ") if not isinstance(ui, str): message.logError("User input must be a string.", "CTerminalFrontend::start") ut.exit(0) message.logDebug("Received message: '" + ui + "'", "CTerminalFrontend::start") if STOP_COMMAND == ui.lower(): self.stop() else: msg = self._agent.process_input(ui) message.logDebug("Sending message: '" + msg + "'", "CTerminalFrontend::start") self.sendMessage(msg)
def embed(sent): """ Will return a vector embedding of the given string sentence. """ if not isinstance(sent, str): message.logError("Given sentence must be a string.", "nlp_util::embed") ut.exit(0) return EMBEDDER(sent)
def sendMessage(self, msg): ''' Send a msg to the user. ''' message.logError( "This function must be overwritten in derived classes.", "CFrontend::sendMessage") ut.exit(0)
def __init__(self, name="Agent"): if not isinstance(name, str): message.logError("Agents name is not a string instance.", "CAgent::__init__") ut.exit(0) self._name = name self._initial_message = [self._make_standard_message(INITIAL_MESSAGE)] self._most_recent_update = self._initial_message self._update = []
def process_input(self, input): """ Will return a response to the given input Will add this response to the self._update param. :param input: The string question. """ message.logError("Function should be overwritten by a derived class.", "CAgent::process_input") ut.exit(0)
def __init__(self, cnn_list: list): super().__init__() if len(cnn_list) == 1: message.logError( "List of CNNs to fuse must have length greater than 1.", "CLateFusionCNN::__init__") ut.exit(0) self.cnn_models = cnn_list for cnn in self.cnn_models: cnn.eval()
def __init__(self, knowledge_name, knowledge_content): ''' :param knowledge_name: The name of this piece of knowledge. :param knowledge_content: The content of this knowledge. ''' if not isinstance(knowledge_name, str): message.logError("Given knowledge name is not a string instance.", "CKnowledgeBase::__init__") ut.exit(0) self._knowledge_name = knowledge_name self._knowledge_content = knowledge_content
def __init__(self, knowledge_base, name="Query Engine"): if not isinstance(knowledge_base, CKnowledgeBase): message.logError( "Given knowledge base is not a CKnowledgeBase instance.", "CQueryEngine::__init__") ut.exit(0) super(CQueryEngine, self).__init__(name) self._initial_message = [self._make_standard_message(INITIAL_MESSAGE)] self._most_recent_update = self._initial_message self._predictor = Predictor.from_path(PREDICTOR_PATH) self._knowledge_base = knowledge_base
def __init__(self, file_path): """ :param file_path: A path to a CSV file. """ if not isinstance(file_path, str): message.logError("Given path to data is not a string instance.", "CKnowledgeBase::__init__") ut.exit(0) # ..todo: make a check that checks there's a CSV at the specified file location. self._data = pd.DataFrame(pd.read_csv(file_path))
def __init__(self, path_to_data): """ This should be given the path to the dataset, it should parse it and store it. :param path_to_data: the file location of the data. """ if not isinstance(path_to_data, str): message.logError("Given path to data is not a string instance.", "CKnowledgeBase::__init__") ut.exit(0) self._knowledge = self._loadData(path_to_data)
def getAttr(self, attr_name): ''' Will return the attribute with attribute name equal to the string :param:`attr_name, or a NoneType instance if not such attributes exist. :param attr_name: A string attribute name to look for. ''' if not isinstance(attr_name, str): message.logError("Given attribute name must be a string instance.", "CTag::getAttr") ut.exit(0) if not self.m_tag.has_attr(attr_name): return None return self.m_tag.attrs[attr_name]
def __init__(self, souptag): ''' Instantiates class. ''' if not isinstance(souptag, Tag): message.logError( "Given souptag is not a bs4.element.Tag instance.", "CTag::__init__") ut.exit(0) self.m_tag = souptag name = souptag.getText() if name != None: self.m_name = name else: self.m_name = "tag_name"
def process_input(self, input): """ Will return an answer to the question given in input. Currently it will always return "I don't want to help you at the moment, ask me another time." :param input: The string question. """ if not isinstance(input, str): message.logError("Given input must be a string instance", "CQueryEngine::getAnswer") ut.exit(0) knowledge_list = self._restrictSearchSpace(input) response = self._getResponse(input, knowledge_list) response = self._make_standard_message(response) self._update.append(response) return [response]
def __init__(self, souptag): """ Instantiates class. """ if not isinstance(souptag, Tag): message.logError( "Given souptag is not a bs4.element.Tag instance.", "CTag::__init__") print( '\n', "argument was of type " + str(type(souptag)) + " " + str(souptag)) ut.exit(0) self.m_tag = souptag name = souptag.getText() if name != None: self.m_name = name else: self.m_name = "tag_name"
def __init__(self, max_events=MAX_TO_DISPLAY, name="Event Engine"): super(CEventEngine, self).__init__(name) if not isinstance(max_events, int): message.logError("Max events must be an int.", "CEventsEngine::__init__") ut.exit(0) self._initial_message = [self._make_standard_message(INITIAL_MESSAGE)] self._most_recent_update = self._initial_message self._state_index = 0 self._state = STATE_PATH[0] self._initial_message.append(STATE_INITIAL_MESSAGES[self._state]) if USE_PREPROCESSED: self._events_list = ut.load(PREPROCESSED_FILE_PATH) else: events_from_ents = ents.getEventsList() self._events_list = ut.merge_events(events_from_ents, recurring_events) self._max_events = max_events self._debugInfo()
def __init__(self, url, home_url, headers=DEFAULT_HEADERS, name="Website"): ''' Instantiates class. ''' if not isinstance(url, str): message.logError("Given URL is not a string instance.", "CWebsite::__init__") ut.exit(0) if not isinstance(home_url, str): message.logError("Given home URL is not a string instance.", "CWebsite::__init__") ut.exit(0) response = requests.get(url, headers=headers) self.m_url = url self.m_home_url = home_url self.m_headers = headers self.m_websoup = BeautifulSoup(response.text, "html.parser") self.m_name = name
def _restrictEventsListByDate(self, dates): """ Will remove members of :param`self._events_list` which do not happen at the specified dates. :param dates: List of dates. """ if not isinstance(dates, list): message.logError("List of dates must be a list.", "CEventEngine::_restrictEventsListByGenre") ut.exit(0) new_ents = [] for ent in self._events_list: start_date = ent[START_DATE] end_date = ent[END_DATE] for date in dates: if ut.dateCheck(date, start_date, end_date): new_ents.append(ent) break self._events_list = new_ents self._debugInfo()
async def sendClientMessage(sid, msg): """ Sends a message to specified client using given socket ID. """ if not isinstance(msg, tuple): message.logError("msg needs to be a tuple of length 2", "server_util::sendClientMessage") ut.exit(0) if len(msg) != 2: message.logError("msg needs to be a tuple of length 2", "server_util::sendClientMessage") ut.exit(0) message_type = msg[1] message_content = msg[0] if message_type == STANDARD_MSG: await SOCKET_SERVER.emit('show_message', message_content, room=sid) elif message_type == CALENDAR_MSG: await SOCKET_SERVER.emit('show_message', message_content, room=sid) await SOCKET_SERVER.emit('show_calendar_message', "NONE", room=sid) elif message_type == GENRE_BTN_MSG: msgs = message_content.split(":") await SOCKET_SERVER.emit('show_message', msgs[0], room=sid) for msg in msgs[1::]: await SOCKET_SERVER.emit('show_clickable_message', msg, room=sid) elif message_type == EVENT_MSG: await SOCKET_SERVER.emit('show_event_message', message_content, room=sid) else: message.logError("Unknown message type.", "server_util::sendClientMessage") ut.exit(0)
def __getitem__(self, index): ind_dict = self.dataset[index] feature_dict = ind_dict[FEAT_DICT] if self.mode == 'LMC': # Log-mel spectrogram, chroma, spectral contrast # and tonnetz are aggregated to form the LMC feature sets feature = self._makeLMC(feature_dict) feature = torch.from_numpy(feature.astype(np.float32)).unsqueeze(0) elif self.mode == 'MC': # MFCC is combined with chroma, # spectral contrast and tonnetz to form the MC feature sets feature = self._makeMC(feature_dict) feature = torch.from_numpy(feature.astype(np.float32)).unsqueeze(0) elif self.mode == 'MLMC': # Combined LM, MFCC and CST together to form MLMC feature. feature = self._makeMLMC(feature_dict) feature = torch.from_numpy(feature.astype(np.float32)).unsqueeze(0) elif self.mode == "TSCNN": # This mode is to be used with a fusion model, we return a list of two features, # the MC and LMC features mc_feature = self._makeMC(feature_dict) mc_feature = torch.from_numpy(mc_feature.astype( np.float32)).unsqueeze(0) lmc_feature = self._makeLMC(feature_dict) lmc_feature = torch.from_numpy(lmc_feature.astype( np.float32)).unsqueeze(0) feature = [mc_feature, lmc_feature] feature = torch.stack(feature) #feature = torch.from_numpy(feature) #feature = torch.from_numpy(feature.astype(np.float32)).unsqueeze(0) else: message.logError("Unknown mode.", "UrbanSound8KDataset::__getitem__") ut.exit(0) label = ind_dict['classID'] fname = ind_dict['filename'] return feature, label, fname
def _getEvents(params): """ Gets the events form the ENTS24 API. Returns events dict. """ params["results_per_page"] = 100 params["full_description"] = 1 resp = None ret = [] while True: if resp is not None: params["page"] = resp.headers._store["x-next-page"][1] resp = r.get(ENTS_PATH, headers={"Authorization": AUTH_DICT["access_token"]}, params=params) if resp.status_code not in [200, 204]: message.logError("Error code in response.", "events::getEventsList") ut.exit(0) lst = resp._content.decode() if lst == '': return ret lst = ut.parseStr(lst) ret += lst
def process_input(self, input): """ Will return an answer to the question given in input. :param input: The string question. """ if not isinstance(input, str): message.logError("Given input must be a string instance", "CQueryEngine::getAnswer") ut.exit(0) if input == "\\reset": self.reset() return self._update if self._state == DATE_STATE: self._process_date_state(input) elif self._state == GENRE_STATE: self._process_genre_state(input) elif self._state == DESIRED_EVENT_INFO: self._process_desired_event_info(input) elif self._state == PRESENT_EVENTS: self._process_present_events(input) return self._update
''' File for testing Allen NLP. Kipp Freud 01/11/2019 ''' # -------------------------------- import allennlp as al from allennlp.predictors.predictor import Predictor import util.utilities as ut from util.message import message import util.events as ents import requests as r from ast import literal_eval as lit import ast import util.nlp_util as nlp # -------------------------------- vector = nlp.embed("I love to do sentence embeddings!") # 300-dim vector ut.exit(1)
def sendMessage(self, msg): '''Send a string msg to the user.''' message.logError( "This function is depreciated for botui frontend class.", "Cbotuifrontend::sendMessage") ut.exit(0)