الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

خطأ فى كود بحث بـ ألف بهمزة وألف بدون همزة والتاء المربوطة

بدأه هانى عوض في 11 يناير 2012 · 3 رد · 917 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

تحية طيبة

بعد بحث وعدة محاولات لعمل كلاس تقوم بدورها بالبحث بالهمزات والتاء المربوطة أو بدون..

وجدت فى منتدى الجافا التالى

MOHAMED FATHEI كتب:

اولا اشكرك على مجاملتك الرقيقة

ثانيا انا لم اطلب منك ان تجربها بنص معين مثل "أمجمد أحمد أشرف"

أنا قلت جربها وتركت لك حرية التجربة

طبعا كلامك صحيح عن الاحتمالات ولكن قبل ان تعرضها كنت جربها وجرب غيرها أيضا

الفئة تقوم بالدوران داخل النص واستبدال الحرف أ مثلا بالحرف ا فقط حتى لو كان موجودا ل 20 مرة او اكثر وليس مرة واحدة فقط

على كل قمت باضافة بعض الحروف و اضافة test case واليك الفئة

package arabic;
/**
 * This class made to help developers to handle Arabic strings with database
 * operations like Insert , Search
 * 
 * @author Mohammed Fathei
 * @version 1.0
 */
public class ArabicFilter {
 /*
  * an array represents Arabic letters and their against standard characters
  */
 private final static String arabicChars[][] = { 
  	{ "أ", "ا" },
  	{ "إ", "ا" },
  	{ "آ", "ا" },
  	{ "اٍ", "ا" },
  	{ "ة", "ه" }, 
  	{ "ى", "ي" },
  	{ "ـ",  "" }
  };

 /**
  * this method receive arabic text with different characters that may cause
  * problems with database handling and apply filter on it to be valid and
  * better for this purpose .
  * 
  * @param arabicText
  *        	a string to apply filters on it like replacing
  *        	<code>'أ'</code> and <code>'اٍ'</code> with <code>'ا'</code>
  *        	and so on .
  * @return the arabic text entered after applying filter and ready to use
  *     	with database .
  */
 public static String encodeArabicText(String arabicText) {
  /*
   * check if the string is empty
   */
  if ((arabicText.isEmpty()) || (arabicText.trim().length() == 0)) {
   return "";
  }

  int length = arabicText.length();
  StringBuffer encodedText = new StringBuffer(2 * length);
  encodedText.append(arabicText);

  int location = -1;

  for (int i = 0; i < arabicChars.length; i++) {
   while ((location = getLocationOfString(arabicChars[0],
     encodedText)) >= 0) {
    encodedText.replace(location,
      (location + arabicChars[0].length()),
      arabicChars[1]);
   }
  }

  return encodedText.toString();
 }

 /*
  * this method search the given StringBuffer for a specified string
  * 
  * @param subString a string to search for .
  * 
  * @param container a stringBuffer object to search in it .
  * 
  * @return the first index of the stringBuffer substring that matches the
  * string search for it or -1 if not found .
  */
 private static int getLocationOfString(String subString,
   StringBuffer container) {
  int location = 0;
  try {
   location = container.indexOf(subString);
   return location;
  } catch (Exception e) {
   System.err.println(e.getMessage());
   return -1;
  }
 }

 public static void main(String args[]) {
  String txt[] = {"مةحأمد أحــــــــمأداٍى","أمجد أحمد اشرف","امجد أحمد اشرف","امجد أحمد أشرف","اٍمجد آحمد أشــــــــرف"};

	for(int i=0; i< txt.length; i++){
    	System.out.println(ArabicFilter.encodeArabicText(txt) + "\n");
	}

 }

}

لاحظ اسم الحزمة arabic package

جربها كيفما تشـــــــــــــــــــــــاء

بالتوفيق

وقمت بتحويل الكود إلى c# كالتالى

using System;
using System.Text;

namespace arabic
{
	/// <summary>
	/// This class made to help developers to handle Arabic strings with database
	/// operations like Insert , Search
	/// 
	/// @author Mohammed Fathei
	/// @version 1.0
	/// </summary>
	public class ArabicFilter
	{
	 /*
	  * an array represents Arabic letters and their against standard characters
	  */
	 private static readonly string[][] arabicChars = {new string[] {"أ", "ا"}, new string[] {"إ", "ا"}, new string[] {"آ", "ا"}, new string[] {"اٍ", "ا"}, new string[] {"ة", "ه"}, new string[] {"ى", "ي"}, new string[] {"ـ", ""}};

	 /// <summary>
	 /// this method receive arabic text with different characters that may cause
	 /// problems with database handling and apply filter on it to be valid and
	 /// better for this purpose .
	 /// </summary>
	 /// <param name="arabicText">
	 ///             a string to apply filters on it like replacing
	 ///             <code>'أ'</code> and <code>'اٍ'</code> with <code>'ا'</code>
	 ///             and so on . </param>
	 /// <returns> the arabic text entered after applying filter and ready to use
	 ///             with database . </returns>
	 public static string encodeArabicText(string arabicText)
	 {
	  /*
	   * check if the string is empty
	   */
	  if ((arabicText.Length == 0) || (arabicText.Trim().Length == 0))
	  {
	   return "";
	  }

	  int length = arabicText.Length;
	  StringBuilder encodedText = new StringBuilder(2 * length);
	  encodedText.Append(arabicText);

	  int location = -1;

	  for (int i = 0; i < arabicChars.Length; i++)
	  {
	   while ((location = getLocationOfString(arabicChars[0], encodedText)) >= 0)
	   {
		encodedText.Replace(location, (location + arabicChars[0].Length), arabicChars[1]);
	   }
	  }

	  return encodedText.ToString();
	 }

	 /*
	  * this method search the given StringBuffer for a specified string
	  * 
	  * @param subString a string to search for .
	  * 
	  * @param container a stringBuffer object to search in it .
	  * 
	  * @return the first index of the stringBuffer substring that matches the
	  * string search for it or -1 if not found .
	  */
	 private static int getLocationOfString(string subString, StringBuilder container)
	 {
	  int location = 0;
	  try
	  {
                location = Convert.ToInt32((container.ToString().IndexOf(subString)));

	   return location;
	  }
	  catch (Exception e)
	  {
	   System.err.println(e.Message);
	   return -1;
	  }
	 }

	 static void Main(string[] args)
	 {
	  string[] txt = {"مةحأمد أحــــــــمأداٍى","أمجد أحمد اشرف","امجد أحمد اشرف","امجد أحمد أشرف","اٍمجد آحمد أشــــــــرف"};

			for (int i = 0; i < txt.Length; i++)
			{
			Console.WriteLine(ArabicFilter.encodeArabicText(txt) + "\n");
			}

	 }

	}
}

ولكن فى السطر التالى

		encodedText.Replace(location, (location + arabicChars[0].Length), arabicChars[1]);

ظهر خطأ كالتالى

No overload for method 'Replace' takes 3 arguments

أرجو الإفادة للإهمية

مع تحياتى

#2

اجرية بعض التعديل علي الوظيفة جرب

 public static string encodeArabicText(string arabicText)
        {
            string[][] arabicChars = { new string[] { "أ", "ا" }, new string[] { "إ", "ا" }, new string[] { "آ", "ا" }, new string[] { "اٍ", "ا" }, new string[] { "ة", "ه" }, new string[] { "ى", "ي" }, new string[] { "ـ", "" } };

            string sText = arabicText;

            foreach (string[] arabicChar in arabicChars)
                sText = sText.Replace(arabicChar[0].ToString(), arabicChar[1].ToString());

            return sText.ToString();
        }
1
#3

شكراً أخى على مساعدتك..

وبالفعل تم العمل على الكود طوال اليوم لمحاولة إرجاع كافة نتائج البحث المتشابهة، من خلال تباديل وتوافيق مع الهمزات والتاء المربوطة...

ولازال العمل مستمر حتى أصل لنتيجة بحث متميزة..

مع تحياتى

#4

يجب ان يمر حقل البحث والكلمة التي سيتم البحث عنها علي هذة الوظيفة ليزيد نسبة التطابق

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…