Example #1
0
        public static void ApplyGrammarRelation(GrammarRelation SuperRelation)
        {
            GrammarRelation GR;

            foreach (int WordIndex in SuperRelation.Elements.Keys)
            {
                GR = SuperRelation.Elements[WordIndex] as GrammarRelation;
                if (GR != null)
                {
                    ApplyGrammarRelation(GR);
                }
                else
                {
                    string         Temp;
                    Interpretation Interp = SuperRelation.Elements[WordIndex];
                    for (int i = 0; i < Analyzer.AllWordsInfo[WordIndex].Count; i++)
                    {
                        if (Interpreter.CompareMeanings(Analyzer.AllWordsInfo[WordIndex][i].Meaning, Interp.Meaning, out Temp))
                        {
                            Interpretation NewInterp = new Interpretation
                            {
                                Meaning = Temp, Description = Interp.Description, SuperRelation = Interp.SuperRelation
                            };
                            if (Analyzer.AllWordsInfo[WordIndex][i].Interpretations == null)
                            {
                                Analyzer.AllWordsInfo[WordIndex][i].Interpretations = new List <Interpretation>();
                            }
                            Analyzer.AllWordsInfo[WordIndex][i].Interpretations.Add(NewInterp);
                        }
                    }
                }
            }
        }
Example #2
0
        public void SendActivationSignal()
        {
            GrammarRelation GR = this;

            while (GR.SuperRelation != null)
            {
                GR = GR.SuperRelation;
            }
            GR.ActivateRelation();
        }
Example #3
0
        public static GrammarRelation CloneRelation(GrammarRelation GR)
        {
            GrammarRelation NewGR = new GrammarRelation();

            foreach (var Key in GR.Elements.Keys)
            {
                NewGR.Elements.Add(Key, GR.Elements[Key]);
            }
            return(NewGR);
        }
Example #4
0
        public static List <GrammarRelation> StartInterpreting(int StartIndex, string ExpectedMeaning, int MaximumRecursion, bool RecursiveCall)
        {
            bool ValidRule = true;
            List <GrammarRelation> AllRelations = new List <GrammarRelation>();

            foreach (string[] Rule in GrammarRelation.GrammarRules)
            {
                string RuleMeaning;
                if (!CompareMeanings(ExpectedMeaning, Rule[0], out RuleMeaning))//القاعدة لا تحقق المعنى المطلوب
                {
                    continue;
                }
                List <GrammarRelation> Relations = new List <GrammarRelation>();
                Relations.Add(new GrammarRelation());
                string[] expression = Rule[1].Split('+');//فصل مكونات علاقة القاعدة النحوية
                string   ElementMeaning, ElementInterpretation = "";
                string[] InterpretFound = new string[2];
                ValidRule = true;

                for (int Element = 0; Element < expression.Length; Element++) //بدء مطابقة القاعدة
                {
                    string[] ElementRootRule = new string[2];
                    if (expression[Element].Contains(':'))
                    {
                        //فصل المعنى عن الإعراب
                        ElementMeaning = expression[Element].Substring(0, expression[Element].IndexOf(':')).Trim();
                        if (ElementMeaning.Contains('['))
                        {
                            ElementRootRule = ElementMeaning.Substring(ElementMeaning.IndexOf('[')).Trim('[', ']').Split(',');
                            ElementMeaning  = ElementMeaning.Substring(0, ElementMeaning.IndexOf('['));
                        }
                        ElementInterpretation = expression[Element].Substring(expression[Element].IndexOf(':') + 1).Trim();
                    }
                    else
                    {
                        ElementMeaning = expression[Element].Trim();//حرف من الحروف ليس له إعراب
                    }
                    if (ElementMeaning[0] != 'T')
                    {
                        if (!Interpretation.Interpretations.ContainsKey(ElementInterpretation) && ElementMeaning[0] != 'T')//إذا كان الإعراب المخزن وصفه غير موجود
                        {
                            //خطأ في قاعدة البيانات
                            ValidRule = false;
                            break;
                        }
                        InterpretFound = Interpretation.Interpretations[ElementInterpretation];//البحث عن معنى رمز الإعراب
                    }
                    int Count = Relations.Count;
                    for (int GRelation = 0; GRelation < Count; GRelation++)
                    {
                        int Offset = Relations[GRelation].WordsCovered;
                        if (Relations.Count == 0 || Analyzer.AllWordsInfo[StartIndex + Offset][0].Word == "EOS")
                        {
                            Relations.RemoveAt(GRelation);
                            Count--;
                            GRelation--;
                            continue;
                        }
                        string InterpretedMeaning;

                        if (ElementRootRule[0] != null)//هل هناك شروط اشتقاق لهذه الكلمة
                        {
                            bool Found = false;
                            foreach (var item in Analyzer.AllWordsInfo[StartIndex + Offset])
                            {
                                if (CompareMeanings(item.Meaning, ElementMeaning, out InterpretedMeaning))
                                {
                                    if (item.Root.Root == ElementRootRule[0] && item.Root.DerivationRules.Contains(ElementRootRule[1]))
                                    {
                                        Interpretation  NewInterpret = new Interpretation(); //إعراب جديد
                                        GrammarRelation NewRelation  = CloneRelation(Relations[GRelation]);
                                        NewInterpret.Description = InterpretFound[0];        //نحميل وصف الإعراب
                                        NewInterpret.Meaning     = ElementMeaning;
                                        NewRelation.Elements.Add(StartIndex + Offset, NewInterpret);
                                        NewInterpret.SuperRelation = NewRelation;
                                        Relations.Add(NewRelation);
                                        Found = true;
                                        break;
                                    }
                                }
                            }
                            if (!Found)
                            {
                                Relations.RemoveAt(GRelation);
                                Count--;
                                GRelation--;
                            }
                            continue;
                        }

                        List <string> WordMeanings = GetPossibleMeanings(Analyzer.AllWordsInfo[StartIndex + Offset]);

                        for (int Possibility = 0; Possibility < WordMeanings.Count; Possibility++)
                        {
                            bool ValidMeaning = CompareMeanings(WordMeanings[Possibility], ElementMeaning, out InterpretedMeaning);
                            if (ElementMeaning[0] == 'T')// إذا كان العنصر حرفا صالحا
                            {
                                if (ValidMeaning)
                                {
                                    Interpretation  NewInterpret = new Interpretation();//إعراب جديد
                                    GrammarRelation NewRelation  = CloneRelation(Relations[GRelation]);
                                    NewInterpret.Meaning = ElementMeaning;
                                    NewRelation.Elements.Add(StartIndex + Offset, NewInterpret);
                                    NewInterpret.SuperRelation = NewRelation;
                                    Relations.Add(NewRelation);
                                }
                                continue;
                            }

                            if (ValidMeaning)
                            {
                                Interpretation  NewInterpret = new Interpretation(); //إعراب جديد
                                GrammarRelation NewRelation  = CloneRelation(Relations[GRelation]);
                                NewInterpret.Description = InterpretFound[0];        //نحميل وصف الإعراب
                                NewInterpret.Meaning     = ElementMeaning;
                                NewRelation.Elements.Add(StartIndex + Offset, NewInterpret);
                                NewInterpret.SuperRelation = NewRelation;
                                Relations.Add(NewRelation);
                            }
                        }

                        if (MaximumRecursion > 0 && Element > 0 && !Relations[GRelation].Elements.ContainsKey(-1))
                        {
                            //ابحث عن مجموعة كلمات تحقق المعنى المطلوب
                            List <GrammarRelation> SubRelations = new List <GrammarRelation>();
                            SubRelations = StartInterpreting(StartIndex + Offset, ElementMeaning, MaximumRecursion - 1, true);
                            if (SubRelations.Count > 0)
                            {
                                //إذا نجح البحث عن مجموعة كلمات
                                for (int R = 0; R < SubRelations.Count; R++)
                                {
                                    GrammarRelation NewSubRelation = CloneRelation(Relations[GRelation]);
                                    SubRelations[R].Description   = InterpretFound[0];
                                    SubRelations[R].Meaning       = ElementMeaning;
                                    SubRelations[R].SuperRelation = NewSubRelation;
                                    NewSubRelation.Elements.Add(-1, SubRelations[R]);
                                    Relations.Add(NewSubRelation);
                                }
                            }
                        }

                        Relations.RemoveAt(GRelation);
                        Count--;
                        GRelation--;
                    }
                    if (!ValidRule)
                    {
                        break;
                    }
                }
                if (ValidRule)
                {
                    foreach (GrammarRelation R in Relations)
                    {
                        AllRelations.Add(R);
                    }
                }
            }
            return(AllRelations);
        }
Example #5
0
        public static string AnalyzeText(string TextToParse, ref BackgroundWorker BackGroundProcess)
        {
            //TextToParse = Tashkeel.Remove(TextToParse);
            Analyzer.TextToParse = TextToParse;
            Corrections          = 0;
            NotRecognized        = 0;
            ArabicWords          = ExtractArabicWords(TextToParse, out Sentences);
            //للاحتفاظ باحتمالات النطق المختلفة لكل كلمة لأغراض التعديل
            AllWordsInfo = new List <List <WordInfo> >();
            con.Open();
            LoadInterpreterData();
            for (int i = 0; i < ArabicWords.Count; i++) //التحليل الصرفي
            {
                if (ArabicWords[i].word == "EOS")       //نهاية جملة
                {
                    List <WordInfo> EOS = new List <WordInfo>();
                    EOS.Add(new WordInfo {
                        Word = "EOS"
                    });
                    AllWordsInfo.Add(EOS);
                    continue;
                }
                List <WordInfo> NewWordInfo = ProcessWord(ArabicWords[i]);                        //ابدأ معالجة الكلمة الجديدة
                AllWordsInfo.Add(NewWordInfo);                                                    //أضف ناتج المعالجة لمعلومات الكلمات
                if (NewWordInfo.Count == 0)                                                       //إذا لم يجد تفسير للكلمة
                {
                    NewWordInfo = Morphology.LookForForignWord(ArabicWords[i].word, NewWordInfo); //ابحث في الكلمات الأعجمية المخزنة
                    if (NewWordInfo.Count == 0)
                    {
                        //فشل تفسير الكلمة والتشكيل بناء على تتابع الحروف -غالبا للكلمات الأعجمية
                        //هذه الخوارزمية تحتاج تحسين
                        //للتعامل مع الكلمات الأعجمية المضاف لها ألف ولام أو ياء نسب
                        WordInfo NewWord = new WordInfo();
                        NewWord.Word       = ArabicWords[i].word;
                        NewWord.Diacritics = Tashkeel.Guess(ArabicWords[i].word);
                        NewWord.Meaning    = "N";
                        NewWordInfo.Add(NewWord);
                        NotRecognized += 1;
                    }
                }
                BackGroundProcess.ReportProgress((i + 1) * 100 / ArabicWords.Count);//تحديث شريط التقدم
            }

            for (int i = 0; i < ArabicWords.Count; i++)
            {
                int Longest = 0, Index = 0;
                List <GrammarRelation> PossibleRelations = Interpreter.StartInterpreting(i, "", 1, false);
                for (int PR = 0; PR < PossibleRelations.Count; PR++)
                {
                    if (Longest < PossibleRelations[PR].WordsCovered)
                    {
                        Longest = PossibleRelations[PR].WordsCovered;
                        Index   = PR;
                    }
                    //Interpreter.ApplyGrammarRelation(PossibleRelations[PR]);
                }
                i += Longest;
                if (PossibleRelations.Count > 0)
                {
                    GrammarRelation.ApplyGrammarRelation(PossibleRelations[Index]);
                    PossibleRelations[Index].ActivateRelation();
                }
            }
            con.Close();
            return(GenerateOutput());
        }
Example #6
0
        public static List <ArabicWord> ArabicWords;        //قائمة بالكلمات العربية ضمن النص المعالج
        //public static List<List<Interpretation>> AllWordsInterpretations = new List<List<Interpretation>>();

        public static void LoadInterpreterData()
        {
            Interpretation.InitializeInterpretations();
            GrammarRelation.InitializeRules();
        }