R Academy · บทเรียน

การตัดคำเป็นโทเคนและการลบคำหยุด

แบ่งข้อความเป็นโทเคนและกรองคำที่ไม่ให้ข้อมูลด้วย anti_join()

บทเรียน 1 จาก 413 ขั้นตอน

การตัดคำเป็นโทเคนและการลบคำหยุด เป็นบทเรียน R Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 มาพร้อมทิบเบิล 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" ในบันทึกทางการแพทย์) ให้สร้างทิบเบิลคำหยุดแบบกำหนดเอง แล้วรวมเข้ากับพจนานุกรมในตัวด้วย 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 เป็นทิบเบิลในตัวที่มีคำภาษาอังกฤษที่พบบ่อย 1,149 คำจากพจนานุกรม 3 ชุด
  • anti_join(tokens, stop_words, by = 'word') ลบคำหยุด
  • ใช้ร่วมกับ bind_rows() เพื่อเพิ่มคำหยุดเฉพาะสาขาแบบกำหนดเอง
  • count(word, sort = TRUE) คำนวณความถี่ของคำจากโทเค็นในรูปแบบ tidy
  • ไบแกรม: 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()
เริ่มต้นได้ฟรี

เรียนรู้ R ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
43
บทเรียน
159

คำถามที่พบบ่อย

บทเรียน “การตัดคำเป็นโทเคนและการลบคำหยุด” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การตัดคำเป็นโทเคนและการลบคำหยุด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส R Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส R Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การตัดคำเป็นโทเคนและการลบคำหยุด”

แบ่งข้อความเป็นโทเคนและกรองคำที่ไม่ให้ข้อมูลด้วย anti_join() คุณปฏิบัติ R Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน R Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน R Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การตัดคำเป็นโทเคนและการลบคำหยุด” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน R Academy นี้ได้ไหม

ได้ บทเรียน R Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตัดคำเป็นโทเคนและการลบคำหยุด
  2. TF-IDF และการวิเคราะห์ความถี่ของคำ
  3. การวิเคราะห์ความรู้สึกใน R
  4. การสร้างโมเดลหัวข้อด้วย LDA
← กลับไปที่ R Academy