當前位置: 首頁>>代碼示例>>Python>>正文


Python spacy.language方法代碼示例

本文整理匯總了Python中spacy.language方法的典型用法代碼示例。如果您正苦於以下問題:Python spacy.language方法的具體用法?Python spacy.language怎麽用?Python spacy.language使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在spacy的用法示例。


在下文中一共展示了spacy.language方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: model_installed

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def model_installed(name: str) -> bool:
        """Check if spaCy language model is installed.

        From https://github.com/explosion/spaCy/blob/master/spacy/util.py

        :param name:
        :return:
        """
        data_path = util.get_data_path()
        if not data_path or not data_path.exists():
            raise IOError(f"Can't find spaCy data path: {data_path}")
        if name in {d.name for d in data_path.iterdir()}:
            return True
        if is_package(name):  # installed as package
            return True
        if Path(name).exists():  # path to model data directory
            return True
        return False 
開發者ID:HazyResearch,項目名稱:fonduer,代碼行數:20,代碼來源:spacy_parser.py

示例2: _load_lang_model

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def _load_lang_model(self) -> None:
        """Load spaCy language model.

        If a model is not installed, download it before loading it.

        Currenty supported spaCy languages

        en English (50MB)
        de German (645MB)
        fr French (1.33GB)
        es Spanish (377MB)

        :return:
        """
        if self.lang in self.languages:
            if not SpacyParser.model_installed(self.lang):
                download(self.lang)
            model = spacy.load(self.lang)
        elif self.lang in self.alpha_languages:
            language_module = importlib.import_module(f"spacy.lang.{self.lang}")
            language_method = getattr(language_module, self.alpha_languages[self.lang])
            model = language_method()
        self.model = model 
開發者ID:HazyResearch,項目名稱:fonduer,代碼行數:25,代碼來源:spacy_parser.py

示例3: load_nlp

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def load_nlp(vectors_loc, lang=None):
    if lang is None:
        nlp = Language()
    else:
        # create empty language class – this is required if you're planning to
        # save the model to disk and load it back later (models always need a
        # "lang" setting). Use 'xx' for blank multi-language class.
        nlp = spacy.blank(lang)
    with open(vectors_loc, 'rb') as file_:
        header = file_.readline()
        nr_row, nr_dim = header.split()
        nlp.vocab.reset_vectors(width=int(nr_dim))
        for line in file_:
            line = line.rstrip().decode('utf8')
            pieces = line.rsplit(' ', int(nr_dim))
            word = pieces[0]
            vector = numpy.asarray([float(v) for v in pieces[1:]], dtype='f')
            nlp.vocab.set_vector(word, vector)  # add the vectors to the vocab
    return nlp 
開發者ID:sonvx,項目名稱:word2vecVN,代碼行數:21,代碼來源:spacy-fastext.py

示例4: create

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def create(cls,
               component_config: Dict[Text, Any],
               config: RasaNLUModelConfig) -> 'SpacyNLP':
        import spacy

        component_config = override_defaults(cls.defaults, component_config)

        spacy_model_name = component_config.get("model")

        # if no model is specified, we fall back to the language string
        if not spacy_model_name:
            spacy_model_name = config.language
            component_config["model"] = config.language

        logger.info("Trying to load spacy model with "
                    "name '{}'".format(spacy_model_name))

        nlp = spacy.load(spacy_model_name, disable=['parser'])
        cls.ensure_proper_language_model(nlp)
        return cls(component_config, nlp) 
開發者ID:weizhenzhao,項目名稱:rasa_nlu,代碼行數:22,代碼來源:spacy_utils.py

