示例#1
0
        public static string AnalyzeText(string TextToParse, ref BackgroundWorker BackGroundProcess)
        {
            Corrections   = 0;
            NotRecognized = 0;
            ArabicWords   = ExtractArabicWords(TextToParse);
            //للاحتفاظ باحتمالات النطق المختلفة لكل كلمة لأغراض التعديل
            AllWordsInfo = new List <List <WordInfo> >();
            StringBuilder OutPutText = new StringBuilder();

            conn.Open();
            con.Open();
            LoadInterpreterData();
            for (int i = 0; i < ArabicWords.Count; i++) //التحليل الصرفي
            {
                //
                ///http://ar.wikipedia.org/wiki/%D8%B2%D8%A7%D8%A6%D8%AF%D8%A9_(%D9%84%D8%BA%D8%A9)
                List <WordInfo> NewWordInfo = ProcessWord(ArabicWords[i]); //ابدأ معالجة الكلمة الجديدة
                AllWordsInfo.Add(NewWordInfo);                             //أضف ناتج المعالجة لمعلومات الكلمات
                if (NewWordInfo.Count == 0)                                //إذا لم يجد تفسير للكلمة
                {
                    //فشل تفسير الكلمة والتشكيل بناء على تتابع الحروف -غالبا للكلمات الأعجمية
                    //هذه الخوارزمية تحتاج تحسين
                    //للتعامل مع الكلمات الأعجمية المضاف لها ألف ولام أو ياء نسب
                    WordInfo NewWord = new WordInfo();
                    NewWord.Word       = ArabicWords[i].word;
                    NewWord.Diacritics = Tashkeel.Guess(ArabicWords[i].word);
                    NewWordInfo.Add(NewWord);
                    NotRecognized += 1;
                }
                BackGroundProcess.ReportProgress((i + 1) * 100 / ArabicWords.Count);//تحديث شريط التقدم
            }

            int             LastAdded = -1;
            WordInfo        WordToAdd;
            GrammarRelation GR;

            for (int i = 0; i < ArabicWords.Count; i++)
            {
                GR = new GrammarRelation();
                i += Interpreter.StartInterpreting(ref GR, i, "", 1, false);
            }
            for (int i = 0; i < ArabicWords.Count; i++)
            {
                WordToAdd = AllWordsInfo[i][0];
                OutPutText.Append(TextToParse.Substring(LastAdded + 1, ArabicWords[i].Start - (LastAdded + 1)));
                OutPutText.Append(WordToAdd.ToString());
                LastAdded = ArabicWords[i].End; //آخر حرف أضيف هو آخر حروف الكلمة
                //إضافة الحروف غير العربية التالية إلى المخرجات كما هي
                if (i + 1 == ArabicWords.Count && LastAdded + 1 < TextToParse.Length)
                {
                    OutPutText.Append(TextToParse.Substring(LastAdded + 1));
                }
            }

            conn.Close();
            con.Close();
            return(OutPutText.ToString());
        }
        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 InterpretedMeaning = Analyzer.AllWordsInfo[WordIndex][0].Meaning;
                    string InterpretedClass   = Analyzer.AllWordsInfo[WordIndex][0].SpecialClass;
                    if (Analyzer.AllWordsInfo[WordIndex][0].Interpretations == null)
                    {
                        Analyzer.AllWordsInfo[WordIndex][0].Interpretations = new List <MyInterpretation>();
                    }
                    Analyzer.AllWordsInfo[WordIndex][0].Interpretations.Add(SuperRelation.Elements[WordIndex]);

                    string Temp;
                    for (int i = 1; i < Analyzer.AllWordsInfo[WordIndex].Count; i++)
                    {
                        if (CompareMeanings(Analyzer.AllWordsInfo[WordIndex][i].Meaning, InterpretedMeaning, out Temp) &&
                            Analyzer.AllWordsInfo[WordIndex][i].SpecialClass == InterpretedClass)
                        {
                            if (Analyzer.AllWordsInfo[WordIndex][i].Interpretations == null)
                            {
                                Analyzer.AllWordsInfo[WordIndex][i].Interpretations = new List <MyInterpretation>();
                            }
                            Analyzer.AllWordsInfo[WordIndex][i].Interpretations.Add(SuperRelation.Elements[WordIndex]);
                        }
                    }
                }
            }
        }
        public static int StartInterpreting(ref GrammarRelation Relation, int StartIndex, string ExpectedMeaning, byte MaximumRecursion, bool RecursiveCall)
        {
            int  Offset    = 0;
            bool ValidRule = true;

            foreach (string[] Rule in GrammarRelation.GrammarRules)
            {
                if (!CompareMeanings(ExpectedMeaning, Rule[0], out ExpectedMeaning))    //القاعدة لا تحقق المعنى المطلوب
                {
                    continue;
                }
                WordInfo CurrentWord = Analyzer.AllWordsInfo[StartIndex][0];

                string[] expression = Rule[1].Split('+');    //فصل مكونات علاقة القاعدة النحوية
                string   ElementMeaning, ElementInterpretation = "";
                string[] InterpretFound;
                Relation  = new GrammarRelation();
                ValidRule = true;
                for (int Element = 0; Element < expression.Length; Element++)    //بدء مطابقة القاعدة
                {
                    if (StartIndex + Element + Offset >= Analyzer.AllWordsInfo.Count)
                    {
                        ValidRule = false;
                        break;
                    }
                    if (expression[Element].Contains(':'))
                    {
                        //فصل المعنى عن الإعراب
                        ElementMeaning        = expression[Element].Substring(0, expression[Element].IndexOf(':')).Trim();
                        ElementInterpretation = expression[Element].Substring(expression[Element].IndexOf(':') + 1).Trim();
                    }
                    else
                    {
                        ElementMeaning = expression[Element].Trim();    //حرف من الحروف ليس له إعراب
                    }
                    string InterpretedMeaning;
                    bool   ValidElement = CompareMeanings(Analyzer.AllWordsInfo[StartIndex + Element + Offset][0].Meaning, ElementMeaning, out InterpretedMeaning);
                    if (CurrentWord.Interpretations != null)
                    {
                        if (Element == 0 && ValidElement)    //العنصر الرئيسي معرب مسبقا ويصلح استخدامه
                        {
                            Relation.Elements.Add(StartIndex + Element + Offset, CurrentWord.Interpretations[0]);
                            continue;
                        }
                        else
                        {
                            ValidRule = false;
                            break;
                        }
                    }

                    for (int Possibility = 0; Possibility < Analyzer.AllWordsInfo[StartIndex + Element + Offset].Count; Possibility++)
                    {
                        if (Possibility > 0)
                        {
                            ValidElement = CompareMeanings(Analyzer.AllWordsInfo[StartIndex + Element + Offset][Possibility].Meaning, ElementMeaning, out InterpretedMeaning);
                            if (ValidElement)
                            {
                                //انقل المعنى المتوافق لبداية القائمة
                                WordInfo T = Analyzer.AllWordsInfo[StartIndex + Element + Offset][0];
                                Analyzer.AllWordsInfo[StartIndex + Element + Offset][0]           = Analyzer.AllWordsInfo[StartIndex + Element + Offset][Possibility];
                                Analyzer.AllWordsInfo[StartIndex + Element + Offset][Possibility] = T;
                            }
                        }
                        if (ValidElement && ElementMeaning[0] == 'T')               // إذا كان العنصر حرفا صالحا
                        {
                            MyInterpretation NewInterpret = new MyInterpretation(); //إعراب جديد
                            NewInterpret.Meaning = InterpretedMeaning;
                            Relation.Elements.Add(StartIndex + Element + Offset, NewInterpret);
                            NewInterpret.SuperRelation = Relation;
                            break;
                        }
                        if (!MyInterpretation.Interpretations.ContainsKey(ElementInterpretation))    //إذا كان الإعراب المخزن وصفه غير موجود
                        {
                            //خطأ في قاعدة البيانات أو حرف غير صالح للقاعدة
                            if (ElementMeaning[0] == 'T')
                            {
                                continue;
                            }
                            ValidElement = false;
                            ValidRule    = false;
                            break;
                        }
                        InterpretFound = MyInterpretation.Interpretations[ElementInterpretation];    //البحث عن معنى رمز الإعراب
                        if (ValidElement)
                        {
                            MyInterpretation NewInterpret = new MyInterpretation(); //إعراب جديد
                            NewInterpret.Description = InterpretFound[0];           //نحميل وصف الإعراب
                            //if (ElementMeaning[0] == 'N')//إضافة الإعراب إلى المعنى حسب نوع الكلمة
                            //{
                            //    CompareMeanings(InterpretedMeaning, "N000" + InterpretFound[1], out ElementMeaning);
                            //}
                            //else if (ElementMeaning[0] == 'V')
                            //{
                            //    CompareMeanings(InterpretedMeaning, "V000000" + InterpretFound[1], out ElementMeaning);
                            //}
                            NewInterpret.Meaning = InterpretedMeaning;
                            Relation.Elements.Add(StartIndex + Element + Offset, NewInterpret);
                            NewInterpret.SuperRelation = Relation;
                            break;
                        }
                        else if (MaximumRecursion > 0 && Element > 0)
                        {
                            //إذا كان العنصر لا يصلح للقاعدة وليس العنصر الرئيسي بها
                            //وهناك فرصة للجمل المتداخلة
                            //ابحث عن مجموعة كلمات تحقق المعنى المطلوب
                            GrammarRelation SubRelation    = new GrammarRelation();
                            int             ModifiedOffset = StartInterpreting(ref SubRelation, StartIndex + Element + Offset, ElementMeaning, --MaximumRecursion, true);
                            if (ModifiedOffset == 0)
                            {
                                //معنى الكلمة غير صالح؛ ابحث باقي احتمالات المعنى
                                continue;
                            }
                            //إذا نجح البحث عن مجموعة كلمات
                            Offset += ModifiedOffset;
                            SubRelation.Description   = InterpretFound[0];
                            SubRelation.Meaning       = InterpretedMeaning;
                            SubRelation.SuperRelation = Relation;
                            Relation.Elements.Add(-1, SubRelation);
                            ValidElement = true;
                            break;
                        }
                    }
                    if (!ValidElement)
                    {
                        ValidRule = false;
                        break;
                    }
                }
                if (!ValidRule)
                {
                    continue;
                }
                if (!RecursiveCall)
                {
                    ApplyGrammarRelation(Relation);
                }
                break;
            }
            if (ValidRule)
            {
                return(Relation.Elements.Count);
            }
            else
            {
                return(0);
            }
        }
示例#4
0
 public static void LoadInterpreterData()
 {
     MyInterpretation.InitializeInterpretations();
     GrammarRelation.InitializeRules();
 }