Попробуйте кэшировать объект стоп-слов, как показано ниже. Создание этого при каждом вызове функции кажется узким местом.
from nltk.corpus import stopwords
cachedStopWords = stopwords.words("english")
def testFuncOld():
text = 'hello bye the the hi'
text = ' '.join([word for word in text.split() if word not in stopwords.words("english")])
def testFuncNew():
text = 'hello bye the the hi'
text = ' '.join([word for word in text.split() if word not in cachedStopWords])
if __name__ == "__main__":
for i in xrange(10000):
testFuncOld()
testFuncNew()
Я запустил это через профилировщик: python -m cProfile -s cumulative test.py . Соответствующие строки размещены ниже.
Совокупное время nCalls
10000 7,723 words.py:7(testFuncOld)
10000 0,140 words.py:11(testFuncNew)
Итак, кэшируем экземпляр стоп-слов дает ускорение в ~ 70 раз.
Используя тип = "файл" и принимает = "изображение /*" (или формат, который Вы хотите), позвольте пользователю, выбрал файл с определенным форматом. Но у Вас есть к проверке ре он снова в стороне клиента, потому что пользователь может выбрать другой тип файлов. Это работает на меня.
<input #imageInput accept="image/*" (change)="processFile(imageInput)" name="upload-photo" type="file" id="upload-photo" />
И затем, в Вашем сценарии
processFile(imageInput) {
if (imageInput.files[0]) {
const file: File = imageInput.files[0];
var pattern = /image-*/;
if (!file.type.match(pattern)) {
alert('Invalid format');
return;
}
// here you can do whatever you want with your image. Now you are sure that it is an image
}
}
JavaScript