diff --git a/机器学习竞赛实战_优胜解决方案/常用特征构建方法/.ipynb_checkpoints/文本特征处理-checkpoint.ipynb b/机器学习竞赛实战_优胜解决方案/常用特征构建方法/.ipynb_checkpoints/文本特征处理-checkpoint.ipynb index 9292cd8..54946b6 100644 --- a/机器学习竞赛实战_优胜解决方案/常用特征构建方法/.ipynb_checkpoints/文本特征处理-checkpoint.ipynb +++ b/机器学习竞赛实战_优胜解决方案/常用特征构建方法/.ipynb_checkpoints/文本特征处理-checkpoint.ipynb @@ -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='