如何限制Spacy使用的CPU数量?
我想从一大组句子中提取词性和命名实体.由于RAM的限制,我首先使用Python NLTK将我的文档解析成句子.然后我迭代我的句子并使用nlp.pipe()进行提取.然而,当我这样做时,Spacy消耗了我的整个计算机; Spacy使用每个可用的CPU.这样做并不好,因为我的电脑是共享的.如何限制Spacy使用的CPU数量?这是我迄今为止的代码:
# require
from nltk import *
import spacy
# initialize
file = './walden.txt'
nlp = spacy.load( 'en' )
# slurp up the given file
handle = open( file, 'r' )
text = handle.read()
# parse the text into sentences, and process each one
sentences = sent_tokenize( text )
for sentence in nlp.pipe( sentences, n_threads=1 ) :
# process each token
for token in sentence : print( "\t".join( [ token.text, token.lemma_, token.tag_ ] ) )
# done …Run Code Online (Sandbox Code Playgroud) spacy ×1