Exemplo n.º 1
0
        public bool Read(Stream stream, MatchData matchData)
        {
            if (data == null)
             {
            data = new PrepNotes();
             }

             var buffer = String.Empty;
             PrepNode foundPrep = null;
             var fFound = false;
             var matchPos = stream.Position;
             int curByte;

             while ((curByte = stream.ReadByte()) >= 0)
             {
            buffer += (char) curByte;

            //Prep tokens can have leading commas or parens - so trim these off
            buffer = Regex.Replace(buffer, @"^\s*(,|-|\()\s*", "");
            buffer = Regex.Replace(buffer, @"\s*\)\s*$", "");

            PrepNode node;
            var match = data.Parse(buffer, out node);
            if (match == MatchPrecision.None)
            {
               break; //No reason to continue reading stream, let's see what we have..
            }

            if (match == MatchPrecision.Exact)
            {
               matchPos = stream.Position;
               foundPrep = node;
               fFound = true;
            }
             }

             if (foundPrep != null) //Add the prep at the end of the loop in case we found multiple preps along the way
             {
            matchData.Preps.Add(foundPrep);
             }

             stream.Seek(matchPos, SeekOrigin.Begin);
             return fFound;
        }
Exemplo n.º 2
0
        public bool Read(Stream stream, MatchData matchData)
        {
            if (data == null)
            {
                data = new PrepNotes();
            }

            var buffer = string.Empty;
            PrepNode foundPrep = null;
            var matchFound = false;
            var matchPos = stream.Position;
            int curByte;

            while ((curByte = stream.ReadByte()) >= 0)
            {
                buffer += (char)curByte;

                //Prep tokens can have leading commas or parens - so trim these off
                buffer = Regex.Replace(buffer, @"^\s*(,|-|\()\s*", "");
                buffer = Regex.Replace(buffer, @"\s*\)\s*$", "");

                PrepNode node;
                var match = data.Parse(buffer, out node);
                if (match == MatchPrecision.None)
                {
                    break;
                }

                if (match == MatchPrecision.Exact)
                {
                    matchPos = stream.Position;
                    foundPrep = node;
                    matchFound = true;
                }
            }

            if (foundPrep != null)
            {
                matchData.Preps.Add(foundPrep);
            }

            stream.Seek(matchPos, SeekOrigin.Begin);
            return matchFound;
        }
Exemplo n.º 3
0
        public bool Read(Stream stream, MatchData matchData)
        {
            if (data == null)
            {
                data = new PrepNotes();
            }

            var      buffer    = String.Empty;
            PrepNode foundPrep = null;
            var      fFound    = false;
            var      matchPos  = stream.Position;
            int      curByte;

            while ((curByte = stream.ReadByte()) >= 0)
            {
                buffer += (char)curByte;

                //Prep tokens can have leading commas or parens - so trim these off
                buffer = Regex.Replace(buffer, @"^\s*(,|-|\()\s*", "");
                buffer = Regex.Replace(buffer, @"\s*\)\s*$", "");

                PrepNode node;
                var      match = data.Parse(buffer, out node);
                if (match == MatchPrecision.None)
                {
                    break; //No reason to continue reading stream, let's see what we have..
                }

                if (match == MatchPrecision.Exact)
                {
                    matchPos  = stream.Position;
                    foundPrep = node;
                    fFound    = true;
                }
            }

            if (foundPrep != null) //Add the prep at the end of the loop in case we found multiple preps along the way
            {
                matchData.Preps.Add(foundPrep);
            }

            stream.Seek(matchPos, SeekOrigin.Begin);
            return(fFound);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes the context and loads necessary data into memory through the configured database adapter.
        /// This must be done before the context is usable.
        /// </summary>
        public virtual void Initialize()
        {
            if (Adapter == null)
            {
                throw new InvalidConfigurationException("DBContext requires a configured database adapter.  Please check your configuration.");
            }

            // Initiaze NHibernate session
            Adapter.Initialize(this);

            new Thread(delegate()
            {
                using (InitLock.WriteLock())
                {
                    // Initialize ingredient parser
                    ingParser           = new IngredientParser();
                    var ingredientIndex = Adapter.LoadIngredientsForIndex();
                    ingParser.CreateIndex(ingredientIndex);

                    // Initialize modeler
                    modeler = new ModelerProxy(this);
                    modeler.LoadSnapshot();

                    // Initialize natural language parsing
                    IngredientSynonyms.InitIndex(Adapter.IngredientLoader);
                    UnitSynonyms.InitIndex(Adapter.UnitLoader);
                    FormSynonyms.InitIndex(Adapter.FormLoader);
                    PrepNotes.InitIndex(Adapter.PrepLoader);
                    Anomalies.InitIndex(Adapter.AnomalyLoader);
                    NumericVocab.InitIndex();

                    parser = new Parser();
                    LoadTemplates();
                }
            }).Start();

            Thread.Sleep(500); // Provides time for initialize thread to start and acquire InitLock
        }
Exemplo n.º 5
0
        public void Setup()
        {
            //Initialize all the maps
            Trace.Write("Initializing NLP Grammar maps... ");

            IngredientSynonyms.InitIndex(new TestIngredientLoader());
            UnitSynonyms.InitIndex(new TestUnitLoader());
            FormSynonyms.InitIndex(new TestFormLoader());
            PrepNotes.InitIndex(new TestPrepLoader());
            NumericVocab.InitIndex();

            //Load parse templates
            parser = new Parser();
            Trace.Write("Loading parse templates... ");

            parser.LoadTemplates(
                "[ING]: [AMT] [UNIT]",                  //cheddar cheese: 5 cups
                "[AMT] [UNIT] [FORM] [ING]",            //5 cups melted cheddar cheese
                "[AMT] [UNIT] [ING]",                   //5 cups cheddar cheese
                "[AMT] [UNIT] of [ING]",                //5 cups of cheddar cheese
                "[AMT] [UNIT] of [FORM] [ING]",         //two cups of shredded cheddar cheese
                "[AMT] [ING]",                          //5 eggs
                "[ING]: [AMT]",                         //eggs: 5
                "[FORM] [ING]: [AMT]",                  //shredded cheddar cheese: 1 cup
                "[FORM] [ING]: [AMT] [UNIT]",           //shredded cheddar cheese: 1 cup

                "[ING]: [AMT] [UNIT], [PREP]",          //cheddar cheese: 5 cups
                "[AMT] [UNIT] [FORM] [ING], [PREP]",    //5 cups melted cheddar cheese
                "[AMT] [UNIT] [ING], [PREP]",           //5 cups cheddar cheese
                "[AMT] [UNIT] of [ING], [PREP]",        //5 cups of cheddar cheese
                "[AMT] [UNIT] of [FORM] [ING], [PREP]", //two cups of shredded cheddar cheese
                "[AMT] [ING], [PREP]",                  //5 eggs
                "[ING]: [AMT], [PREP]",                 //eggs: 5
                "[FORM] [ING]: [AMT], [PREP]",          //shredded cheddar cheese: 1 cup
                "[FORM] [ING]: [AMT] [UNIT], [PREP]"    //shredded cheddar cheese: 1 cup
                );
        }