Python 什么';使用多种语言时,加载spaCy语言模型的最有效方法是什么?

Python 什么';使用多种语言时,加载spaCy语言模型的最有效方法是什么?,python,twitter,spacy,ner,Python,Twitter,Spacy,Ner,我正在做一个项目,从推文中提取实体,任何一条推文都可能是多种语言中的一种。我已经编写了一个函数,它将Twitter的tweet语言作为参数,并返回相应的spaCy模型的名称 def getSpacyModel(langCode): switcher = { 'da': "da_core_news_lg", 'de': "de_core_news_lg", 'en': "en_core_web

我正在做一个项目,从推文中提取实体,任何一条推文都可能是多种语言中的一种。我已经编写了一个函数,它将Twitter的tweet语言作为参数,并返回相应的spaCy模型的名称

def getSpacyModel(langCode):
    switcher = {
        'da': "da_core_news_lg",
        'de': "de_core_news_lg",
        'en': "en_core_web_trf",
        'es': "es_core_news_md",
        'fr': "fr_core_news_lg",
        'ja': "ja_core_news_lg",
        'nb': "nb_core_news_lg",
        'nl': "nl_core_news_lg",
        'pl': "pl_core_news_md",
        'pt': "pt_core_news_md",
        'ro': "ro_core_news_md",
        'ru': "ru_core_news_sm",
        'tr': "", # Turkish must be built from scratch
        'zh': "zh_core_web_trf"
    }
    return switcher.get(langCode, "Unknown")
我的想法是这样做:

model = getSpacyModel(tweet.lang) # Generate model name based on language
nlp = spacy.load(model) # Load the appropriate spaCy model
text = tweet.full_text # Grab the text of the tweet
doc = nlp(text) # Transform the text into a spaCy doc
ent_dict = get_ent_dict(doc) # User defined function to create a dictionary of entities
过去,我曾在一个简单的for循环中处理过被诅咒的tweet:

for tweet in tweets:
    model = getSpacyModel(tweet.lang) # Generate model name based on language
    nlp = spacy.load(model) # Load the appropriate spaCy model
    text = tweet.full_text # Grab the text of the tweet
    doc = nlp(text) # Transform the text into a spaCy doc
    ent_dict = get_ent_dict(doc) # User defined function to create a dictionary of entities
    # Do whatever I need to do with the entities afterward
但这似乎是这个项目的一个问题,因为我需要为每一条tweet加载一个新模型,并且一次处理多达200条tweet的批处理。这些型号的内存在40mb到500mb之间,具体取决于语言,因此它可能会大大降低速度


我考虑将它们分类,例如,处理所有英语tweet,然后处理所有西班牙语tweet等等。我们在一个批次中只讨论了200条推文(尽管批次在一天中会被拉多次),因此排序循环的额外时间可能不会增加大量的时间。但是我想知道是否有更优雅的方法来处理这个问题。

假设您不能同时加载所有模型(可能通过使用多台机器),我认为没有任何方法可以改进这个问题。