Esempio n. 1
0
        // *** PUBLIC ***

        // CONSTRUCTOR
        /// <summary>
        /// Initializes DawgCreator, the only way of initializing
        /// </summary>
        /// <param name="lexiconName">Name of the lexicon</param>
        /// <param name="numNodes">Number of nodes (after compression typically)</param>
        /// <param name="numWords">Number of full words in the Dawg</param>
        public DawgCreator(string lexiconName, uint numNodes, uint numWords, uint numReversePartWords)
        {
            // initialization
            CreateHeader(lexiconName, numNodes, numWords, numReversePartWords);
            nodes         = new DawgNode[numNodes];
            numAddedNodes = 0;
        }
Esempio n. 2
0
        // ADD NODE
        /// <summary>
        /// Adds the given node to DAWG. Please note the nodes need to be added sequentially!
        /// </summary>
        /// <param name="dawgNode">Node to be added</param>
        public void AddNode(DawgNode dawgNode)
        {
            // validations
            Debug.Assert(numAddedNodes < header.NumNodes);

            nodes[numAddedNodes++] = dawgNode;
        }
Esempio n. 3
0
        private const uint MINIMUM_NUMBER_OF_NODES  = 3; // Root, forward and reverse

        // *** PUBLIC ***
        // CONSTRUCTOR
        public Dawg(string fileName)
        {
            // open the file
            using (BinaryReader reader = new BinaryReader(File.Open(fileName, FileMode.Open)))
            {
                // read the header
                header.VersionNumber       = reader.ReadUInt32();
                header.NumNodes            = reader.ReadUInt32();
                header.LexiconName         = reader.ReadString();
                header.LexiconDate         = reader.ReadString();
                header.NumWords            = reader.ReadUInt32();
                header.NumReversePartWords = reader.ReadUInt32();

                // validations
                if (header.VersionNumber != 1U)
                {
                    string exceptionMessage = string.Format(
                        "Unxpected Dawg header version: {0}. Only Version 1 is supported!",
                        header.VersionNumber);
                    throw new InvalidDataException(exceptionMessage);
                }

                if (header.NumNodes < Dawg.MINIMUM_NUMBER_OF_NODES)
                {
                    string exceptionMessage = string.Format(
                        "Number of nodes specified in Dawg header: {0} is less than the minimum number of nodes: {1}!",
                        header.NumNodes, Dawg.MINIMUM_NUMBER_OF_NODES);
                    throw new InvalidDataException(exceptionMessage);
                }

                // read the nodes
                nodes = new DawgNode[header.NumNodes];
                for (uint idx = 0; idx < header.NumNodes; idx++)
                {
                    nodes[idx].ChildNodeIdx = reader.ReadUInt32();
                    nodes[idx].Letter       = reader.ReadByte();
                    nodes[idx].IsTerminal   = reader.ReadBoolean();
                    nodes[idx].IsLastChild  = reader.ReadBoolean();
                }
            }

            // validate the counts
            uint numCountedWords = CountNumWords();

            if (header.NumWords != numCountedWords)
            {
                string exceptionMessage = string.Format(
                    "Number of words specified in Dawg header: {0} is less than the actual number of words: {1}!",
                    header.NumWords, numCountedWords);
                throw new InvalidDataException(exceptionMessage);
            }

            uint numCountedReversePartWords = CountNumReversePartWords();

            if (header.NumReversePartWords != numCountedReversePartWords)
            {
                string exceptionMessage = string.Format(
                    "Number of words specified in Dawg header: {0} is less than the actual number of words: {1}!",
                    header.NumReversePartWords, numCountedReversePartWords);
                throw new InvalidDataException(exceptionMessage);
            }
        }