Пример #1
0
        public ClassResult(string data)
        {
            ResultType = ResultType.Class;
            Time       = Convert.ToDateTime(data.Replace("time: ", "~").Split('~')[1].Split(',')[0]);
            string d1 = data.Replace("data: ", "~").Split('~')[1];

            if (d1.Contains("class: Abstract"))
            {
                Classification = WordClassification.Abstract;
            }
            else if (d1.Contains("class: Physical"))
            {
                Classification = WordClassification.Physical;
            }
            string d2 = d1.Replace("value: ", "~").Split('~')[1].Split(',')[0];

            try
            {
                Value = Double.Parse(d2);
            } catch { }
            string d3 = d1.Replace("word: ", "~").Split('~')[1].Split(',')[0];

            Word = d3;
            int d4 = Int32.Parse(d1.Replace("line: ", "~").Split('~')[1].Split('}')[0]);

            Line = d4;
        }
Пример #2
0
 // ----------------------- SetWord ---------------------------------
 /// <summary>
 /// Set the value of the word and its position in the string.
 /// </summary>
 /// <param name="InWord"></param>
 /// <param name="InWordBx"></param>
 /// <returns></returns>
 public WordCursor SetWord(
     string InWord, WordClassification InClass, int InWordBx, char InBraceChar)
 {
     mWordValue       = new WordValue(InWord, InClass, mTraits, InBraceChar);
     mWordBx          = InWordBx;
     RelativePosition = AcRelativePosition.At;
     return(this);
 }
Пример #3
0
 // ----------------------- SetWord ---------------------------------
 /// <summary>
 /// Set the value of the word and its position in the string.
 /// </summary>
 /// <param name="InWord"></param>
 /// <param name="InWordBx"></param>
 /// <returns></returns>
 public WordCursor SetWord(
     string InWordText, WordClassification InWordClass, int InWordBx, char InBraceChar)
 {
     mWord         = new TextWord(InWordText, InWordClass, mTraits, InBraceChar);
     mWordBx       = InWordBx;
     this.Position = RelativePosition.At;
     return(this);
 }
Пример #4
0
        public TextWord(string Value, WordClassification WordClass, TextTraits Traits)
        {
            char?braceChar = null;

            if (WordClass.IsOpenBraced())
            {
                braceChar = Value[0];
                if (Traits.IsOpenBraceChar(braceChar.Value) == false)
                {
                    throw new ApplicationException("not an open brace char");
                }
            }

            CommonConstruct(Value, WordClass, Traits, braceChar);
        }
