public byte[] ToBytes() { if (Serialization.Selected == Serialization.Mode.Spacy) { using (Py.GIL()) { return(Interop.GetBytes(PyDoc.to_bytes())); } } else { using var stream = new MemoryStream(); var settings = new XmlWriterSettings(); settings.Indent = true; using var writer = XmlWriter.Create(stream, settings); WriteXml(writer); writer.Flush(); return(stream.ToArray()); } }
public void WriteXml(XmlWriter writer) { writer.WriteStartElement(Serialization.Prefix, "Doc", Serialization.Namespace); var serializationMode = Serialization.Selected; if (serializationMode == Serialization.Mode.SpacyAndDotNet) { using (Py.GIL()) { var pyObj = Interop.GetBytes(PyDoc.to_bytes()); var pyObjB64 = Convert.ToBase64String(pyObj); writer.WriteElementString("PyObj", Serialization.Namespace, pyObjB64); } } Debug.Assert(serializationMode != Serialization.Mode.Spacy); // Using the property is important form the members to be loaded writer.WriteElementString("Text", Serialization.Namespace, Text); writer.WriteStartElement("Vocab", Serialization.Namespace); Vocab.WriteXml(writer); writer.WriteEndElement(); writer.WriteStartElement("Tokens", Serialization.Namespace); foreach (var token in Tokens) { writer.WriteStartElement("Token", Serialization.Namespace); token.WriteXml(writer); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("Sentences", Serialization.Namespace); foreach (var sent in Sents) { writer.WriteStartElement("Sent", Serialization.Namespace); sent.WriteXml(writer); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("NounChunks", Serialization.Namespace); foreach (var nounChunk in NounChunks) { writer.WriteStartElement("NounChunk", Serialization.Namespace); nounChunk.WriteXml(writer); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteStartElement("Ents", Serialization.Namespace); foreach (var ent in Ents) { writer.WriteStartElement("Ent", Serialization.Namespace); ent.WriteXml(writer); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); }