def __init__(self, input_dim, output_dim, **params): super(LSTM, self).__init__() self.input_dim = input_dim self.output_dim = output_dim self.loss_type = params["loss_type"] self.learning_rate = params["learning_rate"] self.optimizer_type = params["optimizer_type"] self.grad_clip = params["grad_clip"] self.l2_reg = params["l2_reg"] self.dropout_rate = params["dropout_rate"] self.num_epochs = params["num_epochs"] self.early_stop_tolerance = params["early_stop_tolerance"] self.hidden_dim_list = params["hidden_dim_list"] self.num_layers = len(self.hidden_dim_list) self.final_act_type = params["final_act_type"] self.relu_alpha = params["relu_alpha"] self.input_norm_method = params["input_norm_method"] self.output_norm_method = params["output_norm_method"] self.__create_rnn_cell_list() self.__create_dropout_layer() self.__create_dense_layer() self.__create_final_act_layer() self.optimizer = self.optimizer_dispatcher[self.optimizer_type]( self.parameters(), lr=self.learning_rate, weight_decay=self.l2_reg) self.input_normalizer = Normalizer(self.input_norm_method) self.output_normalizer = Normalizer(self.output_norm_method)
def __init__(self, **params): self.num_epochs = params["num_epochs"] self.early_stop_tolerance = params["early_stop_tolerance"] self.norm_method = params["norm_method"] self.loss_type = params["loss_type"] self.learning_rate = params["learning_rate"] self.l2_reg = params["l2_reg"] self.clip = params['clip'] self.device = params['device'] self.input_normalizer = Normalizer(self.norm_method) self.output_normalizer = Normalizer(self.norm_method)
def __init__(self, n_states, n_actions, n_goals, action_bounds, capacity, env, k_future, batch_size, action_size=1, tau=0.05, actor_lr=1e-3, critic_lr=1e-3, gamma=0.98): self.device = device("cpu") self.n_states = n_states self.n_actions = n_actions self.n_goals = n_goals self.k_future = k_future self.action_bounds = action_bounds self.action_size = action_size self.env = env self.actor = Actor(self.n_states, n_actions=self.n_actions, n_goals=self.n_goals).to(self.device) self.critic = Critic(self.n_states, action_size=self.action_size, n_goals=self.n_goals).to(self.device) self.sync_networks(self.actor) self.sync_networks(self.critic) self.actor_target = Actor(self.n_states, n_actions=self.n_actions, n_goals=self.n_goals).to(self.device) self.critic_target = Critic(self.n_states, action_size=self.action_size, n_goals=self.n_goals).to(self.device) self.init_target_networks() self.tau = tau self.gamma = gamma self.capacity = capacity self.memory = Memory(self.capacity, self.k_future, self.env) self.batch_size = batch_size self.actor_lr = actor_lr self.critic_lr = critic_lr self.actor_optim = Adam(self.actor.parameters(), self.actor_lr) self.critic_optim = Adam(self.critic.parameters(), self.critic_lr) self.state_normalizer = Normalizer(self.n_states[0], default_clip_range=5) self.goal_normalizer = Normalizer(self.n_goals, default_clip_range=5)
def _prepare_normalizers(self): normalizer_tf = dict() with tf.variable_scope('o_stats'): normalizer_tf['o'] = Normalizer(self._env_spec['o_dim'], **self._normalizer_params) with tf.variable_scope('u_stats'): normalizer_tf['a'] = Normalizer(self._env_spec['a_dim'], **self._normalizer_params) with tf.variable_scope('abs_pred_err_stats'): normalizer_tf['abs_pred_err'] = Normalizer( self._env_spec['o_dim'], **self._normalizer_params) return normalizer_tf
def __init__(self, env, act_dim, state_dim, goal_dim, act_range, buffer_size=int(1e6), gamma=0.98, lr=0.001, tau=0.95): """ Initialization """ # Environment and A2C parameters self.act_dim = act_dim self.act_range = act_range self.env_dim = state_dim + goal_dim self.gamma = gamma self.lr = lr self.tau = tau self.env = env # Create actor and critic networks self.actor_network = Actor(self.env_dim, act_dim, act_range) self.actor_target_network = Actor(self.env_dim, act_dim, act_range) self.actor_target_network.load_state_dict( self.actor_network.state_dict()) self.critic_network = Critic(self.env_dim, act_dim, act_range) self.critic_target_network = Critic(self.env_dim, act_dim, act_range) self.actor_target_network.load_state_dict( self.actor_network.state_dict()) sync_networks(self.actor_network) sync_networks(self.critic_network) # Optimizer self.actor_optim = torch.optim.Adam(self.actor_network.parameters(), lr=lr) self.critic_optim = torch.optim.Adam(self.critic_network.parameters(), lr=lr) # Replay buffer # self.buffer = MemoryBuffer(buffer_size) self.buffer = ReplayMemory(buffer_size) # Normalizers self.goal_normalizer = Normalizer( goal_dim, default_clip_range=5) # Clip between [-5, 5] self.state_normalizer = Normalizer(state_dim, default_clip_range=5)
def __init__(self, host='192.168.0.107', port=7777, list_file='inputfiles-full.txt', freqs_file='wordlist', dataset_dir='/home/aelphy/Desktop/ir_project_dataset'): self.normalizer = Normalizer() self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.connect((host, port)) self.document_freqs_list_filename = freqs_file self.document_list_filename = list_file self.dataset_dir = dataset_dir self.documents_freqs = {} self.document_identificators = {} self.identificator_documents = {} with open(self.document_list_filename) as f: for line in f: data = line.strip().split() index = int(data[0]) identifier = data[1] self.document_identificators[index] = identifier self.identificator_documents[identifier] = index self.documents_number = index with open(self.document_freqs_list_filename) as f: for line in f: data = line.strip().split() self.documents_freqs[self.identificator_documents[data[1]]] = int(data[0]) self.avgdl = sum(self.documents_freqs.values()) / float(self.documents_number) self.k1 = 2.0 self.b = 0.75
def __init__(self, hp=None, input_size=None, output_size=None, normalizer=None, policy=None, monitor_dir=None): self.hp = hp or Hp() np.random.seed(self.hp.seed) self.env = gym.make(self.hp.env_name) if monitor_dir is not None: # Record periodic videos should_record = lambda i: self.record_video self.env = wrappers.Monitor(self.env, monitor_dir, video_callable=should_record, force=True) self.hp.episode_length = self.env.spec.timestep_limit or self.hp.episode_length self.input_size = input_size or self.env.observation_space.shape[0] self.output_size = output_size or self.env.action_space.shape[0] self.normalizer = normalizer or Normalizer(self.input_size) self.policy = policy or Policy(self.input_size, self.output_size, self.hp) self.record_video = False
def initialise_everything(): print('''This procedure can take up to hours to finish. The program will now run: - Normalisation pipeline over the shape database. (~3hrs) - Feature extraction over shape database. (~2hrs)\n Are you sure you want to continue (y/n)?\n ''') choice = input(">> ") if choice == "n" or choice == "no": return with open('config.json') as f: data = json.load(f) path_psd = data["DATA_PATH_PSB"] path_normed = data["DATA_PATH_NORMED"] path_feature = data["FEATURE_DATA_FILE"] db = PSBDataset() if len(os.listdir(path_psd)) == 0: print("No valid dataset found.\nPoint to a valid dataset.") return else: prompt_for_class_files(path_psd) choice = input( "Do you wish to go back to the menu to change the current classification settings? (y/n)\n>> " ) if choice == "n": return if not os.path.isfile(path_normed): print("No valid normalised dataset found.\nRunning normalisation.") norm = Normalizer(db) norm.run_full_pipeline() if not os.path.isfile(path_feature): print("No valid feature file found.\nRun feature extraction.") FE = FeatureExtractor(db) FE.run_full_pipeline()
def test_timezone_converstion(self): norm = Normalizer() test_timestamp = "10/2/04 8:44:11 AM" expected_value = "2004-10-02T12:37:11-04:00" test_value = norm.timezone_convert_to_est(test_timestamp) self.assertEqual(expected_value, test_value)
def wrangle(path, out_path): """ An example to show how to use wrangler :param path: path to input data file :param out_path: path to store normalized data """ spark = SparkSession.builder.getOrCreate() data = spark.read.csv(path, header=True, encoding='utf-8') functions = [lowercase, trim] # hospital cols columns = data.columns transformer = Transformer(functions, columns) data = transformer.transform(data) cols_info = list() # hospital cols for col in data.columns: cols_info.append(ColNormInfo(col)) normalizer = Normalizer(cols_info) data = normalizer.normalize(data) data.toPandas().to_csv(out_path, index=False, header=True)
def __init__(self, *args, **kwargs): """ Constructor method, initializes variables. """ # Initializing variables self.pattern_normalizer = Normalizer()
def mean_squared_error(self, processed_file=None, vocal_file=None): normalizer = Normalizer() normalize = normalizer.get(both=False) if processed_file is None: vocal_isolation = VocalIsolation(config) vocal_isolation.loadWeights(config.weights) data = Data() mses = [] for track in data.validation_tracks + data.test_tracks: mashup = data.prepare_spectrogram(data.mashup[track]) vocal = data.prepare_spectrogram(data.vocal[track]) mashup, norm = normalize(mashup) vocal, _ = normalize(vocal, norm) info = vocal_isolation.\ process_spectrogram(mashup, config.get_channels()) new_spectrogram = info[1] mse = ((new_spectrogram - vocal)**2).mean() mses.append(mse) print(track, mse) print(np.mean(mses)) else: vocal_audio, _ = conversion.load_audio_file(vocal_file) processed_audio, _ = conversion.load_audio_file(processed_file) # make sure audios have the same length vocal_audio = vocal_audio[:processed_audio.shape[0]] processed_audio = processed_audio[:vocal_audio.shape[0]] wave_mse = ((vocal_audio - processed_audio)**2).mean() print("\n") self._write("Wave mean squared error: %s" % wave_mse)
def test_convert_to_float_seconds(self): norm = Normalizer() test_timestamp = "100:1:0.0" expected_result = (100 * 3600) + (60 * 1) + (0) test_value = norm.convert_to_float_seconds(test_timestamp) self.assertEqual(expected_result, test_value)
def __init__(self, mode, data_path): self.normalizer = Normalizer() self.data_path = data_path self.mode = mode self.filenames = [] self.contents = [] self.labels = []
def test_compare_to_scikit_learn_changing_k(self): normalizer = Normalizer(self.data) data = normalizer.normalize() testSize = 100 trainSize = len(data.data) - testSize for i in range(1, 12): with self.subTest(i=i): print("k: ", i) neighbours = i trainData = {} testData = {} trainData['data'] = data.data[:trainSize] trainData['target'] = data.target[:trainSize] testData['data'] = data.data[trainSize:] testData['target'] = data.target[:trainSize] knn = KNN(trainData) #scikit-learn model: model = KNeighborsClassifier(n_neighbors=neighbours) model.fit(trainData['data'], trainData['target']) ourCounter = 0 sciCounter = 0 for i, e in enumerate(testData['data']): if knn.makeGuess(e, neighbours) == testData['target'][i]: ourCounter+=1 if model.predict([e]) == testData['target'][i]: sciCounter+=1 self.assertAlmostEqual(ourCounter/(testSize), sciCounter/(testSize), 3)
def prepare_data(self, chop, tracks, post_process=False): normalize = Normalizer().get() x = [] y = [] for track in tracks: x.append(self.mashup[track]) if self.is_instrumental: y.append(self.instrumental[track]) else: y.append(self.vocal[track]) x = [self.prepare_spectrogram(s) for s in x] y = [self.prepare_spectrogram(s) for s in y] x, y = normalize(x, y) mashup_slices = [] output_slices = [] for mashup, output in zip(x, y): x_slices, y_slices = chop(mashup, output) x_slices = np.array(x_slices)[:] y_slices = np.array(y_slices)[:] mashup_slices.append(x_slices) output_slices.append(y_slices) return mashup_slices, output_slices
def __init__(self): self.__vocab: t.List[Replacement] = self.__make_vocab() self.__normalizer: Normalizer = Normalizer(self.__vocab) self.__string: str = '' self.__word: Word = Word() self.__normalized_word: Word = Word()
def __init__(self,env, env_params, args, models=None, record_episodes=[0,.1,.25,.5,.75,1.]): self.env= env self.env_params = env_params self.args = args # networks if models == None: self.actor = Actor(self.env_params).double() self.critic = Critic(self.env_params).double() else: self.actor , self.critic = self.LoadModels() # target networks used to predict env actions with self.actor_target = Actor(self.env_params,).double() self.critic_target = Critic(self.env_params).double() self.actor_target.load_state_dict(self.actor.state_dict()) self.critic_target.load_state_dict(self.critic.state_dict()) if self.args.cuda: self.actor.cuda() self.critic.cuda() self.actor_target.cuda() self.critic_target.cuda() self.actor_optim = torch.optim.Adam(self.actor.parameters(), lr=0.001) self.critic_optim = torch.optim.Adam(self.critic.parameters(), lr=0.001) self.normalize = Normalizer(env_params,self.args.gamma) self.buffer = ReplayBuffer(1_000_000, self.env_params) self.tensorboard = ModifiedTensorBoard(log_dir = f"logs") self.record_episodes = [int(eps * self.args.n_epochs) for eps in record_episodes]
def process(): # load in the images as Image objects files = load_files('raw_images') nameCtr = 0 print("Iterating through files") print(files) for img in files: nameCtr += 1 # Use a Histogram Normalization technique norm = Normalizer(img).normalize() #Median Noise Reduction (blurring), using kernel size of 7 reducedNoise = NoiseReducer(norm).median_reduction(5) thresholded_img = Thresholder(reducedNoise).binary_threshold(50, 255) outputimg = np.empty_like(thresholded_img.read_bright_field()) # erode Image (bf_cntours, gfp_cntours, hierbf, hiergfp) = ImageRegion(thresholded_img, 50, 255).get_contours() cv2.drawContours(outputimg, bf_cntours, -1, (255, 255, 255), 2) # Output the bright field cv2.imwrite( "output/" + str(thresholded_img.name) + "out" + "_" + str(len(bf_cntours)) + "worms.jpg", outputimg) # cv2.imwrite("threshold.jpg",thresholded_img.read_bright_field()) cv2.imwrite("norm.jpg", norm.read_bright_field()) #print("output/" + str(thresholded_img.name) + "out.jpg") break
def __init__(self, parent=None, show=True): Qt.QMainWindow.__init__(self, parent) with open('config.json') as f: self.config_data = json.load(f) self.query_matcher = QueryMatcher(self.config_data["FEATURE_DATA_FILE"]) self.supported_file_types = [".ply", ".off"] self.buttons = {} self.ds = reader.DataSet("") self.meshes = [] self.normalizer = Normalizer() self.smlw = None self.setWindowTitle('Source Mesh Window') self.frame = Qt.QFrame() self.QTIplotter = None self.vlayout = Qt.QVBoxLayout() self.frame.setLayout(self.vlayout) self.setCentralWidget(self.frame) self.hist_dict = {} self.setAcceptDrops(True) # Create main menu mainMenu = self.menuBar() fileMenu = mainMenu.addMenu('File') exitButton = Qt.QAction('Exit', self) exitButton.setShortcut('Ctrl+Q') exitButton.triggered.connect(self.close) fileMenu.addAction(exitButton) viewMenu = mainMenu.addMenu('View') exitButton = Qt.QAction('Plot tSNE', self) exitButton.triggered.connect(self.plot_tsne) viewMenu.addAction(exitButton) # Create load button and init action self.load_button = QPushButton("Load or drop mesh to query") self.load_button.clicked.connect(lambda: self.load_and_prep_query_mesh(self.open_file_name_dialog())) self.load_button.setFont(QtGui.QFont("arial", 30)) # Create Plots widget self.graphWidget = pg.PlotWidget() self.graphWidget.setBackground('w') # Create and add widgets to layout n_sing, n_hist, mapping_of_labels = get_sizes_features(features_file=self.config_data["FEATURE_DATA_FILE"],with_labels=True) # self.hist_labels = list({**FeatureExtractor.get_pipeline_functions()[1]}.values()) self.hist_labels = [val for key, val in mapping_of_labels.items() if "hist_" in key] self.tableWidget = TableWidget({}, self, {}) self.tableWidget.hide() self.vlayout.addWidget(self.load_button) # Position MainWindow screen_topleft = QDesktopWidget().availableGeometry().topLeft() screen_height = QDesktopWidget().availableGeometry().height() width = (QDesktopWidget().availableGeometry().width() * 0.4) self.move(screen_topleft) self.resize(width, screen_height - 50) if show: self.show()
def __init__(self, observation_space, action_space, discount=0.99, td_lambda=0.95, hidden_size=(128, 64), temp=1., max_weight=20, action_std=0.4, actor_lr=0.0001, critic_lr=0.01, device='cpu', batch_size=256, pipe=None, optimizer='SGD', activation='relu'): self.device = device inp_dim = observation_space.shape[0] self.actor = actor(inp_dim, action_space.low.shape[0], std=action_std, hidden_size=hidden_size, activation=activation).to(device) self.critic = critic(inp_dim, hidden_size=hidden_size, activation=activation).to(device) self.normalizer = Normalizer((inp_dim, ), default_clip_range=5).to(device) self.normalizer.count += 1 #unbiased ... self.temp = temp self.max_weight = max_weight # NOTE: optimizer is different if optimizer == 'SGD': self.optim_actor = torch.optim.SGD(self.actor.parameters(), actor_lr, momentum=0.9) self.optim_critic = torch.optim.SGD(self.critic.parameters(), critic_lr, momentum=0.9) else: self.optim_actor = torch.optim.Adam(self.actor.parameters(), actor_lr) self.optim_critic = torch.optim.Adam(self.critic.parameters(), critic_lr) self.pipe = pipe self.batch_size = batch_size self.mse = nn.MSELoss() self.discount = discount self.td_lambda = td_lambda self.val_norm = 1.0 / (1.0 - self.discount) self.action_mean = ((action_space.high + action_space.low) / 2)[None, :] self.action_std = ((action_space.high - action_space.low) / 2)[None, :]
def normalizeDatabase(self): """ If a collection has a field which contains several fields, we extract those fields and make a new collection out of it """ n = Normalizer(self.metadata_db, self.dataset_db) # Bombs away! LOG.info("Processing mongodb dataset normalization") n.process() LOG.info("Finshing normalization")
def get_invalid_matches(manifest, input): """util function to return any invalid matches when running through a test file""" normalizer = Normalizer() normalizer.read_manifest(manifest) samples = read_input(input) matched = normalize_samples(normalizer, samples, verbose=False) invalid_matches = [match for match in matched if match['expected'] != match['output']] return invalid_matches
def stoi(self, filepath, clean_filepath=None): # filepath = path to mashup # Needs octave and octave-signal installed # Use "pip install oct2py" to install python - octave bridge # STOI assumes # * a sampling rate of 10kHz, resamples otherwise # * window length of 384ms # * 15 third octave bands over full frequency range # * overlapping segments with hanning window # * removes silent frames import librosa from oct2py import octave if clean_filepath is None: # No clean file given. # Get processed and clean file from mashup. vocal_isolation = VocalIsolation(config) vocal_isolation.loadWeights(config.weights) audio, sampleRate = conversion.load_audio_file(filepath) spectrogram = conversion.audio_file_to_spectrogram( audio, fftWindowSize=config.fft, learn_phase=self.config.learn_phase) normalizer = Normalizer() normalize = normalizer.get(both=False) denormalize = normalizer.get_reverse() # normalize spectogram, norm = normalize(spectrogram) info = vocal_isolation.process_spectrogram(spectrogram, config.get_channels()) spectrogram, new_spectrogram = info # de-normalize new_spectrogram = denormalize(new_spectrogram, norm) processed = conversion.spectrogram_to_audio_file(new_spectrogram, config.fft, config.phase_iterations) clean_filepath = filepath.replace("_all.wav", "_vocal.wav") clean, sampling_rate = librosa.load(clean_filepath) else: # A clean file is given. # Compare it with the processed audio. processed, sampling_rate = librosa.load(filepath) clean, sampling_rate = librosa.load(clean_filepath) # Make sure the original and processed audio have the same length clean = clean[:processed.shape[0]] octave.eval("pkg load signal") d = octave.stoi(clean, processed, sampling_rate) self._write("stoi: %f" % d)
def test_normalize_row(self): normalizer = Normalizer() self.assertEqual(normalizer.normalize_row('a'), 'a') self.assertEqual(normalizer.normalize_row('B'), 'b') self.assertEqual(normalizer.normalize_row('1'), '1') self.assertEqual(normalizer.normalize_row('Row C'), 'c') self.assertEqual(normalizer.normalize_row('row 12'), '12') self.assertEqual(normalizer.normalize_row('r31'), '31') self.assertEqual(normalizer.normalize_row('Cc'), 'cc') self.assertEqual(normalizer.normalize_row('37Wc '), '37') self.assertEqual(normalizer.normalize_row('A-Z '), 'a-z') self.assertEqual(normalizer.normalize_row('AA-XX '), 'aa-xx') self.assertEqual(normalizer.normalize_row('1-10 '), '1-10')
def test_extract_section_features(self): normalizer = Normalizer() self.assertEqual(normalizer.extract_section_features('136'), generate_feature(d='136')) self.assertEqual(normalizer.extract_section_features('Reserve 40 '), generate_feature(pp="reserve", d='40')) self.assertEqual(normalizer.extract_section_features('Top Deck 6 '), generate_feature(pp="top deck", d='6')) self.assertEqual(normalizer.extract_section_features('31RS '), generate_feature(d='31', s='rs')) self.assertEqual(normalizer.extract_section_features('Left Field Pavilion 311'), generate_feature(pp='left field pavilion', d='311')) self.assertEqual(normalizer.extract_section_features('Infield Reserve IFR7 '), generate_feature(pp='infield reserve', p='ifr', d='7')) self.assertEqual(normalizer.extract_section_features('311PL'), generate_feature(d='311', s='pl')) self.assertEqual(normalizer.extract_section_features('F9'), generate_feature(p='f', d='9')) self.assertEqual(normalizer.extract_section_features('L36'), generate_feature(p='l', d='36'))
def create_network(self): # for actor network self.o_stats = Normalizer(size=self.dimo, eps=self.norm_eps, default_clip_range=self.norm_clip) if self.use_goal: self.g_stats = Normalizer(size=self.dimg, eps=self.norm_eps, default_clip_range=self.norm_clip) else: self.g_stats = None self.main = ActorCritic(self.o_stats, self.g_stats, self.input_dims, self.use_goal).to(self.device) self.target = ActorCritic(self.o_stats, self.g_stats, self.input_dims, self.use_goal).to(self.device) self.target.actor = copy.deepcopy(self.main.actor) self.target.critic = copy.deepcopy(self.main.critic) self.actor_optimizer = optim.Adam(self.main.actor.parameters(), lr=self.pi_lr) self.critic_optimizer = optim.Adam(self.main.critic.parameters(), lr=self.Q_lr)
def reload(self): """Refreshes this instance's normalizers pool.""" self.normalizers = {'raw': [], 'body': []} for path in self.iter_normalizer(): norm = parse(open(path)) if not self.dtd.validate(norm): warnings.warn('Skipping %s : invalid DTD' % path) print 'invalid normalizer ', path else: normalizer = Normalizer(norm, self.ctt, self.ccb) normalizer.uuid = self._compute_norm_uuid(normalizer) self.normalizers.setdefault(normalizer.appliedTo, []) self.normalizers[normalizer.appliedTo].append(normalizer) self.activate_normalizers()
def __init__(self, hparams): super(HER, self).__init__() self.hparams = hparams self.test_env = make_env(hparams, render=self.hparams.render_test) sample_obs = self.test_env.observation_space['observation'].sample() sample_goal = self.test_env.observation_space['achieved_goal'].sample() # HARD CODED VALUES FOR Bullet-HRL action_limits, state_limits = get_env_boundaries() action_offset, action_bounds, action_clip_low, action_clip_high = action_limits state_shape = sample_obs.shape[0] action_shape = self.test_env.action_space.shape[0] goal_shape = sample_goal.shape[0] self.action_clips = (action_clip_low, action_clip_high) self.model = DDPG(params=self.hparams, obs_size=state_shape, goal_size=goal_shape, act_size=action_shape, action_clips=(action_clip_low, action_clip_high), action_bounds=action_bounds, action_offset=action_offset) self.model.actor.share_memory() self.model.critic.share_memory() self.state_normalizer = Normalizer( state_shape, default_clip_range=self.hparams.clip_range) self.goal_normalizer = Normalizer( goal_shape, default_clip_range=self.hparams.clip_range) self.replay_buffer = SharedReplayBuffer(self.hparams.buffer_size, state_shape, action_shape, goal_shape)
def create_app(test_config=None): app = Flask(__name__) app.config['JSON_AS_ASCII'] = False # retrieve UTF-8 messages norm = Normalizer() @app.route('/reply', methods=['POST']) def reply(): params = request.json if not params: return jsonify({ "status": "error", "error": "Request must be of the application/json type!", }) message = params.get("message") method = params.get("method") # Make sure the required params are present. if message is None or method is None: return jsonify({ "status": "error", "error": "message and method are required keys" }) methods = { 'token': norm.tokenizer, 'spell': norm.speller, 'acronym': norm.acronym_searcher, 'textese': norm.untextese, 'proper_noun': norm.proper_noun_normalizer } try: reply = methods[method](message) except KeyError: return jsonify({ "status": "error", "error": "method not valid, try one of the following: token, spell, acronym, textese or proper_noun" }) # Send the response. return jsonify({"status": "ok", "reply": reply}) return app