当我尝试访问import sklearn.svm
import re
from sklearn import metrics
import numpy
import scipy.sparse
import datetime
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
from nltk.tokenize import word_tokenize, sent_tokenize
from sklearn.preprocessing import StandardScaler
# custom feature example
def words_capitalized(sentence):
tokens = []
# tokenize the sentence
tokens = word_tokenize(sentence)
counter = 0
for word in tokens:
if word[0].isupper():
counter += 1
return counter
# custom feature example
def words_length(sentence):
tokens = []
# tokenize the sentence
tokens = word_tokenize(sentence)
list_of_length = list()
for word in tokens:
list_of_length.append(length(word))
return list_of_length
def get_features(untagged_text, value, scaler):
# this function extracts the custom features
# transforms the vectorizer
# scales the features
# and finally stacks all of them
list_of_length = list()
list_of_capitals = list()
# transform vectorizer
X_bow = countVecWord.transform(untagged_text)
# I also see some people use X_bow = countVecWord.transform(untagged_text).todense(), what does the .todense() option do here?
for sentence in untagged_text:
list_of_urls.append([words_length(sentence)])
list_of_capitals.append([words_capitalized(sentence)])
# turn the feature output into a numpy vector
X_length = numpy.array(list_of_urls)
X_capitals = numpy.array(list_of_capitals)
if value == 1:
# fit transform for training set
X_length = = scaler.fit_transform(X_length)
X_capitals = scaler.fit_transform(X_capitals)
# if test set
else:
# transform only for test set
X_length = = scaler.transform(X_length)
X_capitals = scaler.transform(X_capitals)
# stack all features as a sparse matrix
X_two_bows = scipy.sparse.hstack((X_bow, X_length))
X_two_bows = scipy.sparse.hstack((X_two_bows , X_length))
X_two_bows = scipy.sparse.hstack((X_two_bows , X_capitals))
return X_two_bows
def fit_and_predict(train_labels, train_features, test_features, classifier):
# fit the training set
classifier.fit(train_features, train_labels)
# return the classification result
return classifier.predict(test_features)
if __name__ == '__main__':
input_sets = read_data()
X = input_sets[0]
Y = input_sets[1]
X_dev = input_sets[2]
Y_dev = input_sets[3]
# initialize the count vectorizer
countVecWord = sklearn.feature_extraction.text.CountVectorizer(ngram_range=(1, 3))
scaler= StandardScaler()
# extract features
# for training
X_total = get_features(X, 1, scaler)
# for dev set
X_total_dev = get_features(X_dev, 2, scaler)
# store labels as numpy array
y_train = numpy.asarray(Y)
y_dev = numpy.asarray(Y_dev)
# train the classifier
SVC1 = LinearSVC(C = 1.0)
y_predicted = list()
y_predicted = fit_and_predict(y_train, X_total, X_total_dev, SVC1)
print "Result for dev set"
precision, recall, f1, _ = metrics.precision_recall_fscore_support(y_dev, y_predicted)
print "Precision: ", precision, " Recall: ", recall, " F1-Score: ", f1
中的任何内容时,我会被重定向到admin/someSecureThing
。如何更改它以便重定向回/
?
路线:
admin/login
答案 0 :(得分:1)
在AuthController.php中,您可以设置:
protected $loginPath = 'admin/login';
请注意$loginPath
如果用户尝试访问受保护的路由,则不会更改用户退回的位置。这是由App \ Http \ Middleware \ Authenticate中间件的句柄方法控制的。
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('admin/login');
}
}
return $next($request);
}
如果您使用自定义AuthController,那么创建自定义中间件可能会更好。
php artisan make:middleware AdminAuthController
这将在app \ Http \ Middleware文件夹下创建一个AdminAuthController.php。
接下来我们将如上所述编辑句柄功能,但也要确保:
use Illuminate\Support\Facades\Auth;
一旦完成,我们需要在app \ Http \ Kernel.php下注册我们的新中间件
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'auth.admin' => \App\Http\Middleware\AdminAuthController::class
];
在此之后,您所要做的就是使用auth.admin
中间件而不是
对于您想要的任何路线,auth
。
您的路线将如下所示:
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'web'], function() {
Route::group(['middleware' => 'auth.admin'], function(){
/* Admin Auth */
Route::get('login', 'Auth\AuthController@getLogin');
Route::post('login', 'Auth\AuthController@postLogin');
Route::get('register', 'Auth\AuthController@getRegister');
Route::post('register', 'Auth\AuthController@postRegister');
Route::get('logout', 'Auth\AuthController@getLogout');
}
Route::group(['middleware' => 'auth'], function(){
/*Admin Dashboard Routes */
Route::get('dashboard', 'AdminController@getDashboard');
});
});