def __init__(self): self.util = Util() self.document = docx # 域名 self.domain_name = 'shiyantai_a_1' self.kw_excel_name = '{}_keywords.xlsx'.format(self.domain_name) #读取 self.read_path = r'./data/read_path/{}'.format(self.domain_name) self.keyword_path = r'./data/read_path/{}/{}'.format(self.domain_name, self.kw_excel_name) # 保存路径 self.save_article_path = r'./data/save_path/{}_articles'.format(self.domain_name) self.save_img_path = r'./data/save_path/{}_imgs'.format(self.domain_name) self.start_keyword = 0 # 关键词开始的位置 self.end_keyword = 150 # 关键词结束的位置 self.special_keyword = '苏州' #################### 打包 ########################## # self.domain_name = raw_input('please input domain name:') # self.kw_excel_name = '{}_keywords.xlsx'.format(self.domain_name) # self.read_path = r'../data/read_path/{}'.format(self.domain_name) # self.keyword_path = r'../data/read_path/{}/{}'.format(self.domain_name, self.kw_excel_name) # # 保存路径 # self.save_article_path = r'../data/save_path/{}_articles'.format(self.domain_name) # self.save_img_path = r'../data/save_path/{}_imgs'.format(self.domain_name) # self.start_keyword = int(raw_input('start keyword index:')) # 关键词开始的位置 # self.end_keyword = int(raw_input('end keyword index:')) # 关键词结束的位置 # self.special_keyword = raw_input('please input special keyword(example:"苏州"):') ######################################################## # self.domain_name = 'uni_technology' # self.keywords_num = 180 # 关键词数量 # 已使用的关键字 self.used_keyword = []
def __init__(self, path): self.util = Util() self.document = docx # 域名 self.read_path = path self.domain_name = "_".join(path.split("/")[-1].split("_")[:3]) self.start_keyword = 0 # 关键词开始的位置 self.end_keyword = int(path.split("/")[-1].split("_")[-1]) # 关键词结束的位置 # self.special_keyword = '苏州' self.used_keyword = [] # 保存路径 self.save_article_path = r'./data/save_path/{}_articles'.format( self.domain_name) self.save_img_path = r'./data/save_path/{}_imgs'.format( self.domain_name) #################### 打包 ########################## # self.save_article_path = r'../data/save_path/{}_articles'.format(self.domain_name) # self.save_img_path = r'../data/save_path/{}_imgs'.format(self.domain_name) ######################################################## # self.domain_name = 'uni_technology' # self.keywords_num = 180 # 关键词数量 # 已使用的关键字 self.used_keyword = [] # 未使用的段落 # self.unused_paragraphs = [] # 已使用的图片 # self.used_pictures = [] # 所有的段落 # self.paragraphs = [] # self.keywords = self.read_xlsx(self.read_path + '\keyword.xlsx') self.used_articles = list()
def main(): util = Util() dir = args.data_dir rows = args.rows ratings, to_read, books = util.read_data(dir) ratings = util.clean_subset(ratings, rows) num_vis = len(ratings) free_energy = args.free_energy train = util.preprocess(ratings) valid = None if free_energy: train, valid = util.split_data(train) H = args.num_hid user = args.user alpha = args.alpha w = np.random.normal(loc=0, scale=0.01, size=[num_vis, H]) rbm = RBM(alpha, H, num_vis) epochs = args.epochs batch_size = args.batch_size v = args.verbose reco, prv_w, prv_vb, prv_hb = rbm.training(train, valid, user, epochs, batch_size, free_energy, v) unread, read = rbm.calculate_scores(ratings, books, to_read, reco, user) rbm.export(unread, read)
def __make_conspicuity_map(self, srcs): util = Util() intensity = self.__scale_add(list(map(util.normalize, srcs['intensity']))) for key in srcs['colors'].keys(): srcs['colors'][key] = list(map(util.normalize, srcs['colors'][key])) color = self.__scale_add([srcs['colors']['bg'][x] + srcs['colors']['ry'][x] for x in range(len(srcs['colors']['bg']))]) orientation = np.zeros(intensity.shape) for key in srcs['orientations'].keys(): orientation += self.__scale_add(list(map(util.normalize, srcs['orientations'][key]))) return {'intensity': intensity, 'color': color, 'orientation': orientation}
def __init__(self): self.util = Util() self.file_process = File_processing1() # 每次运行需要修改的点 self.domain_name = self.file_process.domain_name self.kw_excel_name = self.file_process.kw_excel_name # 读取路径 self.read_path = self.file_process.read_path self.keyword_path = self.file_process.keyword_path # 保存路径 self.save_article_path = r'./data/save_path/{}_articles_no_picture'.format( self.domain_name) # self.save_img_path = r'./data/save_path/{}_imgs_with_no_picture'.format(self.domain_name) # self.domain_name = 'uni_technology' # self.keywords_num = 180 # 关键词数量 # 已使用的关键字 self.used_keyword = []
def __init__(self): self.util = Util() self.document = docx # 域名 self.domain_name = 'ganyingmen_mix_1' self.kw_excel_name = '{}_keywords.xlsx'.format(self.domain_name) #读取 self.read_path = r'./data/read_path/{}'.format(self.domain_name) self.keyword_path = r'./data/read_path/{}/{}'.format( self.domain_name, self.kw_excel_name) # 保存路径 self.save_article_path = r'./data/save_path/{}_articles'.format( self.domain_name) self.save_img_path = r'./data/save_path/{}_imgs'.format( self.domain_name) self.special_keyword = '苏州' self.start_keyword = 0 # 关键词开始的位置 self.end_keyword = 150 # 关键词结束的位置 self.percent = "2:1" # 新旧文章段落比,目前只支持整数比 #################### 打包 ########################## # self.domain_name = raw_input('please write domain name(example:"senxiqs_mix_1"):') # self.kw_excel_name = '{}_keywords.xlsx'.format(self.domain_name) # self.read_path = r'../data/read_path/{}'.format(self.domain_name) # self.keyword_path = r'../data/read_path/{}/{}'.format(self.domain_name, self.kw_excel_name) # # 保存路径 # self.save_article_path = r'../data/save_path/{}_articles'.format(self.domain_name) # self.save_img_path = r'../data/save_path/{}_imgs'.format(self.domain_name) # self.special_keyword = raw_input('please input special keyword(example:"苏州"):') # self.start_keyword = int(raw_input('start keyword index:')) # 关键词开始的位置 # self.end_keyword = int(raw_input('end keyword index:')) # 关键词结束的位置 # self.percent = 2 # 新旧文章段落比,目前只支持整数比, 如值是2意为新旧文章段落比是2:1 ######################################################## # self.domain_name = 'uni_technology' # self.keywords_num = 180 # 关键词数量 # 已使用的关键字 self.used_keyword = [] # 未使用的段落 # self.unused_paragraphs = [] # 已使用的图片 # self.used_pictures = [] # 所有的段落 # self.paragraphs = [] # self.keywords = self.read_xlsx(self.read_path + '\keyword.xlsx') self.used_articles = list()
def __init__(self, path): self.util = Util() self.document = docx # 读取 self.read_path = path self.domain_name = "_".join(path.split("/")[-1].split("_")[:3]) self.start_keyword = 0 # 关键词开始的位置 self.end_keyword = int(path.split("/")[-1].split("_")[-1]) # 关键词结束的位置 # self.special_keyword = '苏州' self.used_keyword = [] # 保存路径 self.save_article_path = r'./data/save_path/{}_articles'.format( self.domain_name) self.save_img_path = r'./data/save_path/{}_imgs'.format( self.domain_name)
def save(): try: print("User %s logged in!" % request.form['fname']) db = mysql.connector.connect(host=host, user=user, password=password, database=database) cursor = db.cursor(buffered=True) util = Util() status, uid = util.saveUser(db, cursor, request) if status and status == 200: return render_template("thankyou.html", fname=request.form['fname'], uid=uid) raise Exception("Unable to insert data!") except Exception as e: print(json.dumps({"error": str(e)})) return "<h1>Oops! Something went wrong.. Could you try after sometime or reach out to the host!</h1>" finally: db.close() cursor.close()
def get_recc(att_df, cat_rating): util = Util() epochs = 50 rows = 40000 alpha = 0.01 H = 128 batch_size = 16 dir = 'etl/' ratings, attractions = util.read_data(dir) ratings = util.clean_subset(ratings, rows) rbm_att, train = util.preprocess(ratings) num_vis = len(ratings) rbm = RBM(alpha, H, num_vis) joined = ratings.set_index('attraction_id').join(attractions[[ "attraction_id", "category" ]].set_index("attraction_id")).reset_index('attraction_id') grouped = joined.groupby('user_id') category_df = grouped['category'].apply(list).reset_index() rating_df = grouped['rating'].apply(list).reset_index() cat_rat_df = category_df.set_index('user_id').join( rating_df.set_index('user_id')) cat_rat_df['cat_rat'] = cat_rat_df.apply(f, axis=1) cat_rat_df = cat_rat_df.reset_index()[['user_id', 'cat_rat']] cat_rat_df['user_data'] = [cat_rating for i in range(len(cat_rat_df))] cat_rat_df['sim_score'] = cat_rat_df.apply(sim_score, axis=1) user = cat_rat_df.sort_values(['sim_score']).values[0][0] print("Similar User: {u}".format(u=user)) filename = "e" + str(epochs) + "_r" + str(rows) + "_lr" + str( alpha) + "_hu" + str(H) + "_bs" + str(batch_size) reco, weights, vb, hb = rbm.load_predict(filename, train, user) unseen, seen = rbm.calculate_scores(ratings, attractions, reco, user) rbm.export(unseen, seen, 'rbm_models/' + filename, str(user)) return filename, user, rbm_att
"""Insertion Sort Desc -> Reads in strings and prints them in sorted order using insertion sort. I/P -> read in the list words Logic -> Use Insertion Sort to sort the words in the String array O/P -> Print the Sorted List """ from utils import Util elements_number = int(input("Enter number of elements : ")) insertion_object = Util() result_array = insertion_object.insertion_sort(elements_number)
def __make_saliency_map(self, srcs): util = Util() srcs = map(util.normalize, [srcs[key] for key in srcs.keys()]) return srcs[0] / 3. + srcs[1] / 3. + srcs[2] / 3.
def training(self, train, valid, user, epochs, batchsize, free_energy, verbose): ''' Function where RBM training takes place ''' vb = tf.placeholder(tf.float32, [self.num_vis]) # Number of unique books hb = tf.placeholder( tf.float32, [self.num_hid]) # Number of features were going to learn W = tf.placeholder(tf.float32, [self.num_vis, self.num_hid]) # Weight Matrix v0 = tf.placeholder(tf.float32, [None, self.num_vis]) print("Phase 1: Input Processing") _h0 = tf.nn.sigmoid(tf.matmul(v0, W) + hb) # Visible layer activation # Gibb's Sampling h0 = tf.nn.relu(tf.sign(_h0 - tf.random_uniform(tf.shape(_h0)))) print("Phase 2: Reconstruction") _v1 = tf.nn.sigmoid(tf.matmul(h0, tf.transpose(W)) + vb) # Hidden layer activation v1 = tf.nn.relu(tf.sign(_v1 - tf.random_uniform(tf.shape(_v1)))) h1 = tf.nn.sigmoid(tf.matmul(v1, W) + hb) print("Creating the gradients") w_pos_grad = tf.matmul(tf.transpose(v0), h0) w_neg_grad = tf.matmul(tf.transpose(v1), h1) # Calculate the Contrastive Divergence to maximize CD = (w_pos_grad - w_neg_grad) / tf.to_float(tf.shape(v0)[0]) # Create methods to update the weights and biases update_w = W + self.alpha * CD update_vb = vb + self.alpha * tf.reduce_mean(v0 - v1, 0) update_hb = hb + self.alpha * tf.reduce_mean(h0 - h1, 0) # Set the error function, here we use Mean Absolute Error Function err = v0 - v1 err_sum = tf.reduce_mean(err * err) # Initialize our Variables with Zeroes using Numpy Library # Current weight cur_w = np.zeros([self.num_vis, self.num_hid], np.float32) # Current visible unit biases cur_vb = np.zeros([self.num_vis], np.float32) # Current hidden unit biases cur_hb = np.zeros([self.num_hid], np.float32) # Previous weight prv_w = np.random.normal(loc=0, scale=0.01, size=[self.num_vis, self.num_hid]) # Previous visible unit biases prv_vb = np.zeros([self.num_vis], np.float32) # Previous hidden unit biases prv_hb = np.zeros([self.num_hid], np.float32) print("Running the session") config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) print("Training RBM with {0} epochs and batch size: {1}".format( epochs, batchsize)) print("Starting the training process") util = Util() for i in range(epochs): for start, end in zip(range(0, len(train), batchsize), range(batchsize, len(train), batchsize)): batch = train[start:end] cur_w = sess.run(update_w, feed_dict={ v0: batch, W: prv_w, vb: prv_vb, hb: prv_hb }) cur_vb = sess.run(update_vb, feed_dict={ v0: batch, W: prv_w, vb: prv_vb, hb: prv_hb }) cur_hb = sess.run(update_hb, feed_dict={ v0: batch, W: prv_w, vb: prv_vb, hb: prv_hb }) prv_w = cur_w prv_vb = cur_vb prv_hb = cur_hb if valid: etrain = np.mean(util.free_energy(train, cur_w, cur_vb, cur_hb)) self.energy_train.append(etrain) evalid = np.mean(util.free_energy(valid, cur_w, cur_vb, cur_hb)) self.energy_valid.append(evalid) self.errors.append( sess.run(err_sum, feed_dict={ v0: train, W: cur_w, vb: cur_vb, hb: cur_hb })) if verbose: print("Error after {0} epochs is: {1}".format( i + 1, self.errors[i])) elif i % 10 == 9: print("Error after {0} epochs is: {1}".format( i + 1, self.errors[i])) if free_energy: print("Exporting free energy plot") self.export_free_energy_plot() print("Exporting errors vs epochs plot") self.export_errors_plot() inputUser = [train[user]] # Feeding in the User and Reconstructing the input hh0 = tf.nn.sigmoid(tf.matmul(v0, W) + hb) vv1 = tf.nn.sigmoid(tf.matmul(hh0, tf.transpose(W)) + vb) feed = sess.run(hh0, feed_dict={v0: inputUser, W: prv_w, hb: prv_hb}) rec = sess.run(vv1, feed_dict={hh0: feed, W: prv_w, vb: prv_vb}) return rec, prv_w, prv_vb, prv_hb
from flask import Flask, render_template, Response, request from camera import VideoCamera from drowsiness import Drowsy import json import requests from utils import Util import firebase_admin from firebase_admin import credentials, auth, firestore cred = credentials.Certificate('PATH_TO_YOUR_CERTIFICATE') user = {} u = Util() sleep_count = 0 yawn_count = 0 start_time = 0 ride_id = '' app = Flask(__name__) FIREBASE_WEB_API_KEY = "YOUR_FIREBASE_WEB_API_KEY" rest_api_url = f"https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={FIREBASE_WEB_API_KEY}" config = { "apiKey": "YOUR_API_KEY", "authDomain": "YOUR_AUTH_DOMAIN", "databaseURL": "YOUR_DATABASE_URL", "projectId": "YOUR_PROJECT_ID", "storageBucket": "YOUR_STORAGE_BUCKET", "messagingSenderId": "YOUR_MESSAGING_SENDER_ID", "appId": "YOUR_APP_ID" }
''' Simulate Stopwatch Program a. Desc -> Write a Stopwatch Program for measuring the time that elapses between the start and end clicks b. I/P -> Start the Stopwatch and End the Stopwatch c. Logic -> Measure the elapsed time between start and end d. O/P -> Print the elapsed time. ''' from utils import Util timer_object = Util() timer = timer_object.simulate_stopwatch()
''' 1. User Input and Replace String Template “Hello <<UserName>>, How are you?” a. I/P -> Take User Name as Input. Ensure UserName has min 3 char b. Logic -> Replace <<UserName>> with the proper name c. O/P -> Print the String with User Name ''' from utils import Util name = str(input("Enter a Username: ")) name_object = Util() new_string = name_object.get_username(name) print(new_string)
''' Sum of three Integer adds to ZERO a. Desc -> A program with cubic running time. Read in N integers and counts the number of triples that sum to exactly 0. b. I/P -> N number of integer, and N integer input array c. Logic -> Find distinct triples (i, j, k) such that a[i] + a[j] + a[k] = 0 d. O/P -> One Output is number of distinct triplets as well as the second output is to print the distinct triplets. ''' from utils import Util number_of_elements = int(input("Enter number of elements : ")) sum_object = Util() triplets_result = sum_object.triplets(number_of_elements) print(triplets_result)
def setUp(self): self.tester = TestUnit2() self.util = Util() self.util.start('inputs')
''' To the Util Class add temperaturConversion static function, given the temperature in fahrenheit as input outputs the temperature in Celsius or viceversa using the formula Celsius to Fahrenheit: (°C × 9/5) + 32 = °F Fahrenheit to Celsius: (°F − 32) x 5/9 = °C ''' from utils import Util unit = str( input( "Choose the unit of temperature as input\n(Press 'c' for Celsius or 'f' for Fahrenheit): " )) temperature_object = Util() convert = temperature_object.temperature_conversion(unit)
''' Write a program WindChill.java that takes two double command-line arguments t and v and prints the wind chill. Use Math.pow(a, b) to compute ab. Given the temperature t (in Fahrenheit) and the wind speed v (in miles per hour), the National Weather Service defines the effective temperature (the wind chill) to be: Note : the formula is not valid if t is larger than 50 in absolute value or if v is larger than 120 or less than 3 (you may assume that the values you get are in that range). ''' from utils import Util t = float(input("Temperature(in Fahrenheit) = ")) v = float(input("Wind speed(in mph) = ")) formula_object = Util() result = formula_object.windchill_formula(t, v)
''' 4. Power of 2 a. Desc -> This program takes a command-line argument N and prints a table of the powers of 2 that are less than or equal to 2^N. b. I/P -> The Power Value N. Only works if 0 <= N < 31 since 2^31 overflows an int c. Logic -> repeat until i equals N. d. O/P -> Print the year is a Leap Year or not. ''' from utils import Util integer_power = int(input("Enter the power of 2: ")) power_object = Util() result = power_object.powers_2(integer_power) print(result)
"""Bubble Sort a. Desc -> Reads in integers prints them in sorted order using Bubble Sort b. I/P -> read in the list ints c. O/P -> Print the Sorted List """ from utils import Util elements_number = int(input("Enter number of elements : ")) bubble_object = Util() result_array = bubble_object.bubble_sort(elements_number)
''' Gambler a. Desc -> Simulates a gambler who start with $stake and place fair $1 bets until he/she goes broke (i.e. has no money) or reach $goal. Keeps track of the number of times he/she wins and the number of bets he/she makes. Run the experiment N times, averages the results, and prints them out. b. I/P -> $Stake, $Goal and Number of times c. Logic -> Play till the gambler is broke or has won d. O/P -> Print Number of Wins and Percentage of Win and Loss. ''' from utils import Util stake = int(input("Enter stake: ")) goal = int(input("Enter a goal: ")) trials = int(input("Enter no. of trials: ")) gambler_object = Util() gamble = gambler_object.gambling(stake, goal, trials)
def __init__(self): self.username = self.addon.getSetting('username') self.password = self.addon.getSetting('password') self.session_key = self.addon.getSetting('session_key') self.util = Util()
def __init__(self): #constructor of class self.root = None self.comparacao = 0 self.troca = 0 self.util = Util()
''' Write a static function sqrt t o compute the square root of a nonnegative number c given in the input using Newton's method: - initialize t = c - replace t with the average of c/t and t - repeat until desired accuracy reached using condition Math.abs(t - c/t) > epsilon*t where epsilon = 1e-15 ''' from utils import Util c = abs(int(input("Enter a non negative number: "))) root_object = Util() square_root = root_object.sqrt_newton(c)
#!/usr/bin/env python3 import pprint import time from auth import authenticate_and_get_client from utils import Util client = authenticate_and_get_client() utils = Util(client) t0 = time.time() utils.iterate_over_folders_list(func=utils.check_duplicities) pp = pprint.PrettyPrinter(indent=4) duplicates = {} sizes = {} for file, data in utils.files.items(): sizes = {} if len(data) > 1: for item in data: for path in item: size = item[path] if size in sizes: sizes[size].append(path)
''' Write Binary.java to read an integer as an Input, convert to Binary using toBinary function and perform the following functions. i. Swap nibbles and find the new number. ii. Find the resultant number is the number is a power of 2. A nibble is a four-bit aggregation, or half an octet. There are two nibbles in a byte. Given a byte, swap the two nibbles in it. For example 100 is to be represented as 01100100 in a byte (or 8 bits). The two nibbles are (0110) and (0100). If we swap the two nibbles, we get 01000110 which is 70 in decimal. ''' from utils import Util number = int(input("Enter a number to swap value: ")) swapping_object = Util() swapped_number = swapping_object.swap_nibble(number) print(swapped_number)
def __make_saliency_map(self, srcs): util = Util() srcs = list(map(util.normalize, [srcs[key] for key in srcs.keys()])) # 将各个属性下的显著图等比例相加 return srcs[0] / 3. + srcs[1] / 3. + srcs[2] / 3.
""" file_splice = FileSplitAndSplicing(path) file_splice.run() def run_mix(path): """ 新旧文章的混合拼接程序 """ file_splice = File_processing5(path) file_splice.run() if __name__ == '__main__': while True: util = Util() read_path = r'./data/read_path' # IDE执行 # read_path = r'../data/read_path' # 打包执行 folders = util.get_file_dir(read_path) for folder in folders: print("-" * 50) print(folder) print("-" * 50) folder_path = read_path + "/" + folder if "_a_" in folder: run_original(folder_path) else: run_mix(folder_path) while True:
''' 6. Factors Desc -> Computes the prime factorization of N using brute force. I/P -> Number to find the prime factors Logic -> Traverse till i*i <= N instead of i <= N for efficiency . O/P -> Print the prime factors of number N. ''' from utils import Util number = int(input("Enter an integer: ")) prime_object = Util() prime = prime_object.prime_factors(number) print(prime)