Esempio n. 1
0
        public void GetNumberOfNERDetections()
        {
            // Load the sample text to analyze
            IntPtr ptr      = new IntPtr();
            var    sentence = MarshaledWrapper.Exec_mitie_load_entire_file(SAMPLE_TEXT_FILE_NAME,
                                                                           ref ptr);

            // Tokenize the text
            IntPtr tokens = new IntPtr();

            MarshaledWrapper.Exec_mitie_tokenize(sentence, ref tokens, false);

            // Load the sample *trained* ner model for Mitie
            IntPtr ner = MarshaledWrapper.Exec_mitie_load_named_entity_extractor(SAMPLE_MITIE_NER_MODEL);

            // Calculate the detections from  the model
            IntPtr detections    = MarshaledWrapper.Exec_mitie_extract_entities(ner, tokens);
            uint   numDetections = MarshaledWrapper.Exec_mitie_ner_get_num_detections(detections);

            Assert.True(numDetections == 23);

            // Free unmanaged memory
            MarshaledWrapper.Exec_mitie_free(tokens);
            MarshaledWrapper.Exec_mitie_free(ner);
            MarshaledWrapper.Exec_mitie_free(detections);
        }
Esempio n. 2
0
        /// <summary>
        /// Extracts a string List from the char** returned from Mitie.
        /// </summary>
        /// <param name="charArrayPtr">The character array PTR.</param>
        /// <returns></returns>
        public static List <string> IntPtrToStringArrayAnsi(ref IntPtr charArrayPtr,
                                                            bool freeUnmanaged = false)
        {
            var    tokenList = new List <string>();
            IntPtr tempPtr   = charArrayPtr;

            while (true)
            {
                var stringPtr     = (IntPtr)Marshal.PtrToStructure(tempPtr, typeof(IntPtr));
                var managedString = Marshal.PtrToStringAnsi(stringPtr);

                if (!string.IsNullOrEmpty(managedString))
                {
                    tokenList.Add(managedString);

                    tempPtr = IntPtr.Add(tempPtr, IntPtr.Size);

                    // Note: Another way to increment pointer. Above is likely faster:
                    // charArrayPtr = new IntPtr(charArrayPtr.ToInt64() + IntPtr.Size);
                }
                else
                {
                    break;
                }
            }

            if (freeUnmanaged == true)
            {
                MarshaledWrapper.Exec_mitie_free(charArrayPtr);
                charArrayPtr = IntPtr.Zero;
            }

            return(tokenList);
        }
Esempio n. 3
0
        public void LoadNERModel()
        {
            // Load the sample text to analyze
            IntPtr ptr      = new IntPtr();
            var    sentence = MarshaledWrapper.Exec_mitie_load_entire_file(SAMPLE_TEXT_FILE_NAME,
                                                                           ref ptr);

            IntPtr tokens = new IntPtr();

            MarshaledWrapper.Exec_mitie_tokenize(sentence, ref tokens, false);

            IntPtr ner = MarshaledWrapper.Exec_mitie_load_named_entity_extractor(SAMPLE_MITIE_NER_MODEL);

            Assert.NotNull((object)ner);

            // Free unmanaged memory
            MarshaledWrapper.Exec_mitie_free(tokens);
            MarshaledWrapper.Exec_mitie_free(ner);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            IntPtr ptr = new IntPtr();

            // Load a sentence from a sample file
            var sentence = MarshaledWrapper.Exec_mitie_load_entire_file(@"..\..\..\..\sample1.txt",
                                                                        ref ptr);

            Console.WriteLine($"Loaded sentence -> {sentence}\n");

            IntPtr      tokens           = new IntPtr();
            IntPtr      offsetTokens     = new IntPtr();
            List <uint> listOffsetTokens = new List <uint>();

            // Gte the tokens and token offsets
            var listTokens = MarshaledWrapper.Exec_mitie_tokenize_with_offsets(sentence,
                                                                               ref listOffsetTokens,
                                                                               ref tokens,
                                                                               ref offsetTokens,
                                                                               false);

            foreach (var token in listTokens)
            {
                Console.WriteLine($"Token -> {token}");
            }

            Console.WriteLine("\n");
            // Load the sample *trained* model
            IntPtr entityExtractto = MarshaledWrapper.Exec_mitie_load_named_entity_extractor(@"..\..\..\..\ner_model.dat");

            // Get the mumber of detections
            IntPtr detections    = MarshaledWrapper.Exec_mitie_extract_entities(entityExtractto, tokens);
            long   numDetections = MarshaledWrapper.Exec_mitie_ner_get_num_detections(detections);

            Console.WriteLine($"Number of detections -> {numDetections}");

            // Free the unmanaged memory
            MarshaledWrapper.Exec_mitie_free(tokens);
            MarshaledWrapper.Exec_mitie_free(offsetTokens);
            MarshaledWrapper.Exec_mitie_free(entityExtractto);
            MarshaledWrapper.Exec_mitie_free(detections);
        }
Esempio n. 5
0
        /// <summary>
        /// Marshals the long array to uint list.
        /// </summary>
        /// <param name="ptr">The PTR.</param>
        /// <param name="length">The length.</param>
        /// <param name="freeUnmanaged">if set to <c>true</c> [free unmanaged].</param>
        /// <returns></returns>
        public static List <uint> MarshalLongArrayToUintList(ref IntPtr ptr,
                                                             uint length,
                                                             bool freeUnmanaged = false)
        {
            List <uint> list    = new List <uint>();
            IntPtr      tempPtr = ptr;

            for (int i = 0; i < length; i++)
            {
                list.Add((uint)Marshal.PtrToStructure(tempPtr, typeof(uint)));
                tempPtr = IntPtr.Add(tempPtr, IntPtr.Size);
            }

            if (freeUnmanaged == true)
            {
                MarshaledWrapper.Exec_mitie_free(ptr);
                ptr = IntPtr.Zero;
            }

            return(list);
        }
Esempio n. 6
0
        public void GetTokenOffsets()
        {
            // Load the sample text to analyze
            IntPtr ptr      = new IntPtr();
            var    sentence = MarshaledWrapper.Exec_mitie_load_entire_file(SAMPLE_TEXT_FILE_NAME,
                                                                           ref ptr);

            IntPtr      tokens           = new IntPtr();
            IntPtr      offsetTokens     = new IntPtr();
            List <uint> listOffsetTokens = new List <uint>();

            // Tokenize with offsets
            MarshaledWrapper.Exec_mitie_tokenize_with_offsets(sentence,
                                                              ref listOffsetTokens,
                                                              ref tokens,
                                                              ref offsetTokens);

            Assert.True(listOffsetTokens.Count == 212);

            // Free unmanaged memory
            MarshaledWrapper.Exec_mitie_free(tokens);
            MarshaledWrapper.Exec_mitie_free(offsetTokens);
        }