0
我正在處理文本分類問題,解析來自RSS提要的新聞報道,並且我懷疑很多HTML元素和亂碼都被計爲記號。我知道Beautiful Soup提供了清理HTML的方法,但是我想嘗試傳遞一個字典來更好地控制什麼是記號。Scikit-Learn TfidfVectorizer
這個概念看起來很簡單,但我得到的結果我不明白。
from sklearn.feature_extraction.text import TfidfVectorizer
eng_dictionary = []
with open("C:\\Data\\words_alpha.txt") as f:
eng_dictionary = f.read().splitlines()
short_dic = []
short_dic.append(("short"))
short_dic.append(("story"))
stories = []
stories.append("This is a short story about the color red red red red blue blue blue i am in a car")
stories.append("This is a novel about the color blue red red red red i am in a boot")
stories.append("I like the color green, but prefer blue blue blue blue blue red red red red i am on a bike")
vec = TfidfVectorizer(decode_error=u'ignore', stop_words='english', analyzer='word', lowercase=True)
pos_vector = vec.fit_transform(stories).toarray()
print(vec.get_feature_names())
vec = TfidfVectorizer(decode_error=u'ignore', stop_words='english', analyzer='word', lowercase=True, vocabulary=short_dic)
pos_vector = vec.fit_transform(stories).toarray()
print(vec.get_feature_names())
vec = TfidfVectorizer(decode_error=u'ignore', stop_words='english', analyzer='word', lowercase=True, vocabulary=eng_dictionary)
pos_vector = vec.fit_transform(stories).toarray()
print(vec.get_feature_names())
該程序的輸出如下;
['bike', 'blue', 'boot', 'car', 'color', 'green', 'like', 'novel', 'prefer', 'red', 'short', 'story']
['short', 'story']
ptic', 'skeptical', 'skeptically', 'skepticalness', 'skepticism', 'skepticize', 'skepticized', 'skepticizing'...
第三打印的輸出上和去,所以我故意把它剪短,什麼是奇怪的是,雖然它開始中間字,正如我告訴它上面。前兩份印刷聲明的結果對我來說是有意義的;
- 缺少詞彙表示要素是直接從語料庫構建的。
- 提供了一個詞彙意味着功能從令牌建立在語料庫和詞彙
然而,在第三打印出的特點是不是我的文集的一部分,他們爲什麼不顯示?
我如何才能從詞彙中提取特徵? –
你是指eng_dictionary和第三個故事中的單詞重疊嗎?或者只是第三個故事中的單詞? – AMC
我一直在尋找eng_dictionary和每個故事中的單詞之間的重疊 –