def get_command(self, text):
        """
        Get command based on the given command text. Append arguments to the original command structure if command need
         arguments.
        :param str text: command text
        :rtype: dictionary (made from json)
        :return: command dictionary (with args ass addition, if command accepts args)
        """
        assert (text is None or isinstance(text, str))
        init = True
        word_list = []
        if text is not None:
            word_list = self._text_processor.preprocess_text(text)
            init = False

        self._determine_command(init, word_list)
        target_command = self._command
        if target_command["has_args"]:
            arg = ' '.join(word_list)
            if target_command["process_input_text"]:
                arg = self._text_processor.filter_out_keywords(word_list,
                                                               self._keywords[self._command["command_id"]][
                                                                   "words"].keys())
            target_command.update({"arg": arg})
        logger.debug("Command for execution = {}".format(target_command))
        return target_command
Exemplo n.º 2
0
def get_search_type(type_str):
    logger.debug("Mapping type_ {}....".format(type_str))
    for type, type_code in tpe_map.items():
        if type in type_str:
            return type_code

    return ''
Exemplo n.º 3
0
def get_social_network_base_url(social_network_str):
    logger.debug("Mapping social network url {}....".format(social_network_str))
    for social_network, url in social_networks_url_map.items():
        if social_network in social_network_str:
            return url

    return None
    def _determine_command(self, init=False, word_list=[]):
        """
        Determines command based on word list. First step is to calculate command scores. Command with the highest score
        is selected as the new command. If multiple command have the same value, ambiguous command is returned as target
        command. Default option: initial command.
        :param init:
        :param word_list:
        :return:
        """
        assert (isinstance(init, bool) and isinstance(word_list, list))
        if init:
            self._command = self.find_command_by_tag("initial")
        elif self._next_command_id is None:
            score_map = self._calculate_command_scores(word_list)
            max_score = max(score_map.values())
            candidate_commands = list(filter(lambda x: x[1] == max_score, score_map.items()))
            if len(candidate_commands) > 1:
                self._command = self.find_command_by_tag("ambiguous")
            else:
                self._command = self._commands[candidate_commands[0][0]]
        else:
            self._command = self._find_command_by_id(self._next_command_id)

        if self._command is None:
            self._command = self.find_command_by_tag("invalid")

        self._next_command_id = self._command["next_command_id"]
        logger.debug("Next command's id = {}".format(self._next_command_id))
Exemplo n.º 5
0
    def return_response(cls, data={}, status=200, message="", headers={},
                        error_code=200):
        try:
            logger.debug('Return status %s %s', status, message)
        except:
            app.logger.error(traceback.format_exc())

        try:
            filters = request.args.get('filters')
            if filters:
                fd = BaseDict(data).filter(*filters.split(','))
                data = fd
        except:
            app.logger.error(traceback.format_exc())
            data = data

        try:
            if isinstance(data, db.Model):
                data = data.format()
            elif isinstance(data, list) and data and \
                    isinstance(data[0], db.Model):
                data = [o.format() for o in data]

        except:
            logger.error(traceback.format_exc())
            data = data

        res = cls(
            data=data,
            status=status,
            message=message,
            error_code=error_code
        ).to_dict()

        return make_response(jsonify(res), status, headers)
 def _populate_service_command_methods(self):
     """
     Populating service commands methods dictionary.
     :rtype: None
     :return: void method
     """
     logger.debug("Populating service command methods....")
     if self._service_command_methods != {}:
         return
     for command in commands:
         service = command["service"]
         method = command["method"]
         arg_name = command["arg_name"]
         if service is not None:
             if service not in self._service_command_methods:
                 method_dict = {
                     method: {} if arg_name is None else {
                         arg_name: None
                     }
                 }
                 self._service_command_methods[service] = method_dict
             else:
                 if arg_name is not None:
                     if method in self._service_command_methods[service]:
                         self._service_command_methods[service][method][
                             arg_name] = None
                     else:
                         self._service_command_methods[service][method] = {
                             arg_name: None
                         }
Exemplo n.º 7
0
def get_risky_humans_geojson(lat: float, lng: float, days_window: int, km_radius: int) -> dict:
	risky_humans_geojson = {
		"type": "FeatureCollection",
		"src": "https://raw.githubusercontent.com/beoutbreakprepared/nCoV2019/master/latest_data/latestdata.csv",
		"features": []
	}

	risky_humans, err = get_risky_humans(
		lat=lat,
		lng=lng,
		days_window=days_window,
		km_radius=km_radius
	)

	if err is not None:
		return risky_humans_geojson

	for risky_human in risky_humans:
		risky_humans_geojson["features"].append({
			'type': 'Feature',
			"properties": {
				"mag": risky_human['mag']
			},
			'geometry': {
				'type': 'Point',
				'coordinates': [
					float(risky_human[CoordinateType.LONGITUDE.value]),
					float(risky_human[CoordinateType.LATITUDE.value])
				]
			}
		})

	logger.debug("returning {} risky humans".format(len(risky_humans)))
	return risky_humans_geojson
