def verifyModule(self): cfg = self._tp.cfg # Get the actual configurationx #TODO: startup_test - This test will run with simulated command line arguments and configurationx files cfg[_c.noargs] = True _C.insert(cfg.cmdargs) # Now test the results from running the high-level code module-level # code. This actually ran in the setup function since the template was # imported there. self.assertTrue(isinstance(cfg, dict), f'The configurationx is not a dictionary - {type(cfg)}') l = _C(cfg).len() # Number of entries in the configurationx num = 8 # The expected number of entries in the configurationx self.assertEqual(l, num, f'The configurationx dictionary should have {num} item(s), has {l} ' '\n The entries are: {cfg}') #TODO: Cleanup and use the platform module to use this code """ plid = cfg.plid # The determined operating system plsys = 'linux' # The expected operating system self.assertEqual(plid, plsys, 'The configurationx dictionary should say running on {} - is {}'.\ format(plid, plsys)) """ self.assertIsNone(cfg[_c.uac], f'We should not have access to the user startup module yet - have {cfg[_c.uac]}')
def process_before_iteration(self): """ ============================================================= Effectuer des operations avant de parcourir les marchés - Verification si on est dans une Session de Trading - Récupérez les positions ouvertes sur les marchés - Recuperer les evenements economique ============================================================= """ # Recuperation de la configuration pour iteration self.used_stgy = Configuration.from_filepath().get_active_strategy() self.params = Configuration.from_filepath().get_param_strategy( self.used_stgy) # Initialiser le Broker à partir de la classe Factory # La Factory permet de créer le Broker à partir du fichier de configuration # self.broker = BrokerFactory(self.config).make() # Initialiser la Stratégie à partir de la classe Factory # La Factory permet de créer la Strategie à partir du fichier de configuration LOGGER.info("Initialisation de la Strategie : {}".format( self.used_stgy)) self.strategy = StgyFactory(self.used_stgy).make() # Reinitialisation des positions + events self.positions = None self.orders = None self.events = None
def can_apply(self, configuration: Configuration, transition: str) -> bool: """ Determine whether the given transition is legal for this configuration. """ if transition.startswith("L") or transition.startswith("R"): label = transition[2:-1] if transition.startswith("L"): h = configuration.get_stack(0) else: h = configuration.get_stack(1) if h < 0: return False if h == 0 and label != self.root_label: return False n_stack = configuration.get_stack_size() n_buffer = configuration.get_buffer_size() if transition.startswith("L"): return n_stack > 2 elif transition.startswith("R"): if self.single_root: return (n_stack > 2) or (n_stack == 2 and n_buffer == 0) else: return n_stack >= 2 return n_buffer > 0
def __init__(self): """ Initialisation de l'objet """ # Enregistrement des variables utiles self._type = Configuration.from_filepath().get_type_account() self._accId = Configuration.from_filepath().get_credentials()[self._type.lower()]['account_id'] self._mdp = Configuration.from_filepath().get_credentials()[self._type.lower()]['password'] # Initialisation des variables necessaires self._broker = None self._mdl = None self._orders = None pass
def get_oracle(self, configuration: Configuration, tree: DependencyTree) -> str: """ Provide a static-oracle recommendation for the next parsing step to take """ word1 = configuration.get_stack(1) word2 = configuration.get_stack(0) if word1 > 0 and tree.get_head(word1) == word2: return "L(" + tree.get_label(word1) + ")" elif word1 >= 0 and tree.get_head(word2) == word1 and not configuration.has_other_child(word2, tree): return "R(" + tree.get_label(word2) + ")" return "S"
def test_works(self): output_file = tempfile.mkdtemp() config = Configuration(output_file) config.build_configs() config.next() config['random_seed'] = '234524' self.assertTrue(config['random_seed'] == '234524') content = config.generate_experiement_config() self.assertTrue('234524' in content) while config.next(): content = config.generate_experiement_config() shutil.rmtree(output_file)
def __init__(self, account='DEMO'): """ Initialisation de l'objet """ # Enregistrement des variables utiles self._type = 'DEMO' if Configuration.from_filepath( ).get_use_demo_account() is True else 'REEL' self._accId = Configuration.from_filepath().get_credentials()[ self._type.lower()]['account_id'] self._mdp = Configuration.from_filepath().get_credentials()[ self._type.lower()]['password'] # Initialisation des variables necessaires self._broker = None self._mdl = None self._markets = None pass
def play(args): """Handles the 'play' command.""" from threading import Timer from lib.configuration import Configuration from lib.task import Task config = utils.parse_config(args, Configuration()) tasks = [] for task, items in config.tasks.items(): t = Timer(items['timing'], Task.run_task, args=(task, len(tasks) + 1, items, config)) t.daemon = True t.start() tasks.append(t) duration = config.duration if duration == 0: for t in tasks: t.join() else: start = time.time() while time.time() < start + duration: finished = True for t in tasks: if not t.finished.is_set(): finished = False break if finished: break time.sleep(1)
def updateParam(): if request.method == 'POST': # Recuperation + traitement des données du formulaire data = Utils.parseForm(dict(request.form)) # Initialisation de la Config config = Configuration.from_filepath() # Modification de la config if 'time_zone' in data: config.set_time_zone(data['time_zone']) if 'hour_start' in data: config.set_hour_start(data['hour_start']) if 'hour_end' in data: config.set_hour_end(data['hour_end']) if 'filepathCredentials' in data: config.set_filepathCredentials(data['filepathCredentials']) if 'use_demo_account' in data: config.set_use_demo_account(True if data['use_demo_account'] == 'true' else False) if 'spin_interval' in data: config.set_spin_interval(data['spin_interval']) # Envoi du message return Render.jsonTemplate(_OPERATION, 'Enregistrement de la Configuration', categorie="SUCCESS") else: abort(400)
def to_csv(self, index=False): ''' Get the JSON representation of this object :return: JSON string ''' data_frame = self.data_frame headers = [str(sub_col) for _, sub_col in data_frame.columns] headers.insert(0, 'experiment_name') headers.append('_empty_') headers.extend(Configuration.get_parameters()) datas = [] for index, row in data_frame.iterrows(): data = [] data.append(str(row.name)) for col_name, sub_col_name in data_frame.columns: data_value = float(row[col_name][sub_col_name]) # check for NaN if math.isnan(data_value): data_value = 0.0 data.append(str(data_value)) data.extend(str(row.name).split('-')) datas.append(data) csv_string = ','.join(headers) + '\n' for row in datas: csv_string += ','.join(row) + '\n' return csv_string
def __init__(self, name="NOM"): """ Initialisation Objet """ # Recuperation de la Configuration self.config = Configuration.from_filepath() # Definition des Parametres self.name = name self.__prices = None
def run_continously(config: Configuration = None): if config == None: config = Configuration.from_environment() while (True): try: #print('Processing: '+str(config)) Producer(config).invoke() except Exception as error: print(error)
def __init__(self): """ ==================================================================== Initialisation Objet ==================================================================== """ # Constructeur du Model self._name = "DEV" super().__init__(name=self._name) # BBANDS self._BB_PERIOD = Configuration.from_filepath().get_param_strategy( self._name)["bb_period"] self._BB_DEVIATION = Configuration.from_filepath().get_param_strategy( self._name)["bb_deviation"] # RSI self._RSI_PERIOD = Configuration.from_filepath().get_param_strategy( self._name)["rsi_period"] self._RSI_LIMIT_OVER_BUY = Configuration.from_filepath( ).get_param_strategy(self._name)["rsi_limit_over_buy"] self._RSI_LIMIT_OVER_SELL = Configuration.from_filepath( ).get_param_strategy(self._name)["rsi_limit_over_sell"]
def getParam(): """Parse un fichier de log dans un DataFrame""" if request.method == 'POST': # Recuperation des infos data = Configuration.from_filepath().json() # Cryptage des id # df["id"] = df.apply(lambda x: Crypt.encode(cfg._APP_SECRET_KEY, x['id']), axis=1) # Retour du message return Render.jsonTemplate(_OPERATION, 'Parametres', categorie="SUCCESS", data=data) else: abort(400)
def is_in_session_trading(self): """ ============================================================= Verification que nous sommes dans la periode de Trading defini en Configuraton Renvoie Vrai si on y est, Faux dans le cas contraire ============================================================= """ # [DEBUG] - Activation pour dev du weekend # return True # Verification jour ouvrables if datetime.datetime.today().weekday() > 4: return False # Recuperation des parametres de Session de Trading hour_start = Configuration.from_filepath().get_hour_start() hour_end = Configuration.from_filepath().get_hour_end() # Initialisation du moment tz = pytz.timezone(Configuration.from_filepath().get_time_zone()) now_time = datetime.datetime.now(tz=tz) nowStr = str(now_time.strftime('%H:%M')) # Periode de trading defini en configuration if hour_end < hour_start: return nowStr >= hour_start or nowStr <= hour_end return hour_start <= nowStr <= hour_end
def initial_configuration(self, sentence) -> Configuration: configuration = Configuration(sentence) length = len(sentence) # For each token, add dummy elements to the configuration's tree # and add the words onto the buffer for i in range(1, length + 1): configuration.tree.add(constants.NONEXIST, constants.UNKNOWN) configuration.buffer.append(i) # Put the ROOT node on the stack configuration.stack.append(0) return configuration
def getInfo(): """Retourne les informations necessaires au frontend""" info = {} # Utilisateur info["user"] = { 'is_logged_in': Session.getValue("is_logged_in"), 'displayName': Session.getValue("displayName"), 'role': Session.getValue("role"), 'username': Session.getValue("username") } # Application info["app"] = { 'version': cfg._APP_VERSION, 'name': cfg._APP_NAME, 'auteur': cfg._APP_AUTEUR, 'environ': cfg._ENVIRONNEMENT } # Javascript info["js"] = { 'timeout': str(cfg._JS_TIMEOUT_LOADER), 'csrfToken': Session.generateCsrfToken(), 'root': cfg._ENV[cfg._ENVIRONNEMENT]["JS_ROOT_DEV"], 'timeZone': Configuration.from_filepath().get_time_zone() } # System info["system"] = { 'main': SysInfo().getSystem(), 'python': SysInfo().getPython(), } # Compte info["compte"] = { # Courante 'WorkOnCompte': Configuration.from_filepath().get_type_account() } return info
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start operand1 = configuration.get_stack(0) operand2 = configuration.get_stack(1) label = transition[2:-1] # left reduce operation. if transition.startswith("L"): configuration.add_arc(operand1, operand2, label) check = configuration.remove_second_top_stack() if not check: print("Only one element in the stack!") # right reduce operation. elif transition.startswith("R"): configuration.add_arc(operand2, operand1, label) check = configuration.remove_top_stack() if not check: print("Stack is empty!") # shift operation. elif transition == "S": configuration.shift() else: pass return configuration
def process(self, data_object): ''' Process a given file :param data_object: JSON object :return: Updated data_object reference ''' super(ExperimentConfig, self).process(data_object) self.config = data_object row = [] for param in Configuration.get_parameters(): if param == 'repeat': continue if param in data_object: self.add_column(param) row.append(data_object[param]) self.add_row(row) return data_object
def process(self, data_object): ''' Process a given file :param data_object: MetricManager instance :return: Updated data_object reference ''' self.add_column('experiment_name') # ensure we have all the data data_object.analyze() # check columns first for group_key, _, metric_obj in metric_iter( data_object.metrics['summations']): for metric in metric_obj: metric_name = metric['full_name'] exists = metric_get(group_key, metric_name, self.metric_map) # adding new column if exists is None: self.add_column(metric_name) position = len(self.data_frame.columns) + \ len(self._columns_to_add) - 1 metric_add(position, self.metric_map, group_key, metric_name) # populate the values for the table row row = [ None for _ in range( 0, len(self.data_frame.columns) + len(self._columns_to_add)) ] row[0] = Configuration.get_hash_name( data_object.get_config(), [self.experiment_variable, 'repeat']) for group_key, _, metric_obj in metric_iter( data_object.metrics['summations']): for metric in metric_obj: metric_name = metric['full_name'] position = metric_get(group_key, metric_name, self.metric_map) if position is None: raise Exception('Something very bad happened') row[position] = metric['value'] self.add_row(row) return data_object
def run_multi_threaded(topology: CameraNetTopology): """ Create one thread per camera in the topology. """ threads = [] for home_base in topology.home_bases: for camera_name in home_base.cameras: print('Starting {}/{}\n'.format(home_base.name, camera_name)) config = Configuration(server_uri="{}/{}".format( home_base.rtsp_address, camera_name), base_name=home_base.name, camera_name=camera_name, bucket_name=get_value('BUCKET')) thread = threading.Thread(target=run_continously, args=(config, )) threads.append(thread) thread.start() for t in threads: t.join()
def __init__(self, prices): """ ==================================================================== Initialisation Objet ==================================================================== """ # Recuperation de la Configuration self.config = Configuration.from_filepath() # Copie des données self.data = prices.copy(deep=True) # Variables du Graphiques self.tools = "xpan,xwheel_zoom,ywheel_zoom, box_zoom,reset,save" self._plots = [] # Parametrage Generaux self.format_date = '%a %d %b %Y\n%H:%M' self.INCREASING_COLOR = '#009900' self.DECREASING_COLOR = 'red' self.BUY_SIG_COLOR = '#33FF33' self.SELL_SIG_COLOR = '#FF3333' self.DATE_FMT = '%d %b %Y' self.DATETIME_FMT = '%a %d %b %Y, %H:%M:%S'
def compare_experiments(self, experiment_metric_file_paths): ''' Run analysis the compares the output from each experiment :param experiment_metric_file_paths: List of the metric files for each of the experiments :return: dict of the metric objects ''' # pivot data and generate graph for each metric for variable in Configuration.get_parameters(): if variable == 'repeat': continue # get metrics for variable cmp_exps = MetricManagerComparer(variable) class_reader = ClassReader([cmp_exps], MetricManager) finder = FileFinder([class_reader]) finder.process_file_list(self.base_directory, experiment_metric_file_paths) for group_name, metric_name, _ in metric_iter(cmp_exps.metric_map): if group_name == 'variables': continue metric_cmp = SummationVariableComparer(cmp_exps, metric_name) metric_cmp.process(None) self._set_data(metric_cmp.to_string(True), variable, group_name, metric_name) self._set_graph(metric_cmp.create_graph(), variable, group_name, metric_name) # get one table of all the values cmp_exps = MetricManagerComparer('') class_reader = ClassReader([cmp_exps], MetricManager) finder = FileFinder([class_reader]) finder.process_file_list(self.base_directory, experiment_metric_file_paths) self._set_data(cmp_exps.to_string(), 'all')
class DataManager: cfg = Configuration() qry = "" @staticmethod def getDictionaryTableFromExcell(strQuery): dataTable = dict() colNmaes = dict() rowID = 0 colID = 0 try: DataManager.qry = strQuery cStr = DataManager.cfg.connectionStrings["ExcelConn"].replace( "[ControlFileName]", os.getcwd() + DataManager.cfg.appSettings["ControlFileName"]) cnxn = pyodbc.connect(cStr, autocommit=True) cursor = cnxn.cursor() cursor.execute(DataManager.qry) # create a dictionary of column names for desc in cursor.description: colNmaes[colID] = desc[0] colID += 1 # Loop through returned data rows for row in cursor: colID = 0 dataRow = dict() # create a dictionary using column name and value for column in row: dataRow[colNmaes[colID]] = str(column) colID += 1 # Add the dictionary to the table dictionary dataTable[rowID] = dataRow rowID += 1 except: print("Failed to execute query ({0}) on database for Error: {1}". format(strQuery, sys.exc_info()[1])) cnxn.rollback() finally: cursor.close() cnxn.close() return dataTable @staticmethod def getDictionaryFrom2ColumnsInExcellSheet(strQuery): dataTable = dict() try: DataManager.qry = strQuery cStr = DataManager.cfg.connectionStrings["ExcelConn"].replace( "[ControlFileName]", os.getcwd() + DataManager.cfg.appSettings["ControlFileName"]) cnxn = pyodbc.connect(cStr, autocommit=True) cursor = cnxn.cursor() cursor.execute(DataManager.qry) # Loop through returned data rows for row in cursor: dataTable[row[0]] = row[1] except: print("Failed to execute query ({0}) on database for Error: {1}". format(strQuery, sys.exc_info()[1])) cnxn.rollback() finally: cursor.close() cnxn.close() return dataTable @staticmethod def executeQueryOnExcell(strQuery): try: DataManager.qry = strQuery cStr = DataManager.cfg.connectionStrings["ExcelConn"].replace( "[ControlFileName]", os.getcwd() + DataManager.cfg.appSettings["ControlFileName"]) cnxn = pyodbc.connect(cStr, autocommit=True) cursor = cnxn.cursor() cursor.execute(DataManager.qry) print("query (%s) executed successfully.!" % (strQuery)) except: print("Failed to execute query ({0}) on database for Error: {1}". format(strQuery, sys.exc_info()[1])) cnxn.rollback() finally: cursor.close() cnxn.close() print("connection is closed") @staticmethod def getDictionaryTableFromSource(strConn, strQuery): dataTable = dict() colNmaes = dict() rowID = 0 colID = 0 try: DataManager.qry = strQuery cStr = DataManager.cfg.connectionStrings[strConn] cnxn = pyodbc.connect(cStr, autocommit=True) cursor = cnxn.cursor() cursor.execute(DataManager.qry) # create a dictionary of column names for desc in cursor.description: colNmaes[colID] = desc[0] colID += 1 # Loop through returned data rows for row in cursor: colID = 0 dataRow = dict() # create a dictionary using column name and value for column in row: dataRow[colNmaes[colID]] = str(column) colID += 1 # Add the dictionary to the table dictionary dataTable[rowID] = dataRow rowID += 1 except: print("Failed to execute query ({0}) on database for Error: {1}". format(strQuery, sys.exc_info()[1])) cnxn.rollback() finally: cursor.close() cnxn.close() return dataTable
#!/usr/bin/env python # encoding: utf-8 from lib.configuration import Configuration from lib.strategie.factory import StgyFactory ###################################################################################################### # DEV ###################################################################################################### if __name__ == '__main__': # Recuperation de la Configuration config = Configuration.from_filepath() # Recuperation de la Configuration # print(config.get_active_strategy()) # print(config.get_param_strategy(name=config.get_active_strategy())) # print(config.get_values_strategy()) # Creation de la Strategie stgyObj = StgyFactory("DEV").make() print(stgyObj.name) # Recuperation des prix stgyObj.run(symbol='EURUSD', ut='H1') # Affichage des prix print(stgyObj.prices)
def wait_for(self): """ ============================================================= Attendez la durée spécifiée en fonctions des parametres de Sessions de Trading : - Si on DANS : spin_interval - Si on HORS : Determination du temps d'attente restant avant le debut de la prochaine Sessions ============================================================= """ # Recuperation en config des valeurs utiles spin_interval = Configuration.from_filepath().get_spin_interval( ).lower() # Determination du Nombre de Secondes pour le spin_interval seconds_per_unit = { "s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800 } spin_interval_secs = int( spin_interval[:-1]) * seconds_per_unit[spin_interval[-1]] if self.is_in_session_trading() is True: secs = spin_interval_secs else: from_time = datetime.datetime.now() # Determination de la date de prochaine ouverture today_opening = datetime.datetime.strptime( from_time.strftime('%Y-%m-%d') + " " + Configuration.from_filepath().get_hour_start(), '%Y-%m-%d %H:%M') # Si on a depassé Minuit if from_time < today_opening and from_time.weekday() < 5: nextMarketOpening = today_opening else: # Si on est lundi, on ajoute 1 jour if from_time.weekday() == 0: nextMarketOpening = today_opening + datetime.timedelta( days=1) # -- Si on est mardi, on ajoute 1 jour if from_time.weekday() == 1: nextMarketOpening = today_opening + datetime.timedelta( days=1) # -- Si on est mercredi, on ajoute 1 jour if from_time.weekday() == 2: nextMarketOpening = today_opening + datetime.timedelta( days=1) # -- Si on est jeudi, on ajoute 1 jour if from_time.weekday() == 3: nextMarketOpening = today_opening + datetime.timedelta( days=1) # -- Si on est vendredi, on ajoute 3 jours if from_time.weekday() == 4: nextMarketOpening = today_opening + datetime.timedelta( days=3) # -- Si on est samedi, on ajoute 2 jours if from_time.weekday() == 5: nextMarketOpening = today_opening + datetime.timedelta( days=2) # -- Si on est dimanche, on ajoute 1 jour if from_time.weekday() == 6: nextMarketOpening = today_opening + datetime.timedelta( days=1) # Affichage en LOG LOGGER.info("Prochaine Session de Trading : " + nextMarketOpening.strftime('%a %d %b %Y %H:%M:%S')) LOGGER.debug("Date à comparer : " + from_time.strftime('%a %d %b %Y %H:%M:%S')) # Calcul du nombre de secondes à attendre secs = (nextMarketOpening - from_time).total_seconds() # Formatage du Nombre de Secondes mins, seconds = divmod(secs, 60) hours, mins = divmod(mins, 60) days, hours = divmod(hours, 24) # Formatage de la chaine de caractere fmt_str = "" if days > 0: fmt_str += "{} jrs ".format(int(days)) if hours > 0: fmt_str += "{} hrs ".format(int(hours)) if mins > 0: fmt_str += "{} mins ".format(int(mins)) if seconds > 0: fmt_str += "{} secs ".format(int(seconds)) # Renvoi du nombre de Secondes LOGGER.info("Patientez {} ...".format(fmt_str.strip())) return secs
def get_configuration_features(configuration: Configuration, vocabulary: Vocabulary) -> List[List[int]]: """ ================================================================= Implement feature extraction described in "A Fast and Accurate Dependency Parser using Neural Networks"(2014) ================================================================= """ # TODO(Students) Start stack_size = configuration.get_stack_size() buffer_size = configuration.get_buffer_size() #print("Stack:"+str(stack_size)) #print("Buffer"+str(buffer_size)) stack_words = [] buffer_words = [] fst_2nd_leftmost_rightmost = [] fst_2nd_leftmost_leftmost_rightmost_rightmost = [] for i in range(0,3) : stack_words.append(configuration.get_stack(i)) buffer_words.append(configuration.get_buffer(i)) if i < 2 : fst_2nd_leftmost_rightmost.append(configuration.get_left_child(stack_words[i],1)) fst_2nd_leftmost_rightmost.append(configuration.get_right_child(stack_words[i], 1)) fst_2nd_leftmost_rightmost.append(configuration.get_left_child(stack_words[i], 2)) fst_2nd_leftmost_rightmost.append(configuration.get_right_child(stack_words[i], 2)) fst_2nd_leftmost_rightmost.append( configuration.get_left_child(configuration.get_left_child(stack_words[i],1),1) ) fst_2nd_leftmost_rightmost.append( configuration.get_right_child(configuration.get_right_child(stack_words[i], 1),1) ) #fst_2nd_leftmost_leftmost_rightmost_rightmost.append( #configuration.get_left_child(configuration.get_left_child(stack_words[i],1),1) ) #fst_2nd_leftmost_leftmost_rightmost_rightmost.append( #configuration.get_right_child(configuration.get_right_child(stack_words[i], 1),1) ) final = stack_words + buffer_words + fst_2nd_leftmost_rightmost #+ fst_2nd_leftmost_leftmost_rightmost_rightmost pos_tags = [] for j in range(0,len(final)) : pos_tags.append(vocabulary.get_pos_id(configuration.get_pos(final[j]))) temp_final = final[6:18] arc_labels = [] for j in range(0,len(temp_final)) : arc_labels.append(vocabulary.get_label_id(configuration.get_label(temp_final[j]))) for i,x in enumerate(final) : final[i] = vocabulary.get_word_id(configuration.get_word(final[i])) features = final + pos_tags + arc_labels # TODO(Students) End assert len(features) == 48 return features
def apply(self, configuration: Configuration, transition: str) -> Configuration: """ ================================================================= Implement arc standard algorithm based on Incrementality in Deterministic Dependency Parsing(Nirve, 2004): Left-reduce Right-reduce Shift ================================================================= """ # TODO(Students) Start arc = transition[2:-1] if transition.startswith("S"): configuration.shift() else: word1 = configuration.get_stack(0) word2 = configuration.get_stack(1) if transition.startswith('L'): configuration.add_arc(word1, word2, arc) configuration.remove_second_top_stack() elif transition.startswith('R'): configuration.add_arc(word2, word1, arc) configuration.remove_top_stack() else: print('Unknown case encountered') pdb.set_trace() return configuration
def get_configuration_features(configuration: Configuration, vocabulary: Vocabulary) -> List[List[int]]: """ ================================================================= Implement feature extraction described in "A Fast and Accurate Dependency Parser using Neural Networks"(2014) ================================================================= """ # TODO(Students) Start features = [] # Store list of all features (initially only word features) pos_features = [] # Store list of pos tag features label_features = [] # Store list of label features top3Stack = [configuration.get_stack(i) for i in range(3)] # top 3 elements of stack top3Buffer = [configuration.get_buffer(i) for i in range(3)] # top 3 elements of buffer for token_index in top3Stack + top3Buffer: # Iterate over top 3 words in stack and top 3 words in buffer # Add word to the features features.append( vocabulary.get_word_id(configuration.get_word(token_index))) # Add pos tag of corresponding word to the pos_features pos_features.append( vocabulary.get_pos_id(configuration.get_pos(token_index))) for token_index in top3Stack[:2]: # Iterate over top 2 words in stack # Iterate over 1 and 2 to get 1st leftmost, 1st rightmost, 2nd leftmost and 2nd rightmost child # of corresponding word in stack. for i in range(1, 3): ith_left_child = configuration.get_left_child( token_index, i) # Get ith_leftmost_child of word in stack # Add child to the features features.append( vocabulary.get_word_id(configuration.get_word(ith_left_child))) # Add pos tag of corresponding child to the pos_features pos_features.append( vocabulary.get_pos_id(configuration.get_pos(ith_left_child))) # Add label of corresponding child to the label_features label_features.append( vocabulary.get_label_id( configuration.get_label(ith_left_child))) # Similarly for rightmost child add child word, pos tag and label to respective features list ith_right_child = configuration.get_right_child(token_index, i) features.append( vocabulary.get_word_id( configuration.get_word(ith_right_child))) pos_features.append( vocabulary.get_pos_id(configuration.get_pos(ith_right_child))) label_features.append( vocabulary.get_label_id( configuration.get_label(ith_right_child))) for token_index in top3Stack[:2]: # Iterate over top 2 words in stack # Get leftmost child of leftmost child of word in stack left_left_child = configuration.get_left_child( configuration.get_left_child(token_index, 1), 1) # Add the corresponding child word, pos tag and label to respective features list features.append( vocabulary.get_word_id(configuration.get_word(left_left_child))) pos_features.append( vocabulary.get_pos_id(configuration.get_pos(left_left_child))) label_features.append( vocabulary.get_label_id(configuration.get_label(left_left_child))) # Similarly for rightmost child of rightmost child add child word, pos tag and label to respective features list right_right_child = configuration.get_right_child( configuration.get_right_child(token_index, 1), 1) features.append( vocabulary.get_word_id(configuration.get_word(right_right_child))) pos_features.append( vocabulary.get_pos_id(configuration.get_pos(right_right_child))) label_features.append( vocabulary.get_label_id( configuration.get_label(right_right_child))) features += pos_features + label_features # Append the pos and label features to the word features # TODO(Students) End assert len(features) == 48 return features
def is_terminal(self, configuration: Configuration) -> bool: return configuration.get_stack_size( ) == 1 and configuration.get_buffer_size() == 0