ต้องบอกก่อนว่าการทำ Text Mining นั้นเปรียบเสมือนการที่เราเอาข้อมูลต่าง ๆมาทำการกรองเพื่อจะแยกสิ่งที่เราต้องการที่จะทราบ ตัวอย่างในการทำ Text Mining ที่ผมจะยกมาคือการที่เราจะใช้ข้อมูลของ Twitter เกี่ยวกับข้อมูลของคำว่า Hbase กับ Cassandra ว่าคนพูดถึงคำไหนมากกว่ากัน
ขั้นตอนแรกนั้นเราจะต้องทำการ import library ที่จำเป็นมาเพื่อเตรียมใช้งาน
โค้ด: เลือกทั้งหมด
%matplotlib inline
import json
import pandas as pd
import matplotlib.pyplot as plt
import re
โค้ด: เลือกทั้งหมด
tweets_data_path = '../twitter_data.txt'
tweets_data = []
tweets_file = open(tweets_data_path, "r")
# Append Data to the variables
for line in tweets_file:
try:
tweet = json.loads(line)
tweets_data.append(tweet)
except:
continue
โค้ด: เลือกทั้งหมด
print len(tweets_data)
โค้ด: เลือกทั้งหมด
tweets = pd.DataFrame()
# Create 3 columns consist of text, lang, and country
tweets['text'] = map(lambda tweet: tweet['text'], tweets_data)
tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)
โค้ด: เลือกทั้งหมด
tweets_by_lang = tweets['lang'].value_counts()
fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Languages', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 languages', fontsize=15, fontweight='bold')
tweets_by_lang[:5].plot(ax=ax, kind='bar', color='red')
plt.show()
โค้ด: เลือกทั้งหมด
def word_in_text(word, text):
word = word.lower()
text = text.lower()
match = re.search(word, text)
if match:
return True
return False
โค้ด: เลือกทั้งหมด
tweets['hbase'] = tweets['text'].apply(lambda tweet: word_in_text('hbase', tweet))
tweets['cassandra'] = tweets['text'].apply(lambda tweet: word_in_text('cassandra', tweet))
โค้ด: เลือกทั้งหมด
print tweets['hbase'].value_counts()[True]
print tweets['cassandra'].value_counts()[True]
โค้ด: เลือกทั้งหมด
db_type = ['hbase', 'cassandra']
tweets_by_db_type = [tweets['hbase'].value_counts()[True],
tweets['cassandra'].value_counts()[True]]
x_pos = list(range(len(db_type)))
width = 0.8
fig, ax = plt.subplots()
plt.bar(x_pos, tweets_by_db_type, width, alpha=1,color='g')
ax.set_ylabel('Number of tweets', fontsize=15)
ax.set_title('Rankging: hbase vs. cassandra (Raw data)', fontsize=10, fontweight='bold')
ax.set_xticks([p + 0.4 * width for p in x_pos])
ax.set_xticklabels(db_type)
plt.grid()
plt.show()
คำสั่งเพื่อทำการกรองคำเชิงลึก
โดยเราจะกรองคำที่เกี่ยวข้องกับ คำว่า “bigdata”, “database”, “parallel”, “nosql”
โค้ด: เลือกทั้งหมด
tweets['relevant'] = tweets['text'].apply(lambda tweet: word_in_text('bigdata', tweet) or word_in_text('database', tweet) or word_in_text('parallel',tweet) or word_in_text('nosql', tweet))
โค้ด: เลือกทั้งหมด
print tweets[tweets['relevant'] == True]['hbase'].value_counts()[True]
print tweets[tweets['relevant'] == True]['cassandra'].value_counts()[True]
เราลองมาดูในส่วนของกราฟกันต่อ
โค้ด: เลือกทั้งหมด
tweets_by_db_type = [tweets[tweets['relevant'] == True]['hbase'].value_counts()[True],
tweets[tweets['relevant'] == True]['cassandra'].value_counts()[True]]
width = 0.8
fig, ax = plt.subplots()
plt.bar(x_pos, tweets_by_db_type, width, alpha=1, color='r')
ax.set_ylabel('Number of tweets', fontsize=15)
ax.set_title('Ranking: hbase vs. cassandra (Relevant data)', fontsize=10, fontweight='bold')
ax.set_xticks([p + 0.4 * width for p in x_pos])
ax.set_xticklabels(db_type)
plt.grid()
plt.show()
อ้างอิง : lukkiddd.com