Exemplo n.º 8
0
    def _get_forecast_in_readable_form(self, weather_data, param_name,
                                       **kwargs):
        """
        Returns weather forecast in for-read user-friendly form.
        :param weather_data (pyowm.weatherapi25.weather.Weather): Weather object
        :param param_name (str): parameter we want e.g. temperature, humidity,....
        metric units: wind (m/s), temperature (celsius), cloudines(%), pressure(mbar), humidity...
        :param kwargs:
        :return: weather data in string format weather_param:value
        """
        forecast = self._get_forecast(weather_data, param_name, **kwargs)
        logger.debug("Complete forecast info: {}".format(forecast))
        display_str = ''
        for param, struct in WEATHER_PARAMETERS.items():
            if param in ("temperature_min", "temperature_max"):
                param = "temperature"

            if param not in forecast:
                continue
            # NOTE:pressure value is measured in hPa, which is equal to mbar
            weather_param_value = forecast[param]
            if param == "reference_time":
                weather_param_value = datetime.utcfromtimestamp(
                    int(weather_param_value)).strftime('%d-%m-%Y %H:%M:%S')
            child_alias_status = struct[1]
            child_alias_value = struct[0]
            display_name = struct[2][self._api.get_language()]
            value = weather_param_value[
                child_alias_value] if child_alias_status == "child" else weather_param_value
            display_str += display_name + ": " + str(value) + ",\n"
        return display_str
Exemplo n.º 9
0
def send_email(sender: str, recipient: str, subject: str,
               body_text: str) -> bool:
    charset = "UTF-8"
    client = boto3.client('ses', region_name=CONFIG.get('aws-region'))
    try:
        response = client.send_email(Destination={
            'ToAddresses': [recipient],
        },
                                     Message={
                                         'Body': {
                                             'Text': {
                                                 'Charset': charset,
                                                 'Data': body_text
                                             },
                                         },
                                         'Subject': {
                                             'Charset': charset,
                                             'Data': subject
                                         },
                                     },
                                     Source=sender)
    except ClientError as e:
        logger.error(e.response['Error']['Message'])
        return False
    else:
        logger.debug("Email sent! Message ID : {}".format(
            response['MessageId']))
        return True
def clean_data(data: pd.DataFrame) -> pd.DataFrame:
    logger.debug("Cleaning dataset...")
    data: pd.DataFrame = data[~pd.isna(data['ID'])]
    data: pd.DataFrame = data[~pd.isna(data['date_confirmation'])]
    data['date_confirmation'] = pd.to_datetime(
        data['date_confirmation'].apply(lambda x: x[:10]))
    data['created'] = data['date_confirmation']
    data['modified'] = data['date_confirmation']
    data['latitude'] = pd.to_numeric(data['latitude'])
    data['longitude'] = pd.to_numeric(data['longitude'])
    data['biological_sex'] = data['sex'].apply(
        lambda x: 'F' if x == 'female' else ('M' if x == 'male' else np.nan))
    data['symptom'] = 'verified_covid19'
    data['source'] = 'beoutbreakprepared'
    data['source_id'] = data['ID']
    data['id'] = data['source_id'].apply(lambda x: str(uuid.uuid4()))
    data['human_id'] = data['id']
    try:
        source_ids_to_remove = get_existing_source_ids()
    except Exception as e:
        logger.error(e)
    else:
        data: pd.DataFrame = data[~data['source_id'].isin(source_ids_to_remove
                                                          )]
    return data
Exemplo n.º 11
0
 def set_language(self, language):
     """
     Sets _language of wikipedia client API
     :param language: _language code (str)
     :return:
     """
     logger.debug("Setting wikipedia _language: {}".format(language))
     wikipedia.set_lang(language)
Exemplo n.º 12
0
 def set_language(self, language):
     """
     Sets _language for OWM API.
     :param language : _language code (str) e.g. en, fr, sr...
     :return:
     """
     logger.debug("Setting owm _language: {}".format(language))
     language = 'hr' if language == 'sr' else language
     self._api.set_language(language)
Exemplo n.º 13
0
 def _get_movies_by_title(self, title):
     """
     Returns list of movies (imdb.Movie.Movie) that suits [title] argument
     :param title: Movie title (str)
     :return:
     """
     assert (isinstance(title, str))
     logger.debug("Searching for movies with title = {}.".format(title))
     return self._imdb.search_movie(title)