示例5: ensure_proper_language_model

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def ensure_proper_language_model(nlp: Optional['Language']) -> None:
        """Checks if the spacy language model is properly loaded.

        Raises an exception if the model is invalid."""

        if nlp is None:
            raise Exception("Failed to load spacy language model. "
                            "Loading the model returned 'None'.")
        if nlp.path is None:
            # Spacy sets the path to `None` if
            # it did not load the model from disk.
            # In this case `nlp` is an unusable stub.
            raise Exception("Failed to load spacy language model for "
                            "lang '{}'. Make sure you have downloaded the "
                            "correct model (https://spacy.io/docs/usage/)."
                            "".format(nlp.lang)) 
開發者ID:weizhenzhao,項目名稱:rasa_nlu,代碼行數:18,代碼來源:spacy_utils.py

示例6: __call__

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def __call__(self, doc: Doc, language: Language = None, stop_on_first_profane_word: Optional[bool] = None) -> Doc:
        self.register_extensions(exist_ok=True)
        if language is None:
            language = self._language
        if stop_on_first_profane_word is None:
            stop_on_first_profane_word = self._stop_on_first_profane_word
        i = 0
        while i < len(doc):
            j = i + 1
            while (j < len(doc)
                   and not doc[j - 1].whitespace_ and not doc[j - 1].is_space and not doc[j - 1].is_punct
                   and not doc[j].is_space and not doc[j].is_punct):
                j += 1
            span = self._censor_spaceless_span(doc[i:j], language=language)
            if stop_on_first_profane_word and span._.is_profane:
                break
            i += len(span)
        return doc 
開發者ID:rominf,項目名稱:profanity-filter,代碼行數:20,代碼來源:spacy_component.py

示例7: _censor

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def _censor(self, text: str, return_bool=False) -> Union[str, bool]:
        """:return: text with any profane words censored or bool (True - text has profane words, False otherwise) if
        return_bool=True"""
        result = ''
        text_parts = self._split_by_language(text=text)
        for language, text_part in text_parts:
            result_part = text_part
            doc = self._parse(language=language, text=text_part)
            for token in doc:
                if token._.is_profane:
                    if return_bool:
                        return True
                    else:
                        result_part = self._replace_token(text=result_part, old=token, new=token._.censored)
            result += result_part
        if return_bool:
            return False
        else:
            return result 
開發者ID:rominf,項目名稱:profanity-filter,代碼行數:21,代碼來源:profanity_filter.py

示例8: create

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def create(
        cls, component_config: Dict[Text, Any], config: RasaNLUModelConfig
    ) -> "SpacyNLP":

        component_config = override_defaults(cls.defaults, component_config)

        spacy_model_name = component_config.get("model")

        # if no model is specified, we fall back to the language string
        if not spacy_model_name:
            spacy_model_name = config.language
            component_config["model"] = config.language

        logger.info(f"Trying to load spacy model with name '{spacy_model_name}'")

        nlp = cls.load_model(spacy_model_name)

        cls.ensure_proper_language_model(nlp)
        return cls(component_config, nlp) 
開發者ID:botfront,項目名稱:rasa-for-botfront,代碼行數:21,代碼來源:spacy_utils.py

示例9: ensure_proper_language_model

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def ensure_proper_language_model(nlp: Optional["Language"]) -> None:
        """Checks if the spacy language model is properly loaded.

        Raises an exception if the model is invalid."""

        if nlp is None:
            raise Exception(
                "Failed to load spacy language model. "
                "Loading the model returned 'None'."
            )
        if nlp.path is None:
            # Spacy sets the path to `None` if
            # it did not load the model from disk.
            # In this case `nlp` is an unusable stub.
            raise Exception(
                "Failed to load spacy language model for "
                "lang '{}'. Make sure you have downloaded the "
                "correct model (https://spacy.io/docs/usage/)."
                "".format(nlp.lang)
            ) 
開發者ID:botfront,項目名稱:rasa-for-botfront,代碼行數:22,代碼來源:spacy_utils.py

