|
|
|
@ -131,7 +131,7 @@
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"execution_count": 3,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
@ -140,20 +140,196 @@
|
|
|
|
|
"text": [
|
|
|
|
|
"showing info https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
|
|
|
|
"True"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"execution_count": 3,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "execute_result"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"nltk.download()"
|
|
|
|
|
"nltk.download() # 下载失败的用这个方法https://blog.csdn.net/qq_37891889/article/details/104418106"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"execution_count": 5,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"*** Introductory Examples for the NLTK Book ***\n",
|
|
|
|
|
"Loading text1, ..., text9 and sent1, ..., sent9\n",
|
|
|
|
|
"Type the name of the text or sentence to view it.\n",
|
|
|
|
|
"Type: 'texts()' or 'sents()' to list the materials.\n",
|
|
|
|
|
"text1: Moby Dick by Herman Melville 1851\n",
|
|
|
|
|
"text2: Sense and Sensibility by Jane Austen 1811\n",
|
|
|
|
|
"text3: The Book of Genesis\n",
|
|
|
|
|
"text4: Inaugural Address Corpus\n",
|
|
|
|
|
"text5: Chat Corpus\n",
|
|
|
|
|
"text6: Monty Python and the Holy Grail\n",
|
|
|
|
|
"text7: Wall Street Journal\n",
|
|
|
|
|
"text8: Personals Corpus\n",
|
|
|
|
|
"text9: The Man Who Was Thursday by G . K . Chesterton 1908\n"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"from nltk.book import *"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 10,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now']\n"
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 词频与停用词\n",
|
|
|
|
|
"wpt = nltk.WordPunctTokenizer()\n",
|
|
|
|
|
"stop_words = nltk.corpus.stopwords.words('english')\n",
|
|
|
|
|
"print(stop_words)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "markdown",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"停用词:\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"这里面除了天气和动物信息,其它都基本没用,如i me my等等这些词,这些相当于停用词"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 11,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": [
|
|
|
|
|
"# 词频与停用词"
|
|
|
|
|
"def normalize_document(doc):\n",
|
|
|
|
|
" # 预处理\n",
|
|
|
|
|
" doc = re.sub(r'[^a-zA-Z0-9\\s]', '', doc, re.I) # 去掉多余字符\n",
|
|
|
|
|
" doc = doc.lower() # 统一转小写\n",
|
|
|
|
|
" doc = doc.strip() # 去空格\n",
|
|
|
|
|
" # 分词,切分提取全部词\n",
|
|
|
|
|
" tokens = wpt.tokenize(doc) \n",
|
|
|
|
|
" # 查找停用词,并过滤\n",
|
|
|
|
|
" filtered_tokens = [token for token in tokens if token not in stop_words]\n",
|
|
|
|
|
" # 拼接所有的词\n",
|
|
|
|
|
" doc = ' '.join(filtered_tokens)\n",
|
|
|
|
|
" return doc\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"normalize_corpus = np.vectorize(normalize_document)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 8,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
|
|
|
|
"array(['sky blue beautiful', 'love blue beautiful sky',\n",
|
|
|
|
|
" 'quick brown fox jumps lazy dog', 'brown fox quick blue dog lazy',\n",
|
|
|
|
|
" 'sky blue sky beautiful today', 'dog layz brown fox quick'],\n",
|
|
|
|
|
" dtype='<U30')"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"execution_count": 8,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "execute_result"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"norm_corpus = normalize_corpus(corpus)\n",
|
|
|
|
|
"norm_corpus # 处理完成的结果"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "markdown",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"## 词袋模型\n",
|
|
|
|
|
"将所有词语装进一个袋子里,不考虑其词法和语序的问题,即每个词语都是独立的。"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": 12,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [
|
|
|
|
|
{
|
|
|
|
|
"name": "stdout",
|
|
|
|
|
"output_type": "stream",
|
|
|
|
|
"text": [
|
|
|
|
|
"['sky blue beautiful' 'love blue beautiful sky'\n",
|
|
|
|
|
" 'quick brown fox jumps lazy dog' 'brown fox quick blue dog lazy'\n",
|
|
|
|
|
" 'sky blue sky beautiful today' 'dog layz brown fox quick']\n",
|
|
|
|
|
"['beautiful', 'blue', 'brown', 'dog', 'fox', 'jumps', 'layz', 'lazy', 'love', 'quick', 'sky', 'today']\n"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"data": {
|
|
|
|
|
"text/plain": [
|
|
|
|
|
"array([[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],\n",
|
|
|
|
|
" [1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0],\n",
|
|
|
|
|
" [0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0],\n",
|
|
|
|
|
" [0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0],\n",
|
|
|
|
|
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1],\n",
|
|
|
|
|
" [0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0]], dtype=int64)"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
"execution_count": 12,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"output_type": "execute_result"
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"source": [
|
|
|
|
|
"from sklearn.feature_extraction.text import CountVectorizer\n",
|
|
|
|
|
"print(norm_corpus)\n",
|
|
|
|
|
"# 利用语句中的词,构建成一个词汇表。\n",
|
|
|
|
|
"# min_df表示词频率小于多少不会被当做关键词,max_df则相反\n",
|
|
|
|
|
"# 直接API文档https://scikit-learn.org/stable/modules/classes.html,搜CountVectorizer\n",
|
|
|
|
|
"cv = CountVectorizer(min_df=0., max_df=1.)\n",
|
|
|
|
|
"cv.fit(norm_corpus)\n",
|
|
|
|
|
"print(cv.get_feature_names())\n",
|
|
|
|
|
"# 构建向量表\n",
|
|
|
|
|
"cv_matrix = cv.fit_transform(norm_corpus)\n",
|
|
|
|
|
"cv_matrix = cv_matrix.toarray()\n",
|
|
|
|
|
"cv_matrix"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "markdown",
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"source": [
|
|
|
|
|
"如上数据中,在词汇表中,是否出现过,有则在相应位置标记为1,有两个则标记为2。\n",
|
|
|
|
|
"\n",
|
|
|
|
|
"如:第一句话'sky blue beautiful',在词汇表有第一个词一次,第二个词一次,导数第二个词一次,那么下面的向量表则是[1,1,...,1,0]"
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
"cell_type": "code",
|
|
|
|
|
"execution_count": null,
|
|
|
|
|
"metadata": {},
|
|
|
|
|
"outputs": [],
|
|
|
|
|
"source": []
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"metadata": {
|
|
|
|
|