تجزئة النص وإزالة كلمات التوقف
قسّم النص إلى رموز، واستبعد الكلمات غير المفيدة باستخدام anti_join()
تجزئة النص وإزالة كلمات التوقف درس مجاني في R Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في R Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة R Academy 4 دروس في المجموع.
ما التنقيب النصي؟
يحوّل التنقيب النصي (أو تحليلات النصوص) النص غير المنظم إلى بيانات منظمة يمكن تحليلها إحصائيًا. تتيح حزمة tidytext اتباع منهج tidy، بحيث يمثل كل صف وحدة نصية (كلمة أو ثنائي كلمات أو جملة)، مما يجعل البيانات متوافقة مع dplyr وggplot2.
library(tidytext)
library(dplyr)
# A simple text corpus
text_df <- tibble::tibble(
doc_id = 1:3,
text = c(
'The quick brown fox jumps over the lazy dog.',
'Text mining with R is powerful and fun.',
'Natural language processing enables many applications.'
)
)
cat('Input: ', nrow(text_df), 'documents\n')
cat('Columns:', names(text_df), '\n')unnest_tokens: تقسيم النص إلى وحدات كلمات
تقسم unnest_tokens(output, input) النص إلى صيغة يكون فيها لكل وحدة نصية صف واحد. وتقسم النص افتراضيًا حسب الكلمات، مع تحويلها إلى أحرف صغيرة وإزالة علامات الترقيم. وتدعم الوسيطة token القيم 'words' و'ngrams' و'sentences' وغيرها.
library(tidytext)
library(dplyr)
text_df <- tibble::tibble(
doc_id = 1:2,
text = c(
'R is a great language for data analysis.',
'Text mining reveals hidden patterns in documents.'
)
)
# Tokenise into words
tokens <- text_df |>
unnest_tokens(word, text)
cat('Tokens extracted:', nrow(tokens), '\n')
print(tokens)مجموعة بيانات stop_words
تأتي حزمة tidytext مع tibble مضمّن باسم stop_words يحتوي على 1,149 كلمة شائعة من كلمات التوقف الإنجليزية، مأخوذة من ثلاث قوائم معجمية: SMART وSnowball وonix. تحمل هذه الكلمات ("the" و"is" و"and"…) معنى دلاليًا محدودًا، ولذلك تُزال عادةً قبل التحليل.
library(tidytext)
library(dplyr)
# Inspect the stop_words dataset
cat('Total stop words:', nrow(stop_words), '\n')
cat('Lexicons:', paste(unique(stop_words$lexicon), collapse = ', '), '\n')
# First few stop words from each lexicon
stop_words |>
group_by(lexicon) |>
slice_head(n = 3) |>
print()anti_join: إزالة كلمات التوقف
تزيل anti_join(tokens, stop_words, by = 'word') جميع الصفوف التي تطابق فيها قيمة word أي إدخال في stop_words. وهذا هو النمط القياسي لإزالة كلمات التوقف باستخدام tidytext؛ فهو واضح ومقروء وسهل التوسعة.
library(tidytext)
library(dplyr)
text_df <- tibble::tibble(
doc_id = 1:3,
text = c(
'The quick brown fox jumps over the lazy dog',
'A stitch in time saves nine important words',
'To be or not to be that is the question'
)
)
tokens <- text_df |>
unnest_tokens(word, text)
cat('Before removing stop words:', nrow(tokens), '\n')
tokens_clean <- tokens |>
anti_join(stop_words, by = 'word')
cat('After removing stop words:', nrow(tokens_clean), '\n')
print(tokens_clean)count: تكرار المصطلحات
بعد تقسيم النص إلى وحدات وإزالة كلمات التوقف، استخدم count(word, sort = TRUE) لحساب تكرار المصطلحات. وهذا هو الأساس للعديد من تحليلات التنقيب النصي؛ فالكلمات ذات المعنى الأكثر تكرارًا تميز موضوعات المستند.
library(tidytext)
library(dplyr)
# Use Jane Austen's novels from janeaustenr package
# For demo: simulate a small corpus
corpus <- tibble::tibble(
text = c(
'data science involves statistics programming analysis',
'machine learning algorithms data patterns training',
'statistics probability distributions random variables data',
'programming languages python r julia statistics',
'analysis patterns distributions algorithms science'
)
)
word_counts <- corpus |>
unnest_tokens(word, text) |>
anti_join(stop_words, by = 'word') |>
count(word, sort = TRUE)
cat('Top 8 words:\n')
print(head(word_counts, 8))كلمات توقف مخصصة
غالبًا ما تحتوي المجموعات النصية الخاصة بمجال معين على كلمات كثيرة التكرار لكنها غير مهمة (مثل "patient" و"doctor" في الملاحظات الطبية). أنشئ tibble مخصصًا لكلمات التوقف، ثم ادمجه مع القائمة المعجمية المضمّنة باستخدام bind_rows().
library(tidytext)
library(dplyr)
# Domain-specific words to remove
custom_stops <- tibble::tibble(
word = c('data', 'analysis', 'r', 'using', 'also'),
lexicon = 'custom'
)
# Combine with built-in stop words
all_stops <- bind_rows(stop_words, custom_stops)
cat('Total stop words after custom:', nrow(all_stops), '\n')
corpus <- tibble::tibble(
text = c(
'Using R for data analysis and machine learning models',
'Statistical data analysis also involves data visualisation'
)
)
clean_tokens <- corpus |>
unnest_tokens(word, text) |>
anti_join(all_stops, by = 'word') |>
count(word, sort = TRUE)
print(clean_tokens)تقسيم النص إلى ثنائيات كلمات
اضبط token = 'ngrams', n = 2 لتقسيم النص إلى أزواج متتالية من الكلمات (ثنائيات الكلمات). تلتقط ثنائيات الكلمات عبارات مثل "machine learning" أو "data science" التي قد تفوتها الكلمات المفردة، مما يوفر سياقًا أغنى.
library(tidytext)
library(dplyr)
corpus <- tibble::tibble(
doc_id = 1:2,
text = c(
'machine learning and deep learning are powerful techniques',
'natural language processing uses machine learning methods'
)
)
bigrams <- corpus |>
unnest_tokens(bigram, text, token = 'ngrams', n = 2)
cat('Bigrams extracted:', nrow(bigrams), '\n')
print(bigrams)
# Count most common bigrams
bigram_counts <- bigrams |> count(bigram, sort = TRUE)
cat('\nTop bigrams:\n')
print(head(bigram_counts, 5))تصفية ثنائيات الكلمات لتحسين الجودة
تتضمن ثنائيات الكلمات الأكثر تكرارًا أزواجًا تحتوي على كلمات توقف، مثل "of the". افصل ثنائي الكلمات إلى عمودين باستخدام tidyr::separate()، ثم صفِّ الصفوف التي تكون فيها أي من الكلمتين كلمة توقف، وأعد دمجهما للحصول على أزواج عبارات ذات معنى.
library(tidytext)
library(dplyr)
library(tidyr)
corpus <- tibble::tibble(
text = c(
'the field of machine learning and deep learning is growing',
'natural language processing is a subfield of artificial intelligence'
)
)
bigrams_filtered <- corpus |>
unnest_tokens(bigram, text, token = 'ngrams', n = 2) |>
separate(bigram, into = c('word1', 'word2'), sep = ' ') |>
filter(
!word1 %in% stop_words$word,
!word2 %in% stop_words$word
) |>
unite(bigram, word1, word2, sep = ' ') |>
count(bigram, sort = TRUE)
print(bigrams_filtered)تصوير تكرارات الكلمات
اعرض تكرارات الكلمات في مخطط أعمدة باستخدام ggplot2. رتّب الأعمدة باستخدام reorder(word, n)، واستخدم coord_flip() لعرض التسميات أفقيًا. ويُعد هذا من أكثر المخرجات إيصالًا للمعلومات في التنقيب النصي.
library(tidytext)
library(dplyr)
library(ggplot2)
corpus <- tibble::tibble(
text = c(
'statistics probability machine learning algorithms patterns',
'data science programming analysis visualisation models',
'machine learning deep learning neural networks patterns',
'probability statistics hypothesis testing distributions',
'algorithms optimisation gradient descent models training'
)
)
top_words <- corpus |>
unnest_tokens(word, text) |>
anti_join(stop_words, by = 'word') |>
count(word, sort = TRUE) |>
slice_head(n = 10)
ggplot(top_words, aes(reorder(word, n), n)) +
geom_col(fill = 'steelblue') +
coord_flip() +
labs(x = 'Word', y = 'Count', title = 'Top 10 Terms') +
theme_minimal()عدد الكلمات لكل مستند
عندما تحتوي مجموعتك النصية على عدة مستندات، أضف عمودًا لمعرّف المستند، وجمّع حسب doc_id, word لحساب تكرارات المصطلحات لكل مستند. ويمهّد ذلك مباشرةً لتحليلات TF-IDF ونمذجة الموضوعات.
library(tidytext)
library(dplyr)
docs <- tibble::tibble(
doc_id = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
text = c(
'machine learning models training',
'neural networks deep learning',
'algorithms gradient descent optimisation',
'statistical analysis probability distributions',
'hypothesis testing confidence intervals regression',
'bayesian inference prior posterior likelihood',
'data visualisation plots charts dashboards',
'ggplot2 ggvis plotly interactive graphics',
'colour scales aesthetics themes layers'
)
)
word_counts <- docs |>
unnest_tokens(word, text) |>
anti_join(stop_words, by = 'word') |>
count(doc_id, word, sort = TRUE)
cat('Word-doc pairs:', nrow(word_counts), '\n')
print(head(word_counts, 10))تقسيم النص إلى جمل
اضبط token = 'sentences' لتقسيم النص عند حدود الجمل. وتفيد الوحدات النصية على مستوى الجملة في تحليل المشاعر (حساب درجة كل جملة)، والتلخيص (اختيار الجمل الممثلة)، ومهام الإجابة عن الأسئلة.
library(tidytext)
library(dplyr)
doc <- tibble::tibble(
id = 1L,
text = paste(
'R is a statistical programming language.',
'It is widely used in data science and machine learning.',
'The tidyverse makes data manipulation easy.',
'Text mining with tidytext is elegant and powerful.'
)
)
sentences <- doc |>
unnest_tokens(sentence, text, token = 'sentences')
cat('Sentences found:', nrow(sentences), '\n')
print(sentences$sentence)تحقق سريع
لقد قسمت مجموعة نصية إلى كلمات باستخدام unnest_tokens()، وتريد إزالة الكلمات الإنجليزية الشائعة. ما العملية التي تحقق ذلك باستخدام مجموعة البيانات المضمّنة stop_words؟
مراجعة: تقسيم النص إلى وحدات وكلمات التوقف
أهم النقاط:
- تحوّل
unnest_tokens(word, text)النص الخام إلى صيغة tidy، بحيث يكون لكل كلمة صف واحد - الإعداد الافتراضي: تحويل إلى أحرف صغيرة وإزالة علامات الترقيم؛ مع دعم
'words'و'ngrams'و'sentences' -
stop_wordsهو tibble مضمّن يضم 1,149 كلمة إنجليزية شائعة من 3 قوائم معجمية - تزيل
anti_join(tokens, stop_words, by = 'word')كلمات التوقف - استخدم
bind_rows()لإضافة كلمات توقف مخصصة للمجال - تحسب
count(word, sort = TRUE)تكرار المصطلحات من الوحدات النصية المنظّمة - ثنائيات الكلمات:
unnest_tokens(bigram, text, token = 'ngrams', n = 2)
library(tidytext)
library(dplyr)
tibble::tibble(text = 'The quick brown fox jumps over the lazy dog') |>
unnest_tokens(word, text) |>
anti_join(stop_words, by = 'word') |>
count(word, sort = TRUE) |>
print()الأسئلة الشائعة
هل درس «تجزئة النص وإزالة كلمات التوقف» مجاني؟
نعم — نص درس «تجزئة النص وإزالة كلمات التوقف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة R Academy، انتقل إلى CoddyKit PRO. تتضمن دورة R Academy 4 دروس في المجموع.
ماذا ستتعلم في «تجزئة النص وإزالة كلمات التوقف»؟
قسّم النص إلى رموز، واستبعد الكلمات غير المفيدة باستخدام anti_join() تتمرن على R Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ R Academy؟
لا تُشترط خبرة سابقة. R Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «تجزئة النص وإزالة كلمات التوقف»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس R Academy هذا؟
نعم. كل درس في R Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تجزئة النص وإزالة كلمات التوقف
- تحليل TF-IDF وتكرار المصطلحات
- تحليل المشاعر في R
- نمذجة الموضوعات باستخدام LDA