Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        public static List <ArabicWord> ArabicWords;        //قائمة بالكلمات العربية ضمن النص المعالج
        //public static List<List<Interpretation>> AllWordsInterpretations = new List<List<Interpretation>>();

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