Пример #5
0
 public static bool IsOpenBraced(this WordClassification WordClass)
 {
     if (WordClass == WordClassification.OpenContentBraced)
     {
         return(true);
     }
     else if (WordClass == WordClassification.OpenNamedBraced)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #6
0
        public string ToValuePresentationString()
        {
            string s1 = null;

            if (WordClassification == WordClassification.None)
            {
                s1 = WordClassification.ToString();
            }
            else
            {
                s1 =
                    " wc: " + WordClassification.ToString() +
                    " wBx: " + WordBx.ToString( ) +
                    " wv: " + Word.ToString();
            }
            return(s1);
        }
Пример #7
0
        private void CommonConstruct(
            string InValue, WordClassification InClass, TextTraits InTraits, char InBraceChar)
        {
            mValue     = InValue;
            mClass     = InClass;
            mTraits    = InTraits;
            mBraceChar = InBraceChar;

            // depending on word class, brace char must be specified or be blanks.
            if ((mClass == WordClassification.Braced) ||
                (mClass == WordClassification.NameBraced))
            {
                if (mBraceChar == ' ')
                {
                    throw(new ApplicationException("Brace character of braced word is missing"));
                }
            }
        }
Пример #8
0
        // ----------------------- SetValue ---------------------------------
        /// <summary>
        /// Set the value of the word and its position in the string.
        /// </summary>
        /// <param name="InWord"></param>
        /// <param name="InWordBx"></param>
        /// <returns></returns>
        private void SetValue(
            string InValue, WordClassification InClass, char InBraceChar)
        {
            mValue     = InValue;
            mClass     = InClass;
            mBraceChar = ' ';

            // brace character.
            if (mClass == WordClassification.Braced)
            {
                mBraceChar = mValue[0];
            }
            else if (InClass == WordClassification.NameBraced)
            {
                if (InBraceChar == ' ')
                {
                    throw(new ApplicationException("brace char of NameBraced word is missing"));
                }
                mBraceChar = InBraceChar;
            }
        }
Пример #9
0
        public TextWord(string Text, LiteralType LiteralType, TextTraits Traits)
        {
            mValue       = Text;
            _LiteralType = LiteralType;

            if (this.LiteralType.IsNumeric( ))
            {
                mClass = WordClassification.Numeric;
            }
            else if (this.LiteralType.IsQuoted( ))
            {
                mClass = WordClassification.Quoted;
            }
            else
            {
                throw new ApplicationException(
                          "LiteralType is not supported. " + LiteralType.ToString());
            }

            mTraits    = Traits;
            _BraceChar = null;
        }
Пример #10
0
        // ----------------------- SetValue ---------------------------------
        /// <summary>
        /// Set the value of the word and its position in the string.
        /// </summary>
        /// <param name="InWord"></param>
        /// <param name="InWordBx"></param>
        /// <returns></returns>
        private void SetValue(
            string InValue, WordClassification InClass, char?BraceChar)
        {
            mValue     = InValue;
            mClass     = InClass;
            _BraceChar = null;

            // brace character.
            if ((mClass == WordClassification.ContentBraced) ||
                (mClass == WordClassification.OpenContentBraced))
            {
                _BraceChar = mValue[0];
            }
            else if ((InClass == WordClassification.NamedBraced) ||
                     (mClass == WordClassification.OpenNamedBraced))
            {
                if (BraceChar == null)
                {
                    throw (new ApplicationException("brace char of NameBraced word is missing"));
                }
                _BraceChar = BraceChar;
            }
        }
Пример #11
0
        private static void RunCommand(string cmd)
        {
            cmd = cmd.ToLower();
            if (cmd.StartsWith("run! "))
            {
                cmd = cmd.Replace("run! ", "");
                if (TrainedNetwork != null)
                {
                    Console.WriteLine("\t" + TrainedNetwork.Compute(RootBreaker.BreakWord(cmd).SignificantRoots(2).GetVals())[0]);
                    Console.WriteLine("");
                }
            }
            else if (cmd.StartsWith("graph "))
            {
                cmd = cmd.Replace("graph ", "");
                var                        lineCount = File.ReadAllLines(cmd).Length;
                StreamReader               file      = new StreamReader(cmd);
                string                     line      = "";
                List <LogResult>           results   = new List <LogResult>();
                int                        maxLine   = 0;
                Dictionary <int, double[]> initial   = new Dictionary <int, double[]>();
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("type: new-word"))
                    {
                        NewWordResult res = new NewWordResult(line);
                        results.Add(res);
                    }
                    else if (line.Contains("type: abstraction"))
                    {
                        ClassResult res = new ClassResult(line);
                        if (initial.ContainsKey(res.Line))
                        {
                            initial[res.Line] = new double[] { initial[res.Line][0] + res.Value, initial[res.Line][1] + 1 };
                        }
                        else
                        {
                            initial.Add(res.Line, new double[] { res.Value, 1 });
                        }
                        results.Add(res);
                    }
                }
                double[] classResults = new double[20000];
                foreach (KeyValuePair <int, double[]> e in initial)
                {
                    classResults[e.Key] = (e.Value[0] / e.Value[1]);
                }
                for (int i = 0; i < classResults.Length; i++)
                {
                    Console.WriteLine(classResults[i]);
                }
            }
            else if (cmd.StartsWith("read "))
            {
                cmd = cmd.Replace("read ", "");
                StreamReader     file      = new StreamReader(cmd);
                string           line      = "";
                Queue <string>   readQueue = new Queue <string>();
                Queue <Sentence> sentences = new Queue <Sentence>();
                int lineNum = 0;
                while ((line = file.ReadLine()) != null)
                {
                    // remove not needed words
                    line = line.Replace(" the ", " ").Replace(" an ", " ").Replace(" a ", " ");
                    string[] words = line.Split(' ');

                    int i = 0;
                    for (i = 0; i < words.Length; i++)
                    {
                        Word w = new Word(words[i]);
                        // check abstraction only for nouns
                        if (w.WordType == WordType.Noun)
                        {
                            double             d  = GlobalNetworks.GetRootsNetwork().Compute(RootBreaker.BreakWord(w.Isolation).SignificantRoots(2).GetVals())[0];
                            WordClassification cl = d > 0.5 ? WordClassification.Abstract : WordClassification.Physical;
                            GlobalCache.LogAction("abstraction", $"class: {cl}, value: {d}, word: {w.Isolation}, line: {lineNum}");
                        }
                    }
                    i = 0;
                    while (i < words.Length)
                    {
                        string word = words[i];
                        readQueue.Enqueue(word);
                        if (word.Contains(".") || word.Contains("?") || word.Contains("!"))
                        {
                            // empty queue into sentance
                            string all = "";
                            while (readQueue.Count > 0)
                            {
                                all += " " + readQueue.Dequeue();
                            }
                            all = all.Trim();
                            Sentence s = new Sentence(all);
                            Console.WriteLine("\n\t > " + s.Raw);
                            sentences.Enqueue(s);
                            if (sentences.Count > 50)
                            {
                                Sentence        sen  = sentences.Dequeue();
                                List <Sentence> sens = SentenceBreaker.BreakSentence(sen);
                                // pass sentances into context analyzer
                            }
                        }
                        i++;
                    }
                    lineNum++;
                }

                file.Close();
            }
            else if (cmd.StartsWith("train "))
            {
                cmd = cmd.Replace("train ", "");
                if (cmd.StartsWith("root from "))
                {
                    cmd = cmd.Replace("root from ", "");
                    string[] args = cmd.Split(' ');
                    if (args.Length > 1)
                    {
                        string input  = args[0];
                        string output = args[1];
                        Console.WriteLine(">> training roots\n");
                        ActivationNetwork an = Analyzer.RunRoots(input, output);
                        Console.WriteLine(">> finished training roots\n");
                        TrainedNetwork = an;
                    }
                }
            }
            else if (cmd.StartsWith("test "))
            {
                cmd = cmd.Replace("test ", "");
                if (cmd.StartsWith("sen "))
                {
                    cmd = cmd.Replace("sen ", "");
                    List <List <Sentence> > s = SentenceBreaker.BreakString(cmd);
                    foreach (List <Sentence> list in s)
                    {
                        Console.WriteLine("\n\t> " + list);
                        foreach (Sentence sen in list)
                        {
                            Console.WriteLine($"\t - {sen.Raw}");
                        }
                    }
                }
                else if (cmd.StartsWith("syn "))
                {
                    cmd = cmd.Replace("syn ", "");
                    Word w = new Word(cmd);
                    w.LoadSynonyms();
                }
                else if (cmd == "backpropogation" || cmd == "bp")
                {
                    double[][] input = new double[4][] {
                        new double[] { 0, 0 }, new double[] { 0, 1 },
                        new double[] { 1, 0 }, new double[] { 1, 1 }
                    };
                    double[][] output = new double[4][] {
                        new double[] { 0 }, new double[] { 1 },
                        new double[] { 1 }, new double[] { 0 }
                    };

                    Console.WriteLine(">> testing back-propogation\n");
                    ActivationNetwork net = Neural.RunBackPropogation(input, output, 6);
                    Console.WriteLine($"\n>> test results:\n" +
                                      $"\n[0,0]\t{net.Compute(new double[] { 0, 0 })[0]}" +
                                      $"\n[0,1]\t{net.Compute(new double[] { 0, 1 })[0]}" +
                                      $"\n[1,0]\t{net.Compute(new double[] { 1, 0 })[0]}" +
                                      $"\n[1,1]\t{net.Compute(new double[] { 1, 1 })[0]}");
                }
            }
        }
