{"record":{"id":"80a99f2cb88ce1b4","repo":"stanfordnlp/CoreNLP","slug":"wordvectors-used-to-initialize-embedding-doesn-t","errorCode":null,"errorMessage":"! wordVectors used to initialize Embedding doesn't contain any recognized form of ","messagePattern":"! wordVectors used to initialize Embedding doesn't contain any recognized form of ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/neural/Embedding.java","lineNumber":308,"sourceCode":"    this.wordVectors = wordVectors;\n    this.embeddingSize = getEmbeddingSize(wordVectors);\n  }\n\n  private static int getEmbeddingSize(Map<String, SimpleMatrix> wordVectors){\n    if (!wordVectors.containsKey(UNKNOWN_WORD)){\n      // find if there's any other unk string\n      String unkStr = \"\";\n      if (wordVectors.containsKey(\"UNK\")) { unkStr = \"UNK\"; }\n      if (wordVectors.containsKey(\"UUUNKKK\")) { unkStr = \"UUUNKKK\"; }\n      if (wordVectors.containsKey(\"UNKNOWN\")) { unkStr = \"UNKNOWN\"; }\n      if (wordVectors.containsKey(\"*UNKNOWN*\")) { unkStr = \"*UNKNOWN*\"; }\n      if (wordVectors.containsKey(\"<unk>\")) { unkStr = \"<unk>\"; }\n\n      // set UNKNOWN_WORD\n      if ( ! unkStr.isEmpty()){\n        wordVectors.put(UNKNOWN_WORD, wordVectors.get(unkStr));\n      } else {\n        throw new RuntimeException(\"! wordVectors used to initialize Embedding doesn't contain any recognized form of \" + UNKNOWN_WORD);\n      }\n    }\n\n    return wordVectors.get(UNKNOWN_WORD).getNumElements();\n  }\n\n}\n","sourceCodeStart":290,"sourceCodeEnd":316,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/neural/Embedding.java#L290-L316","documentation":"Embedding.getEmbeddingSize requires the supplied word-vector map to contain an entry for the unknown-word token. It looks for known UNK spellings (e.g. '<unk>') and, if none is present, throws this RuntimeException because the embedding's unknown-word vector cannot be resolved and its size (numElements) cannot be determined.","triggerScenarios":"Calling new Embedding(Map<String,double[]>) or setWordVectors() with a map that has neither the library's default UNKNOWN_WORD key nor '<unk>' nor any recognized UNK form.","commonSituations":"Building embeddings from a custom word2vec/GloVe map that lacks an UNK entry; trimming vocab and dropping UNK; renaming UNK to something the library doesn't recognize.","solutions":["Add an entry keyed '<unk>' (or the library's UNKNOWN_WORD) with a vector before constructing the Embedding","Use the string form constructor with a vectors file that includes an UNK line","Pre-process the map: wordVectors.put(\"<unk>\", someVector)"],"exampleFix":"// before\nEmbedding e = new Embedding(vocabVectors); // no unk key\n// after\nvocabVectors.put(\"<unk>\", new double[]{0.0, 0.0, 0.0});\nEmbedding e = new Embedding(vocabVectors);","handlingStrategy":"validation","validationCode":"if (!wordVectors.containsKey(\"<unk>\") && !wordVectors.containsKey(\"UNK\") && !wordVectors.containsKey(\"UUUNKKK\"))\n  wordVectors.put(\"<unk>\", new double[expectedDim]);","typeGuard":"boolean hasUnk = wordVectors.keySet().stream().anyMatch(k -> k.toLowerCase().contains(\"unk\"));","tryCatchPattern":"try { Embedding e = new Embedding(wordVectors); } catch (RuntimeException e) { if (e.getMessage().contains(\"doesn't contain any recognized form\")) { wordVectors.put(\"<unk>\", defaultVector()); e = new Embedding(wordVectors); } else throw e; }","preventionTips":["Always add an UNK entry when building a vocabulary map from scratch","Keep the UNK token spelling consistent with the library's UNKNOWN_WORD","Unit-test embedding construction with your exact vocab preprocessing"],"tags":["java","stanford-nlp","embeddings","unknown-word"],"backgroundTag":"missing-required-argument","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}