Trong SEO, phân tích n-gram giúp chúng ta hiểu rõ hơn về các cụm từ phổ biến mà người dùng tìm kiếm. Bằng cách xác định những cụm từ quan trọng nhất từ báo cáo truy vấn, chúng ta có thể tối ưu hóa nội dung để phù hợp với xu hướng tìm kiếm của người dùng.
Bài viết này hướng dẫn bạn cách sử dụng Python để phân tích n-gram từ dữ liệu SEO, xác định các cụm từ phổ biến nhất và tổng hợp lượng hiển thị (Impressions) của chúng. Toàn bộ quy trình được thực hiện trong Google Colab.
1. Cài Đặt Thư Viện Cần Thiết
Trước khi chạy mã, bạn cần cài đặt các thư viện Python sau trong Google Colab:
!pip install nltk pandas numpy
- nltk: Thư viện xử lý ngôn ngữ tự nhiên giúp phân tích n-gram.
- pandas: Quản lý dữ liệu dưới dạng DataFrame.
- numpy: Hỗ trợ các phép toán nhanh chóng trên dữ liệu số.
2. Chuẩn Bị Dữ Liệu
Chúng ta sử dụng dữ liệu từ tệp CSV chứa truy vấn tìm kiếm của người dùng. Hãy tải tệp lên Google Colab trước khi chạy mã.
import pandas as pd
import collections
import nltk
import numpy as np
from google.colab import files
# Tải dữ liệu
uploaded = files.upload()
# Đọc file CSV (giả sử tệp có cột 'queries' và 'Impressions')
df = pd.read_csv('/content/query.csv')
Dữ liệu cần có hai cột:
- queries: Chứa truy vấn của người dùng.
- Impressions: Số lần hiển thị truy vấn đó trên công cụ tìm kiếm.
3. Phân Tích N-Gram
Bước tiếp theo là tách các truy vấn thành các cụm từ (n-gram) để xem từ hoặc cụm từ nào xuất hiện nhiều nhất.
# Tạo danh sách truy vấn
list_of_keywords = df['queries'].tolist()
list_of_words_in_keywords = [x.split(" ") for x in list_of_keywords]
# Đếm số lần xuất hiện của từng n-gram
counts = collections.Counter()
for phrase in list_of_words_in_keywords:
counts.update(nltk.ngrams(phrase, 1)) # Unigram (1-gram)
counts.update(nltk.ngrams(phrase, 2)) # Bigram (2-gram)
# Lấy 400 n-grams phổ biến nhất
top_400 = counts.most_common(400)
x = pd.DataFrame(top_400, columns=['query', 'count'])
4. Tính Tổng Số Lần Hiển Thị Cho Mỗi N-Gram
Chúng ta sẽ tính tổng số lần hiển thị (Impressions) của từng n-gram bằng cách kiểm tra tất cả các truy vấn chứa nó.
# Hàm lấy tổng impressions
def GetVolume(query):
df_filtered = df[df['queries'].str.contains(query, regex=False, na=False)]
return df_filtered['Impressions'].sum()
# Xóa các ký tự không cần thiết
def RemoveUselessChar(x):
return ''.join([char for char in x if char not in "'(),"])
# Làm sạch dữ liệu và tính volume
x['query'] = x['query'].apply(lambda q: RemoveUselessChar(str(q)))
x['Volume'] = x['query'].apply(GetVolume)
5. Xuất Dữ Liệu Thành File CSV
Sau khi hoàn thành phân tích, chúng ta sẽ lưu dữ liệu vào một tệp CSV để sử dụng sau này.
# Xuất dữ liệu
x.to_csv('ngram_analysis.csv', index=False)
# Tải file về máy
files.download('ngram_analysis.csv')
6. Ứng Dụng Trong SEO
- Nghiên cứu từ khóa: Xác định các cụm từ phổ biến mà người dùng tìm kiếm.
- Tối ưu nội dung: Chỉnh sửa bài viết để bao gồm các n-gram quan trọng.
- Phân tích xu hướng: Theo dõi sự thay đổi của các n-gram theo thời gian.
Kết Luận
Với Python và Google Colab, bạn có thể nhanh chóng phân tích n-gram từ dữ liệu SEO để tối ưu hóa nội dung hiệu quả hơn. Hãy thử chạy mã trên Google Colab và áp dụng vào chiến lược SEO của bạn ngay hôm nay!
https://shorturl.fm/upEfy
https://shorturl.fm/uXfvP
https://shorturl.fm/QznAb
https://shorturl.fm/FEnvQ
weed gummies bulk orders with discounts
Join our affiliate program today and earn generous commissions!
Jilibet178, now that sounds like a proper betting site. In for some action, hopefully good odds. Let’s go! Find out more here jilibet178
Jiiliasia, ready for an Asian-themed adventure! Hope the gameplay is smooth and the payouts are sweet. Let’s see what’s in store. Give it a whirl here jiiliasia
Alright mates, gave idd888 a whirl the other day. Not bad, not bad at all. A decent selection of games, and the site’s pretty easy to navigate. Might stick around for a bit, y’know. Check it out here idd888.
https://shorturl.fm/W7KY2
This was helpful, the part about clothing care was broken down so well, will bookmark this for reference.