Пример #12
0
        // -------------------- IsolateWord ---------------------------
        // We have a word starting at InBx. Scan to the end of the word.
        // Returns the word in the InOutResults parm.
        // Returns the word delim in the return argument.
        private static ScanPatternResults IsolateWord(
            string Text,
            int Bx,
            ref WordCursor Results,
            TextTraits Traits)
        {
            int bx;
            ScanPatternResults spr = null;

            bx = Bx;
            char ch1 = Text[bx];

            // is start of either verbatim string literal or quoted literal.
            if (
                ((Traits.VerbatimLiteralPattern != null) &&
                 (Traits.VerbatimLiteralPattern.Match(Text, bx))) ||
                (Traits.IsQuoteChar(ch1) == true)
                )
            {
                var rv = ScanWord.IsolateQuotedWord(Text, bx, Traits);
                bx = rv.Item1;
                int?               ex       = rv.Item2;
                string             wordText = rv.Item3;
                WordClassification wc       = WordClassification.Quoted;
                var                litType  = rv.Item4;
                spr = rv.Item5;
                Results.SetWord(wordText, wc, bx);
                Results.Word.LiteralType = litType;
            }

            else
            {
                // Scan the string for any of the non word patterns spcfd in Traits.
                DelimClassification sprdc = DelimClassification.None;
                int remLx = Text.Length - bx;
                spr = Scanner.ScanEqualAny(Text, bx, remLx, Traits.NonWordPatterns);
                if (spr.IsNotFound == false)
                {
                    sprdc = spr.FoundPat.DelimClassification;
                }

                // a quote character within the name.  this is an error.
                if (sprdc == DelimClassification.Quote)
                {
                    throw new ApplicationException(
                              "quote character immed follows name character at position " +
                              spr.FoundPos.ToString() + " in " + Text);
                }

                // no delim found. all word to the end of the string.
                else if (spr.IsNotFound)
                {
                    string wordText = Text.Substring(Bx);
                    Results.SetWord(wordText, WordClassification.Identifier, Bx);
                }

                // found an open named brace char
                // Open named braced words are words that combine the word and the braced contents.
                // debateable that this feature is needed and should be retained.
                else if (sprdc == DelimClassification.OpenNamedBraced)
                {
                    Scanner.ScanWord_IsolateWord_Braced(
                        Text, bx, spr, ref Results, Traits);
                }

                // delim is same position as the word.  so either the word is the delim ( an
                // expression symbol ) or the word is empty ( the delim is a comma, semicolon,
                // ... a content divider )
                else if (spr.FoundPos == Bx)
                {
                    if ((Traits.NonDividerIsWord == true) &&
                        (Traits.IsDividerDelim(spr.FoundPat.DelimClassification) == false))
                    {
                        Results.SetWord(
                            spr.FoundPat.PatternValue,
                            spr.FoundPat.DelimClassification.ToWordClassification( ).Value,
                            Bx,
                            spr.FoundPat.LeadChar);
                    }

                    // start of CommentToEnd comment. This is a word, not a delim. Find the
                    // end of the comment and set the delim to that end position.
                    else if (sprdc == DelimClassification.CommentToEnd)
                    {
                        spr = Scanner.ScanWord_IsolateWord_CommentToEnd(
                            Text, spr.FoundPos, ref Results, Traits);
                    }

                    else
                    {
                        Results.SetNullWord();
                    }
                }

                // we have a word that ends with a delim.
                else
                {
                    int    lx       = spr.FoundPos - Bx;
                    string wordText = Text.Substring(Bx, lx);
                    Results.SetWord(wordText, WordClassification.Identifier, Bx);
                }
            }

            // return ScanPatternResults of the delim that ends the word.
            return(spr);
        }
