This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def prior_probabilities(classes): | |
| """Compute prior probabilities for all class labels.""" | |
| dic = {} | |
| freq = FreqDist(classes) | |
| for k, v in freq.items(): | |
| prior = freq[k] / len(classes) | |
| dic[k] = round(prior, 3) | |
| return dic |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def shuffle_split(documents, labels, split): | |
| """Shuffle data to ensure random class distribution in train/test split.""" | |
| tuples = [[doc, label] for doc, label in zip(documents, labels)] | |
| random.shuffle(tuples) | |
| X, Y = zip(*tuples) | |
| split_point = int(split*len(X)) | |
| Xtrain = X[:split_point] | |
| Ytrain = Y[:split_point] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def tokenize_pos(tokens): | |
| """Add POS-tags to each token.""" | |
| return [token+"_POS-"+tag for token, tag in nltk.pos_tag(tokens)] | |
| class LengthFeatures(BaseEstimator, TransformerMixin): | |
| """Feature engineer the length of each feature.""" | |
| def fit(self, x, y=None): | |
| return self |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def tabular_results(Xtest, Ytest, Yguess, prior_prob, posterior_prob): | |
| """Return table with classification results.""" | |
| maximum_array = posterior_prob.max(axis=1) | |
| df = pd.DataFrame(columns=['sentence', 'true_label', 'prior_probabilities', 'predicted_label', 'posterior_probabilities']) | |
| df['sentence'] = Xtest | |
| df['true_label'] = Ytest | |
| for index, row in df.iterrows(): | |
| row['prior_probabilities'] = prior_prob[row['true_label']] | |
| df['predicted_label'] = Yguess |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def baseline_classifier(Xtest, Ytest): | |
| """Baseline by randomly assigning a label to each document.""" | |
| label_list = [label for label in set(Ytest)] # random.choice cannot index a set, so needs to be a list | |
| baseline = [random.choice(label_list) for sent in Xtest] | |
| return baseline |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def main(): | |
| X, Y = read_corpus(args.input, args.binary) | |
| Xtrain, Xtest, Ytrain, Ytest = shuffle_split(X, Y, 0.8) | |
| prior_prob = prior_probabilities(Y) | |
| info('Prior probabilities per class: {0}'.format(prior_prob)) | |
| classifier = feature_union(count=True, tfidf=True, textstats=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @require_http_methods(["GET"]) | |
| @login_required(login_url='/login', redirect_field_name='') | |
| def index(request): | |
| # Verbose user permission authentication! | |
| groups = [group.name for group in request.user.groups.all()] | |
| if not any(x in groups for x in ['PERMISSION_GR_1', 'PERMISSION_GR_2']): | |
| messages.add_message(request, messages.WARNING, '{0} does not have permission.'.format(request.user)) | |
| return redirect('home') | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import wraps | |
| from django.contrib import messages | |
| from django.contrib.auth.views import redirect_to_login | |
| from django.contrib.auth import REDIRECT_FIELD_NAME | |
| from django.shortcuts import resolve_url | |
| from urllib.parse import urlparse | |
| from [projectname].settings import config | |
| import [projectname].settings as settings | |
| import requests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def superuser_required(view_func=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='/login', message=default_message): | |
| """ | |
| Decorator for views that checks that the user is logged in and is a | |
| superuser, displaying message if provided. | |
| """ | |
| actual_decorator = user_passes_test( | |
| lambda u: u.is_active and u.is_superuser and u.is_authenticated, | |
| login_url=login_url, | |
| redirect_field_name=redirect_field_name, | |
| message=message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from decorators import staff_required, superser_required | |
| @login_required(login_url='/login', redirect_field_name='') | |
| @staff_required(login_url='/', redirect_field_name='', message='You are not authorised to view this page.') | |
| def staff_required_page(request): | |
| // Do something meaningful | |
| @require_http_methods(["POST"]) | |
| @login_required(login_url='/login', redirect_field_name='') |