Skip to content

Instantly share code, notes, and snippets.

View bluedistro's full-sized avatar
🏠
...

romann_empyre bluedistro

🏠
...
  • United Kingdom
View GitHub Profile
@bluedistro
bluedistro / sorting.py
Created April 12, 2019 08:21
Insertion Sort and Merge Sort
def insertion_sort(array):
for i in range(1, len(array)):
data = array[i]
j = i
while j > 0 and array[j-1] > data:
array[j] = array[j - 1]
j -= 1
array[j] = data
return array
@bluedistro
bluedistro / MongoServices.java
Last active April 7, 2019 13:49
JAVA MongoDB Helper Script
/*
* A Java Script containing methods to manipulate a MongoDB
* Biney Kingsley - 07-04-2019
*
* */
package com.mongo;
import org.bson.Document;
@bluedistro
bluedistro / score_review.py
Created January 1, 2019 19:29
scoring reviews
def score_review(source=None, file_type=None):
'''
source: the text, as either a string or a list of strings
file_type: (str): indicating whether we expecting a file containing the
text data or a directory containing a list files holding the text
options: 'file' or 'dir'
'''
text_list = list()
if isinstance(source, str) and file_type is None:
text_list.append(source)
@bluedistro
bluedistro / decode_review.py
Created January 1, 2019 19:27
decode review
def decode_review(text_list):
word_index = tokenizer.word_index
sequences = tokenizer.texts_to_sequences(text_list)
data = pad_sequences(sequences, maxlen=500)
# decode the words
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
decoded_review = ' '.join([reverse_word_index.get(i, '?') for i in sequences[0]])
return decoded_review, data
@bluedistro
bluedistro / polarity_scale.py
Created January 1, 2019 19:27
Scale polarity
def review_rating(score, decoded_review):
if float(score) >= 0.9:
print('Review: {}\nSentiment: Strongly Positive\nScore: {}'.format(decoded_review, score))
elif float(score) >= 0.7 and float(score) < 0.9:
print('Review: {}\nSentiment: Positive\nScore: {}'.format(decoded_review, score))
elif float(score) >= 0.5 and float(score) < 0.7:
print('Review: {}\nSentiment: Okay\nScore: {}'.format(decoded_review, score))
else:
print('Review: {}\nSentiment: Negative\nScore: {}'.format(decoded_review, score))
print('\n\n')
@bluedistro
bluedistro / load_model_tokenizer.py
Created January 1, 2019 19:24
load model and tokenizer
tokenizer_path = 'tokenizer'
model_path = 'model'
model_file = os.path.join(model_path, 'movie_sentiment_m1.h5')
tokenizer_file = os.path.join(tokenizer_path, 'tokenizer_m1.pickle')
model = load_model(model_file)
# load tokenizer
with open(tokenizer_file, 'rb') as handle:
tokenizer = pickle.load(handle)
@bluedistro
bluedistro / save_tokenizer.py
Created January 1, 2019 19:23
save tokenizer
# save the tokenizer
with open(os.path.join(tokenizer_path, 'tokenizer_m1.pickle'), 'wb') as handle:
pk.dump(tokenizer, handle, protocol=pk.HIGHEST_PROTOCOL)
@bluedistro
bluedistro / callback.py
Created December 31, 2018 15:15
callbacks
callback_list = [
keras.callbacks.EarlyStopping(
patience=1,
monitor='acc',
),
keras.callbacks.TensorBoard(
log_dir='log_dir_m1',
histogram_freq=1,
embeddings_freq=1,
@bluedistro
bluedistro / fit.py
Created December 31, 2018 15:11
fitting model
history = model.fit(x_train, y_train, epochs=50, batch_size=128, callbacks=callback_list,
validation_data=(x_val, y_val))
@bluedistro
bluedistro / model.py
Last active December 31, 2018 15:10
model building
# model developing
text_input_layer = Input(shape=(500,))
embedding_layer = Embedding(max_words, 50)(text_input_layer)
text_layer = Conv1D(256, 3, activation='relu')(embedding_layer)
text_layer = MaxPooling1D(3)(text_layer)
text_layer = Conv1D(256, 3, activation='relu')(text_layer)
text_layer = MaxPooling1D(3)(text_layer)
text_layer = Conv1D(256, 3, activation='relu')(text_layer)
text_layer = MaxPooling1D(3)(text_layer)
text_layer = Conv1D(256, 3, activation='relu')(text_layer)