Пример #13
0
 // ----------------------- SetWord ---------------------------------
 /// <summary>
 /// Set the value of the word and its position in the string.
 /// </summary>
 /// <param name="InWord"></param>
 /// <param name="InWordBx"></param>
 /// <returns></returns>
 public WordCursor SetWord(string InWord, WordClassification InClass, int InWordBx)
 {
     return(SetWord(InWord, InClass, InWordBx, ' '));
 }
Пример #14
0
 public WordValue(
     string InValue, WordClassification InClass, TextTraits InTraits, char InBraceChar)
 {
     CommonConstruct(InValue, InClass, InTraits, InBraceChar);
 }
Пример #15
0
 public WordValue(string InValue, WordClassification InClass, TextTraits InTraits)
 {
     CommonConstruct(InValue, InClass, InTraits, ' ');
 }
Пример #16
0
 public TextWord(
     string InValue, WordClassification InWordClass, TextTraits InTraits,
     char?BraceChar)
 {
     CommonConstruct(InValue, InWordClass, InTraits, BraceChar);
 }
Пример #17
0
        /// <summary>
        /// Examine the string for its WordClassification content.
        /// </summary>
        /// <param name="InWord"></param>
        /// <param name="InTraits"></param>
        /// <returns></returns>
        public static CharObjectPair CalcWordClassification(
            string InWord, TextTraits InTraits)
        {
            int Fx = 0, Ix = 0;
            WordClassification wc = WordClassification.None;
            char braceChar        = ' ';
            char ch1              = AcCommon.PullChar(InWord, 0);
            int  Ex               = InWord.Length - 1;

            // is quoted. the word runs to the closing quote.
            if (Scanner.IsOpenQuoteChar(ch1) == true)
            {
                Ix = Scanner.ScanCloseQuote(InWord, 0, InTraits.QuoteEncapsulation);
                if (Ix == Ex)
                {
                    wc = WordClassification.Quoted;
                }
                else
                {
                    wc = WordClassification.MixedText;
                }
            }

            // check if the string is a Braced or NameBraced word.
            if (wc == WordClassification.None)
            {
                char[] combo = AcCommon.Concat(InTraits.DelimChars, InTraits.BraceChars);
                Scanner.ScanCharResults results = Scanner.ScanEqual(InWord, 0, combo);
                Fx  = results.ResultPos;
                ch1 = results.ResultChar;

                // found a brace char
                if ((InTraits.IsOpenBraceChar(ch1) == true) &&
                    (InTraits.IsDelimChar(ch1) == false))
                {
                    Ix = Scanner.ScanCloseBrace(InWord, Fx);
                    if (Ix == Ex)
                    {
                        braceChar = ch1;
                        if (Fx == 0)
                        {
                            wc = WordClassification.Braced;
                        }
                        else
                        {
                            wc = WordClassification.NameBraced;
                        }
                    }
                }
            }

            // word is all delimeter.
            if (wc == WordClassification.None)
            {
                Fx = Scanner.ScanNotEqual(InWord, 0, InTraits.DelimChars).a;
                if (Fx >= 0)
                {
                    wc = WordClassification.Delimeter;
                }
            }

            // check if a numeric string.
            if (wc == WordClassification.None)
            {
                char[] digitChars =
                    new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-' };
                Fx = Scanner.ScanNotEqual(InWord, 0, digitChars).a;
                if (Fx == -1)
                {
                    wc = WordClassification.Numeric;
                    try
                    {
                        double vx = double.Parse(InWord);
                    }
                    catch (Exception)
                    {
                        wc = WordClassification.None;
                    }
                }
            }

            // any delim chars in the string.  if not, the string is a name.  otherwise, it is
            // mixed.
            if (wc == WordClassification.None)
            {
                Fx = Scanner.ScanEqual(InWord, 0, InTraits.DelimChars).a;
                if (Fx == -1)
                {
                    wc = WordClassification.Name;
                }
                else
                {
                    wc = WordClassification.MixedText;
                }
            }

            return(new CharObjectPair(braceChar, wc));
        }
Пример #18
0
        public Word(string word)
        {
            WordClassification = WordClassification.None;
            Literal            = word;
            word      = word.Replace(":", "").Replace("?", "").Replace("_", "").Replace("!", "").Replace(".", "").ToLower().Replace("\"", "").Replace("\\", "").Replace("/", "").Replace(",", "");
            Isolation = word;
            if (GlobalCache.WordCache.ContainsKey(Isolation))
            {
                Word w = GlobalCache.WordCache[Isolation];
                WordType = w.WordType;
            }
            else
            {
                try
                {
                    string   w  = WebInteractive.GetPageContents(@"https://www.dictionary.com/browse/" + word);
                    string[] bk = w.Split(new string[] { "<span class=\"luna-pos\">" }, StringSplitOptions.None);
                    if (bk.Length < 2)
                    {
                        WordType = WordType.None;
                        return;
                    }
                    string type = bk[1].Split(new string[] { "</span>" }, StringSplitOptions.None)[0].Replace(",", "").Replace(";", "").ToLower();
                    switch (type)
                    {
                    case "definite article":
                        WordType = WordType.DefiniteArticle;
                        break;

                    case "noun adjective":
                    case "noun":
                        WordType = WordType.Noun;
                        break;

                    case "verb (used without object)":
                    case "verb (used with object)":
                    case "auxiliary verb":
                    case "verb":
                        WordType = WordType.Verb;
                        break;

                    case "interjection":
                        WordType = WordType.Interjection;
                        break;

                    case "adjective":
                        WordType = WordType.Adjective;
                        break;

                    case "adverb":
                        WordType = WordType.Adverb;
                        break;

                    case "pronoun":
                        WordType = WordType.Pronoun;
                        break;

                    case "preposition":
                        WordType = WordType.Preposition;
                        break;

                    case "conjunction":
                    case "connective":
                        WordType = WordType.Adjective;
                        break;

                    default:
                        WordType = WordType.None;
                        break;
                    }
                    if (WordType == WordType.None)
                    {
                        Console.WriteLine(Isolation + " - " + type);
                    }
                    GlobalCache.WordCache.Add(Isolation, this);
                    GlobalCache.LogAction("new-word", $"word: {Isolation}, type: {WordType}");
                    //Console.WriteLine($"\tcached: [{Isolation}, {WordType}]");
                }
                catch
                {
                }
            }
        }