Exemplo n.º 14
0
 def _get_first_movie(self, title):
     """
     Returns first fetched movie (if there is any).
     :param title: Movie title (str)
     :return: imdb.Movie.Movie object
     """
     assert (isinstance(title, str))
     movies = self._get_movies_by_title(title)
     logger.debug("Movies fetching is over.")
     return movies[0] if len(movies) > 0 else None
Exemplo n.º 15
0
 def control(self, state):
     """
     Control arduino writing through serial port. Output state is written as str.
     :param state: value that determines output state - one of the following values (`switch`, `power off`,
     `power on`) (str)
     :return: void method
     """
     logger.debug("Calling arduino control method with params: [state = {}]".format(state))
     self._set_state(state)
     self._controller.write(str(self._state).encode())
Exemplo n.º 16
0
def get_relay_state(arduino_control_str):
    keywords = {"switch": ("switch", "promeni", "stanje"), "power on": ("power on", "turn on", "switch on",
                                                                        "uključi", "upali", "aktiviraj"),
                "power off": ("power off", "turn off", "switch off", "isključi", "ugasi",
                              "deaktiviraj")}
    logger.debug("Converting input command to control demand....")
    for target_word, assoc_words in keywords.items():
        if any(word in assoc_words for word in arduino_control_str):
            return target_word
    #NOTE:default case is switch (scenarios when speech is not recognized correctly)
    return "switch"
 def set_language(self, language):
     """
     Sets _language of resolver and load keywords based on _language.
     :param str language: _language code
     :rtype: None
     :return: void method
     """
     assert (isinstance(language, str))
     logger.debug("Command resolver _language = {}.".format(language))
     self._language = language
     keywords = load_json_data(LANG_KEYWORDS[language])
     self._set_keywords(keywords)
def get_existing_source_ids() -> List[str]:
    logger.debug("Getting existing source ids")
    database_connection: Engine = sqlalchemy.create_engine(generate_db_uri())
    with database_connection.begin() as cnx:
        sql_query = sqlalchemy.text("""
			SELECT `source_id`
			FROM `humans`
			WHERE `source` = 'beoutbreakprepared'
		""")
        result = cnx.execute(sql_query)
        result_as_list: list = result.fetchall()
        source_ids_to_remove: str = [item[0] for item in result_as_list]
    return source_ids_to_remove
Exemplo n.º 19
0
 def _get_movie_info(self, movie, info_props):
     """
     Returns movie info - parameters that are used in info are described in info_props tuple.
     :param movie: dictionary with movie details
     :param info_props: tuple with parameters for info
     :return: dictionary that contains parameters from info_props as keys
     """
     assert (isinstance(movie, dict))
     assert (isinstance(info_props, tuple))
     movie_info = {}
     logger.debug("Creating movie info....")
     for property in info_props:
         movie_info[property] = movie[property]
     return movie_info
 def _browser_open(self, url, new=2, autoraise=False):
     """
     Opens the given url in the browser.
     :param str url: URL to open
     :param int new: opening flag: 0 - the url is opened in the same browser window if possible, 1 - new window,
     2 - new tab (default)
     :param autoraise:If autoraise is True, the window is raised if possible (note that under many window managers
     this will occur regardless of the setting of this variable).
     :rtype None
     :return: void method
     """
     assert (isinstance(url, str) and isinstance(new, int) and isinstance(autoraise, bool))
     logger.debug("Opening url = {}.".format(url))
     webbrowser.open(url, new=new, autoraise=autoraise)
Exemplo n.º 21
0
 def filter_out_keywords(self, word_list, keyword_list):
     """
     Filter out keywords from [word_list]. Keywords are stored in [keyword_list].
     :param list word_list: list of words that needs to be filtered
     :param dict_keys keyword_list: list of words that we want to filter out from [word_list]
     :rtype: str
     :return: string composed from words from [word_list] that do not belong to [keyword_list]
     """
     assert (isinstance(word_list, list))
     text_without_keywords = ' '.join(
         [word for word in word_list if word not in keyword_list])
     logger.debug(
         "Text without keywords = {}".format(text_without_keywords))
     return text_without_keywords
 def open_social_network_page(self, nickname=None, social_network_url=FACEBOOK_BASE_URL):
     """
     Opens social network page
     :param str nickname: user nickname
     :param str social_network_url: base url of wanted social network (FB, IG, TW, IN)
     :rtype ActionResult
     :return: Empty ActionResult with SUCCESS status
     """
     assert (isinstance(nickname, str) and isinstance(social_network_url, str))
     logger.debug("Calling open_social_network_page with params: [nickname = {}, social_network_url = {}].".
                  format(nickname, social_network_url))
     url = social_network_url + nickname + "/"
     self._browser_open(url)
     return ActionResult("", SUCCESS)
