简体   繁体   English

spaCy 的 python 类型提示

[英]python type hints for spaCy

I'm struggling to set type hints for anything involving spaCy types.我正在努力为涉及 spaCy 类型的任何内容设置类型提示。

Trying to use the return types like "Language" and "Token" from the documentation of spaCy but they don't seem to work...I'm on python 3.8.5.尝试使用 spaCy 文档中的“语言”和“令牌”等返回类型,但它们似乎不起作用......我在 python 3.8.5 上。

As an example say I have this:例如说我有这个:

import spacy

def load_lang() -> what_goes_here:
   return spacy.load("en_core_web_sm")

According to the docs this "Returns Language".根据文档,这个“返回语言”。 I tried Language or spacy.Language with no luck.我尝试了 Language 或 spacy.Language 但没有运气。 I have similar problems when working with tokens for example doing something like this:我在使用令牌时遇到类似的问题,例如做这样的事情:

return [token for token in doc if not token.is_stop]

Again docs mention "Returns Token" as the return object but I can't type hint it.文档再次提到“返回令牌”作为返回 object 但我无法输入提示。 I don't want to go down the List[str] from typing as I'm assuming it's the wrong way to go.我不想 go 从打字到 List[str] 下,因为我假设这是 go 的错误方式。

Any help will be much appreciated.任何帮助都感激不尽。 Thank you in advance.先感谢您。

There should be no problem if you import Language class using:如果您使用以下命令导入Language class 应该没有问题:

from spacy.language import Language

and then use it as the type hint:然后将其用作类型提示:

def load_lang() -> Language:
   return spacy.load("en_core_web_sm")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM