成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何用Python清理文本數據?

開發 后端
不是所有數據格式都會采用表格格式。隨著我們進入大數據時代,數據的格式非常多樣化,包括圖像、文本、圖形等等。

 不是所有數據格式都會采用表格格式。隨著我們進入大數據時代,數據的格式非常多樣化,包括圖像、文本、圖形等等。

因為格式非常多樣,從一個數據到另一個數據,所以將這些數據預處理為計算機可讀的格式是非常必要的。

在本文中,將展示如何使用Python預處理文本數據,我們需要用到 NLTK 和 re-library 庫。

 

如何用Python清理文本數據?

 

過程

1.文本小寫

在我們開始處理文本之前,最好先將所有字符都小寫。我們這樣做的原因是為了避免區分大小寫的過程。

假設我們想從字符串中刪除停止詞,正常操作是將非停止詞合并成一個句子。如果不使用小寫,則無法檢測到停止詞,并將導致相同的字符串。這就是為什么降低文本大小寫這么重要了。

用Python實現這一點很容易。代碼是這樣的:

 

  1. # 樣例 
  2. x = "Watch This Airport Get Swallowed Up By A Sandstorm In Under A Minute http://t.co/TvYQczGJdy" 
  3. # 將文本小寫 
  4. x = x.lower() 
  5. print(x) 
  6. >>> watch this airport get swallowed up by a sandstorm in under a minute http://t.co/tvyqczgjdy 

 

2.刪除 Unicode 字符

一些文章中可能包含 Unicode 字符,當我們在 ASCII 格式上看到它時,它是不可讀的。大多數情況下,這些字符用于表情符號和非 ASCII 字符。要刪除該字符,我們可以使用這樣的代碼:

 

  1. # 示例 
  2. x = "Reddit Will Now Quarantine‰Û_ http://t.co/pkUAMXw6pm #onlinecommunities #reddit #amageddon #freespeech #Business http://t.co/PAWvNJ4sAP" 
  3. # 刪除 unicode 字符 
  4. x = x.encode('ascii''ignore').decode() 
  5. print(x) 
  6. >>> Reddit Will Now Quarantine_ http://t.co/pkUAMXw6pm #onlinecommunities #reddit #amageddon #freespeech #Business http://t.co/PAWvNJ4sAP 

 

3.刪除停止詞

停止詞是一種對文本意義沒有顯著貢獻的詞。因此,我們可以刪除這些詞。為了檢索停止詞,我們可以從 NLTK 庫中下載一個資料庫。以下為實現代碼:

 

  1. import nltk 
  2. nltk.download() 
  3. # 只需下載所有nltk 
  4. stop_words = stopwords.words("english"
  5. # 示例 
  6. x = "America like South Africa is a traumatised sick country - in different ways of course - but still messed up." 
  7. # 刪除停止詞 
  8. x = ' '.join([word for word in x.split(' ') if word not in stop_words]) 
  9. print(x) 
  10. >>> America like South Africa traumatised sick country - different ways course - still messed up. 

 

4.刪除諸如提及、標簽、鏈接等術語。

除了刪除 Unicode 和停止詞外,還有幾個術語需要刪除,包括提及、哈希標記、鏈接、標點符號等。

要去除這些,如果我們僅依賴于已經定義的字符,很難做到這些操作。因此,我們需要通過使用正則表達式(Regex)來匹配我們想要的術語的模式。

Regex 是一個特殊的字符串,它包含一個可以匹配與該模式相關聯的單詞的模式。通過使用名為 re. 的 Python 庫搜索或刪除這些模式。以下為實現代碼:

 

  1. import re 
  2. # 刪除提及 
  3. x = "@DDNewsLive @NitishKumar  and @ArvindKejriwal can't survive without referring @@narendramodi . Without Mr Modi they are BIG ZEROS" 
  4. x = re.sub("@\S+"" ", x) 
  5. print(x) 
  6. >>>      and   can't survive without referring   . Without Mr Modi they are BIG ZEROS 
  7. # 刪除 URL 鏈接 
  8. x = "Severe Thunderstorm pictures from across the Mid-South http://t.co/UZWLgJQzNS" 
  9. x = re.sub("https*\S+"" ", x) 
  10. print(x) 
  11. >>> Severe Thunderstorm pictures from across the Mid-South 
  12. # 刪除標簽 
  13. x = "Are people not concerned that after #SLAB's obliteration in Scotland #Labour UK is ripping itself apart over #Labourleadership contest?" 
  14. x = re.sub("#\S+"" ", x) 
  15. print(x) 
  16. >>> Are people not concerned that after   obliteration in Scotland   UK is ripping itself apart over   contest? 
  17. # 刪除記號和下一個字符 
  18. x = "Notley's tactful yet very direct response to Harper's attack on Alberta's gov't. Hell YEAH Premier! http://t.co/rzSUlzMOkX #ableg #cdnpoli" 
  19. x = re.sub("\'\w+", '', x) 
  20. print(x) 
  21. >>> Notley tactful yet very direct response to Harper attack on Alberta gov. Hell YEAH Premier! http://t.co/rzSUlzMOkX #ableg #cdnpoli 
  22. # 刪除標點符號 
  23. x = "In 2014 I will only smoke crqck if I becyme a mayor. This includes Foursquare." 
  24. x = re.sub('[%s]' % re.escape(string.punctuation), ' ', x) 
  25. print(x) 
  26. >>> In 2014 I will only smoke crqck if I becyme a mayor. This includes Foursquare. 
  27. # 刪除數字 
  28. x = "C-130 specially modified to land in a stadium and rescue hostages in Iran in 1980... http://t.co/tNI92fea3u http://t.co/czBaMzq3gL" 
  29. x = re.sub(r'\w*\d+\w*''', x) 
  30. print(x) 
  31. >>> C- specially modified to land in a stadium and rescue hostages in Iran in ... http://t.co/ http://t.co/ 
  32. #替換空格 
  33. x = "     and   can't survive without referring   . Without Mr Modi they are BIG ZEROS" 
  34. x = re.sub('\s{2,}'" ", x) 
  35. print(x) 
  36. >>>  and can't survive without referring . Without Mr Modi they are BIG ZEROS 

 

5.功能組合

在我們了解了文本預處理的每個步驟之后,讓我們將其應用于列表。如果仔細看這些步驟,你會發現其實每個方法都是相互關聯的。因此,必須將其應用于函數,以便我們可以按順序同時處理所有問題。在應用預處理步驟之前,以下是文本示例:

 

  1. Our Deeds are the Reason of this #earthquake May ALLAH Forgive us all 
  2. Forest fire near La Ronge Sask. Canada 
  3. All residents asked to 'shelter in place' are being notified by officers. No other evacuation or shelter in place orders are expected 
  4. 13,000 people receive #wildfires evacuation orders in California  
  5. Just got sent this photo from Ruby #Alaska as smoke from #wildfires pours into a school 

 

在預處理文本列表時,我們應先執行幾個步驟:

  • 創建包含所有預處理步驟的函數,并返回預處理的字符串
  • 使用名為"apply"的方法應用函數,并使用該方法將列表鏈接在一起。

代碼如下:

 

  1. # 導入錯誤的情況下 
  2. # ! pip install nltk 
  3. # ! pip install textblob 
  4. import numpy as np 
  5. import matplotlib.pyplot as plt 
  6. import pandas as pd 
  7. import re 
  8. import nltk 
  9. import string 
  10. from nltk.corpus import stopwords 
  11. # # 如果缺少語料庫 
  12. # 下載 all-nltk 
  13. nltk.download() 
  14. df = pd.read_csv('train.csv'
  15. stop_words = stopwords.words("english"
  16. wordnet = WordNetLemmatizer() 
  17. def text_preproc(x): 
  18.   x = x.lower() 
  19.   x = ' '.join([word for word in x.split(' ') if word not in stop_words]) 
  20.   x = x.encode('ascii''ignore').decode() 
  21.   x = re.sub(r'https*\S+'' ', x) 
  22.   x = re.sub(r'@\S+'' ', x) 
  23.   x = re.sub(r'#\S+'' ', x) 
  24.   x = re.sub(r'\'\w+''', x) 
  25.   x = re.sub('[%s]' % re.escape(string.punctuation), ' ', x) 
  26.   x = re.sub(r'\w*\d+\w*''', x) 
  27.   x = re.sub(r'\s{2,}'' ', x) 
  28.   return x 
  29. df['clean_text'] = df.text.apply(text_preproc) 

 

 

上面的文本預處理結果如下:

  1. deeds reason may allah forgive us 
  2. forest fire near la ronge sask canada 
  3. residents asked place notified officers evacuation shelter place orders expected 
  4.  people receive evacuation orders california  
  5. got sent photo ruby smoke pours school 

最后

以上內容就是使用 Python 進行文本預處理的具體步驟,希望能夠幫助大家用它來解決與文本數據相關的問題,提高文本數據的規范性以及模型的準確度。

責任編輯:華軒 來源: 今日頭條
相關推薦

2023-02-08 07:44:56

Pandas數據分析

2021-03-28 08:57:57

Python 文本數據

2011-04-08 14:45:08

文本數據Oracle

2020-07-10 09:49:53

數據清理數據分析查找異常

2025-05-14 13:23:19

數據模型AI

2023-06-11 17:00:06

2019-01-15 14:21:13

Python數據分析數據

2011-09-19 18:49:33

Vista

2017-11-03 12:57:06

機器學習文本數據Python

2024-05-23 08:48:21

2023-02-08 07:09:40

PythonChatGPT語言模型

2023-11-07 08:33:08

2018-03-27 18:12:12

PythonHTML

2016-11-16 15:05:42

情感分析

2024-06-05 09:17:31

Python數據清洗開發

2021-03-18 10:21:45

數據科學大數據機器學習

2021-12-02 09:00:00

數據庫NoSQLWeb

2022-06-27 17:40:14

大數據數據科學

2016-02-17 15:15:01

2020-11-02 08:15:00

Python數據開發
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 激情一区二区三区 | 亚洲一区二区中文字幕在线观看 | 毛片高清| 中文字幕在线观看 | 日韩中文字幕第一页 | 亚洲一区中文字幕 | 99久久精品国产一区二区三区 | 97视频人人澡人人爽 | 久久久高清 | 亚洲电影一区 | 亚洲国产精品va在线看黑人 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 | 日韩成人精品一区 | 精品99在线| 成人在线免费视频观看 | 国产精品久久久久久久久久尿 | 99精品视频网 | 毛片链接 | 国产九九九九 | 男人的天堂久久 | 亚洲福利在线视频 | 久久久网 | 中文字幕免费视频 | 午夜在线电影网 | 91在线精品视频 | 国产精品免费在线 | 免费在线观看一区二区 | 这里有精品| 亚洲一区二区在线播放 | 久久精品一二三影院 | www.国产日本 | 午夜影院官网 | 亚洲国产一 | 日日日视频 | 欧美一级黄视频 | av网站在线播放 | 国产在线精品一区二区三区 | 天天欧美 | 国产精品国产三级国产aⅴ中文 | 久久精品中文字幕 | 羞羞视频在线观看网站 |