Exemplo n.º 23
0
 def _get_appropriate_lang_code(self, language):
     """
     Determines and returns the corresponding lang_code.
     :param str language: _language or _language code
     :rtype str
     :return:lang_code of the given _language
     """
     assert(isinstance(language, str))
     logger.debug("Getting appropriate _language code...")
     if self._language == 'sr':
         language = convert_latin_to_cyrillic(language)
         language = self._langs_in_serbian.get(language, "en")
     if language not in googletrans.LANGUAGES:
         language = self._convert_language_to_lang_code(language)
     return language
Exemplo n.º 24
0
 def brief_search(self, query, sentences=3):
     """
     Get summary section of page that satisfy query.
     :param query: searching term  (str)
     :param sentences: number of sentences that wikipedia summary should have (int)
     :return: ActionResult with the result from wikipedia page (or appropriate error message)
     """
     logger.debug(
         "Calling wikipedia brief_search with [query = {}]".format(query))
     if sentences > 10:
         raise ValueError(
             "Number of summary sentences can not be greater than 10.")
     summary = wikipedia.summary(query, sentences=sentences)
     logger.debug("Wikipedia summary = {}".format(summary))
     return ActionResult(summary, SUCCESS)
Exemplo n.º 25
0
 def _speak_out(self, message_prefix, output_message):
     """
     Runs speaker's speaking method. Speak out [message_prefix] at first, ad then the [output_message]. Use speaking
     language to handle cases when the translation service is used. Resets the speaking language at the end.
     :param str message_prefix: prefix message (success or fail message)
     :param str output_message: command's output or exception message
     :rtype: None
     :return: void method
     """
     logger.debug("Message prefix = {}".format(message_prefix))
     self._speaker.save_speech_and_play(message_prefix)
     self._set_speaking_language(self._speaking_language)
     logger.debug("Output message = {}".format(output_message))
     self._speaker.save_speech_and_play(output_message)
     self._reset_speaking_language_()
 def _calculate_command_scores(self, word_list):
     """
     Calculates score for every available command.
     :param list word_list: list of words
     :rtype: dictionary (str:float)
     :return: Returns scores dictionary with the following structure command_id
         : score
     """
     assert (isinstance(word_list, list))
     logger.debug("Calculating commands scores...")
     scores = {}
     for command_id, command in self._keywords.items():
         words = command["words"]
         scores[command_id] = sum([words.get(word, 0) * word_list.count(word) \
                                   for word in words if word in word_list])
     return scores
Exemplo n.º 27
0
 def _set_state(self, state):
     """
     Sets output state based on state value.
     :param state: value that determines output state - one of the following values (`switch`, `power off`,
     `power on`) (str)
     :return: void method
     """
     if state == "switch":
         self._state_switch()
     elif state == "power off":
         self._turn_off()
     elif state == "power on":
         self._turn_on()
     else:
         raise ValueError("Invalid state.")
     logger.debug("Current relay state = {}".format(self.get_state()))
Exemplo n.º 28
0
 def set_language(self, language):
     """
     Sets the _language and target words based on _language.
     :param str language: _language code
     :rtype: None
     :return: void method (raise ValueError if _language is not supported)
     """
     assert (isinstance(language, str))
     try:
         logger.debug("Text processor _language = {}.".format(language))
         self._target_words = flatten_lists(
             word_dictionary_mapping[language].values())
         self._language = language
     except KeyError:
         raise ValueError(
             "Language is not supported. Use English or Serbian.")
Exemplo n.º 29
0
    def update_if_change(self, **kwargs):
        if not kwargs:
            return False
        is_update = False
        fields = []
        for k, v in kwargs.items():
            ov = getattr(self, k)
            if ov != v:
                setattr(self, k, v)
                fields.append('{}: {}'.format(k, v))
                is_update = True

        if is_update:
            logger.debug('{} need update {}'.format(repr(self),
                                                    fields))
            self.update_self()
            self.refresh_cache()
        return is_update
 def open_found_url_in_browser(self, query, tpe=""):
     """
     Opens the found url in browser.
     :param str query: google search query string. Must not be URL-encoded
     :param str tpe: type_ of search, default=`all`
     :rtype ActionResult
     :return: Empty ActionResult with SUCCESS status
     """
     assert (isinstance(query, str) and isinstance(tpe, str))
     logger.debug("Calling open_found_url_in_browser with params: [query = {}, tpe = {}].".format(query, tpe))
     url = self._get_first_search_result(query, tpe=tpe)
     #url = google_result.get_result()
     if url is not None:
         logger.debug("Found url = {}.".format(url))
         self._browser_open(url=url)
         return ActionResult("", SUCCESS)
     else:
         raise GoogleSearchException