البرمجة

بحث الكلمات الرئيسية في C#

لتحقيق هذا في C#، يمكنك استخدام Dictionary لتخزين مصفوفة الكلمات ومن ثم البحث عن الكلمات المفتاحية في النص الذي تريد البحث فيه. يمكنك استخدام Regex لتطابق الكلمات الرئيسية بشكل مرن. هناك عدة طرق لتحقيق هذا، وفيما يلي أحد الطرق الممكنة:

csharp
using System; using System.Collections.Generic; using System.Text.RegularExpressions; class Program { static void Main() { string[] terms = { "windows", "dual sim", "32 gb", "Intel i5" }; Dictionary<string, int> termDictionary = new Dictionary<string, int>(); for (int i = 0; i < terms.Length; i++) { termDictionary.Add(terms[i], i); } string searchTerm = "android mobile 32 gb"; int position = FindTermPosition(searchTerm, termDictionary); Console.WriteLine($"Search term '{searchTerm}' found at position {position}"); } static int FindTermPosition(string searchTerm, Dictionary<string, int> termDictionary) { // Split the search term into individual words string[] words = searchTerm.ToLower().Split(' '); foreach (string word in words) { // Use Regex to match the words in the termDictionary foreach (KeyValuePair<string, int> term in termDictionary) { if (Regex.IsMatch(word, "\\b" + Regex.Escape(term.Key.ToLower()) + "\\b")) { return term.Value; } } } return -1; // Not found } }

هذا الكود يبدأ بإنشاء Dictionary لتخزين مصفوفة الكلمات مع مواقعها. ثم، يقوم بتقسيم الكلمات في النص البحثي إلى كلمات فردية ويبحث عن تطابق باستخدام Regex. إذا تم العثور على تطابق، يُرجع الموقع في مصفوفة الكلمات.

المزيد من المعلومات

يمكنك تحسين هذا الكود بعدة طرق. على سبيل المثال، يمكنك استخدام Regex لبحث عن الكلمات المفتاحية معاً بدلاً من البحث عن كل كلمة على حدة. كما يمكنك استخدام Regex لتجاهل حالات الأحرف الكبيرة والصغيرة.

إليك كيفية تحسين الكود باستخدام Regex:

csharp
using System; using System.Collections.Generic; using System.Text.RegularExpressions; class Program { static void Main() { string[] terms = { "windows", "dual sim", "32 gb", "Intel i5" }; Dictionary<string, int> termDictionary = new Dictionary<string, int>(); for (int i = 0; i < terms.Length; i++) { termDictionary.Add(terms[i], i); } string searchTerm = "android mobile 32 gb"; int position = FindTermPosition(searchTerm, termDictionary); Console.WriteLine($"Search term '{searchTerm}' found at position {position}"); } static int FindTermPosition(string searchTerm, Dictionary<string, int> termDictionary) { foreach (KeyValuePair<string, int> term in termDictionary) { // Use Regex to match the term in the search string if (Regex.IsMatch(searchTerm.ToLower(), "\\b" + Regex.Escape(term.Key.ToLower()) + "\\b")) { return term.Value; } } return -1; // Not found } }

هذا الكود يستخدم Regex للبحث عن الكلمات المفتاحية في النص البحثي، ويعود بموقع أول كلمة متطابقة يتم العثور عليها. يتم تجاهل حالات الأحرف الكبيرة والصغيرة في هذا البحث.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر