private void Copy(DocBin docBin) { _docs = docBin._docs; // I'd rather copy Python object no matter the serialization mode // If set to DotNet, the variable will be initialized to null // disregarding its current value which might be a default object _pyDocBin = docBin._pyDocBin; if (Serialization.Selected == Serialization.Mode.SpacyAndDotNet) { using (Py.GIL()) { dynamic spacy = Py.Import("spacy"); dynamic pyVocab = spacy.vocab.Vocab.__call__(); dynamic pyDocs = _pyDocBin.get_docs(pyVocab); dynamic builtins = Py.Import("builtins"); dynamic listDocs = builtins.list(pyDocs); var pyCount = new PyInt(builtins.len(listDocs)); var count = pyCount.ToInt32(); for (var i = 0; i < count; i++) { dynamic pyDoc = listDocs[i]; _docs[i].PyDoc = pyDoc; _docs[i].Vocab.PyVocab = pyDoc.vocab; } } } }
public void FromBytes(byte[] bytes) { if (Serialization.Selected == Serialization.Mode.Spacy) { using (Py.GIL()) { var pyObj = ToPython.GetBytes(bytes); _pyDocBin.from_bytes(pyObj); } } else { var stream = new MemoryStream(bytes); var settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreWhitespace = true; var reader = XmlReader.Create(stream, settings); var docBin = new DocBin(); docBin.ReadXml(reader); Copy(docBin); } }
public void FromDisk(string pathFile) { if (Serialization.Selected == Serialization.Mode.Spacy) { using (Py.GIL()) { var pyPath = new PyString(pathFile); _pyDocBin.from_disk(pyPath); } } else { using var stream = new FileStream(pathFile, FileMode.Open, FileAccess.Read); var settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreWhitespace = true; var reader = XmlReader.Create(stream, settings); var docBin = new DocBin(); docBin.ReadXml(reader); Copy(docBin); } }