{"record":{"id":"2af6ebbed3f86f13","repo":"hankcs/HanLP","slug":"not-supported-2af6eb","errorCode":null,"errorMessage":"Not supported.","messagePattern":"Not supported\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"hanlp/layers/embeddings/word2vec.py","lineNumber":172,"sourceCode":"                padding.append(vocab.pad_idx)\n            word_dropout = WordDropout(self.word_dropout, vocab.unk_idx, exclude_tokens=padding)\n        else:\n            word_dropout = None\n        return Word2VecEmbeddingModule(self.field, embed, word_dropout=word_dropout, cpu=self.cpu,\n                                       second_channel=self.second_channel, num_tokens_in_trn=num_tokens_in_trn,\n                                       unk_idx=vocab.unk_idx)\n\n    def transform(self, vocabs: VocabDict = None, **kwargs) -> Optional[Callable]:\n        assert vocabs is not None\n        if self.field not in vocabs:\n            vocabs[self.field] = Vocab(pad_token=self.pad, unk_token=self.unk)\n        return super().transform(**kwargs)\n\n\nclass Word2VecDataset(TransformableDataset):\n\n    def load_file(self, filepath: str):\n        raise NotImplementedError('Not supported.')\n\n\nclass Word2VecEmbeddingComponent(TorchComponent):\n\n    def __init__(self, **kwargs) -> None:\n        \"\"\" Toy example of Word2VecEmbedding. It simply returns the embedding of a given word\n\n        Args:\n            **kwargs:\n        \"\"\"\n        super().__init__(**kwargs)\n        self._tokenizer: Trie = None\n\n    def build_dataloader(self, data: List[str], shuffle=False, device=None, logger: logging.Logger = None,\n                         doc2vec=False, batch_size=32, **kwargs) -> DataLoader:\n        dataset = Word2VecDataset([{'token': x} for x in data], transform=self._tokenize if doc2vec else self.vocabs)\n        return PadSequenceDataLoader(dataset, device=device, batch_size=batch_size)\n","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/hankcs/HanLP/blob/ddb1299bddff079e447af52ec12549c50636bfa8/hanlp/layers/embeddings/word2vec.py#L154-L190","documentation":"Word2VecDataset.load_file raises NotImplementedError because word2vec embedding components consume in-memory token lists (built in build_dataloader), not corpus files. The class only exists to carry transforms over tokens; reading training samples from a path is unsupported.","triggerScenarios":"Constructing Word2VecDataset with a file path (forcing load_file) or calling load_file directly.","commonSituations":"Attempting to train word2vec vectors from a text corpus inside HanLP; reusing the dataset class generically.","solutions":["Pass in-memory data: Word2VecDataset([{'token': x} for x in data], transform=...)","Train word2vec externally (gensim/fastText) and load vectors via the embedding config"],"exampleFix":"# before\nds = Word2VecDataset(['corpus.txt'])  # error\n# after\nds = Word2VecDataset([{'token': sent} for sent in sents], transform=vocabs)","handlingStrategy":"type-guard","validationCode":"assert isinstance(data, list), 'pass in-memory token lists to Word2VecDataset'","typeGuard":"def is_token_list(data) -> bool:\n    return isinstance(data, list) and not isinstance(data, str)","tryCatchPattern":null,"preventionTips":["Pre-tokenize corpora into lists","Train word2vec with gensim and load vectors via config"],"tags":["hanlp","word2vec","dataset","not-implemented"],"backgroundTag":"unsupported-operation","analyzedSha":"ddb1299bddff079e447af52ec12549c50636bfa8","analyzedAt":"2026-08-27T03:36:54.287Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}