示例10: create

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def create(cls, cfg):
        # type: (RasaNLUModelConfig) -> SpacyNLP
        import spacy

        component_conf = cfg.for_component(cls.name, cls.defaults)
        spacy_model_name = component_conf.get("model")

        # if no model is specified, we fall back to the language string
        if not spacy_model_name:
            spacy_model_name = cfg.language
            component_conf["model"] = cfg.language

        logger.info("Trying to load spacy model with "
                    "name '{}'".format(spacy_model_name))

        nlp = spacy.load(spacy_model_name, parser=False)
        cls.ensure_proper_language_model(nlp)
        return SpacyNLP(component_conf, nlp) 
開發者ID:crownpku,項目名稱:Rasa_NLU_Chi,代碼行數:20,代碼來源:spacy_utils.py

示例11: ensure_proper_language_model

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def ensure_proper_language_model(nlp):
        # type: (Optional[Language]) -> None
        """Checks if the spacy language model is properly loaded.

        Raises an exception if the model is invalid."""

        if nlp is None:
            raise Exception("Failed to load spacy language model. "
                            "Loading the model returned 'None'.")
        if nlp.path is None:
            # Spacy sets the path to `None` if
            # it did not load the model from disk.
            # In this case `nlp` is an unusable stub.
            raise Exception("Failed to load spacy language model for "
                            "lang '{}'. Make sure you have downloaded the "
                            "correct model (https://spacy.io/docs/usage/)."
                            "".format(nlp.lang)) 
開發者ID:crownpku,項目名稱:Rasa_NLU_Chi,代碼行數:19,代碼來源:spacy_utils.py

示例12: test_neg_spacy_processor

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def test_neg_spacy_processor(self):
        spacy = Pipeline[DataPack]()
        spacy.set_reader(StringReader())

        config = {
            "processors": 'ner',
            "lang": "xx_ent_wiki_sm",
            # Language code for the language to build the Pipeline
            "use_gpu": False
        }
        spacy.add(SpacyProcessor(), config=config)
        spacy.initialize()

        sentences = ["This tool is called Forte.",
                     "The goal of this project to help you build NLP "
                     "pipelines.",
                     "NLP has never been made this easy before."]
        document = ' '.join(sentences)
        with self.assertRaises(ProcessExecutionException):
            _ = spacy.process(document) 
開發者ID:asyml,項目名稱:forte,代碼行數:22,代碼來源:spacy_processors_test.py

示例13: __init__

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def __init__(
        self, nlp: Language, input_id_col: str = "id", input_text_col: str = "text"
    ):
        """Initialize the SpacyExtractor pipeline.
        
        nlp (spacy.language.Language): pre-loaded spacy language model
        input_text_col (str): property on each document to run the model on
        input_id_col (str): property on each document to correlate with request

        RETURNS (EntityRecognizer): The newly constructed object.
        """
        self.nlp = nlp
        self.input_id_col = input_id_col
        self.input_text_col = input_text_col 
開發者ID:microsoft,項目名稱:cookiecutter-spacy-fastapi,代碼行數:16,代碼來源:spacy_extractor.py

示例14: __init__

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def __init__(self, vocab: Vocab) -> None:
        """Initialize a custom tokenizer.

        :param vocab: The vocab attribute of the respective spacy language object.
        """
        self.vocab = vocab 
開發者ID:HazyResearch,項目名稱:fonduer,代碼行數:8,代碼來源:spacy_parser.py

示例15: cache_key

# 需要導入模塊: import spacy [as 別名]
# 或者: from spacy import language [as 別名]
def cache_key(cls,
                  component_meta: Dict[Text, Any],
                  model_metadata: 'Metadata') -> Optional[Text]:

        # Fallback, use the language name, e.g. "en",
        # as the model name if no explicit name is defined
        spacy_model_name = component_meta.get("model", model_metadata.language)

        return cls.name + "-" + spacy_model_name 
開發者ID:weizhenzhao,項目名稱:rasa_nlu,代碼行數:11,代碼來源:spacy_utils.py


注:本文中的spacy.language方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。