偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Python 文本清洗和預處理的 15 項技術

開發(fā)
本文詳細介紹了15項Python文本清洗和預處理技術,通過實際代碼示例,我們展示了如何應用這些技術來清洗和預處理文本數(shù)據(jù)。

文本清洗和預處理是自然語言處理(NLP)中的重要步驟。無論你是處理社交媒體數(shù)據(jù)、新聞文章還是用戶評論,都需要先對文本進行清洗和預處理,以確保后續(xù)的分析或建模能夠順利進行。本文將詳細介紹15項Python文本清洗和預處理技術,并通過實際代碼示例來幫助你更好地理解和應用這些技術。

1. 去除空白字符

空白字符包括空格、制表符、換行符等,這些字符通常不會影響文本內(nèi)容的意義,但會增加數(shù)據(jù)的復雜性。使用 strip() 和 replace() 方法可以輕松去除這些字符。

text = "  Hello, World! \n"
clean_text = text.strip()  # 去除首尾空白字符
print(clean_text)  # 輸出: Hello, World!

text_with_tabs = "Hello\tWorld!"
clean_text = text_with_tabs.replace("\t", " ")  # 將制表符替換為空格
print(clean_text)  # 輸出: Hello World!

2. 轉(zhuǎn)換為小寫

將所有文本轉(zhuǎn)換為小寫可以避免因大小寫不同而引起的不一致問題。

text = "Hello, World!"
lower_text = text.lower()
print(lower_text)  # 輸出: hello, world!

3. 去除標點符號

標點符號通常不會對文本的語義產(chǎn)生實質(zhì)性的影響,但在某些情況下(如情感分析)可能會有影響。使用 string 模塊中的 punctuation 可以輕松去除標點符號。

import string

text = "Hello, World!"
clean_text = text.translate(str.maketrans("", "", string.punctuation))
print(clean_text)  # 輸出: Hello World

4. 分詞

分詞是將文本分割成單詞或短語的過程。使用 nltk 庫的 word_tokenize 方法可以實現(xiàn)這一點。

import nltk
from nltk.tokenize import word_tokenize

nltk.download('punkt')
text = "Hello, World! This is a test."
tokens = word_tokenize(text)
print(tokens)  # 輸出: ['Hello', ',', 'World', '!', 'This', 'is', 'a', 'test', '.']

5. 去除停用詞

停用詞是那些在文本中頻繁出現(xiàn)但對語義貢獻不大的詞匯,如“the”、“is”等。使用 nltk 庫的 stopwords 模塊可以去除這些詞。

from nltk.corpus import stopwords

nltk.download('stopwords')
stop_words = set(stopwords.words('english'))
tokens = ['Hello', 'World', 'This', 'is', 'a', 'test']
filtered_tokens = [token for token in tokens if token not in stop_words]
print(filtered_tokens)  # 輸出: ['Hello', 'World', 'test']

6. 詞干提取

詞干提取是將單詞還原為其基本形式的過程。使用 nltk 庫的 PorterStemmer 可以實現(xiàn)這一點。

from nltk.stem import PorterStemmer

stemmer = PorterStemmer()
words = ['running', 'jumps', 'easily']
stemmed_words = [stemmer.stem(word) for word in words]
print(stemmed_words)  # 輸出: ['run', 'jump', 'easili']

7. 詞形還原

詞形還原是將單詞還原為其詞典形式的過程。使用 nltk 庫的 WordNetLemmatizer 可以實現(xiàn)這一點。

from nltk.stem import WordNetLemmatizer

nltk.download('wordnet')
lemmatizer = WordNetLemmatizer()
words = ['running', 'jumps', 'easily']
lemmatized_words = [lemmatizer.lemmatize(word) for word in words]
print(lemmatized_words)  # 輸出: ['running', 'jump', 'easily']

8. 去除數(shù)字

數(shù)字通常不會對文本的語義產(chǎn)生實質(zhì)性的影響。使用正則表達式可以輕松去除數(shù)字。

import re

text = "Hello, World! 123"
clean_text = re.sub(r'\d+', '', text)
print(clean_text)  # 輸出: Hello, World! 

9. 去除特殊字符

特殊字符如 @、#、$ 等通常不會對文本的語義產(chǎn)生實質(zhì)性的影響。使用正則表達式可以輕松去除這些字符。

text = "Hello, @World! #Python $123"
clean_text = re.sub(r'[^\w\s]', '', text)
print(clean_text)  # 輸出: Hello  World  Python 123

10. 去除 HTML 標簽

如果文本來自網(wǎng)頁,可能包含 HTML 標簽。使用 BeautifulSoup 庫可以輕松去除這些標簽。

from bs4 import BeautifulSoup

html_text = "<html><body><h1>Hello, World!</h1></body></html>"
soup = BeautifulSoup(html_text, 'html.parser')
clean_text = soup.get_text()
print(clean_text)  # 輸出: Hello, World!

11. 去除 URL

URL 通常不會對文本的語義產(chǎn)生實質(zhì)性的影響。使用正則表達式可以輕松去除 URL。

text = "Check out this link: https://example.com"
clean_text = re.sub(r'http\S+|www.\S+', '', text)
print(clean_text)  # 輸出: Check out this link: 

12. 去除重復單詞

重復單詞可能會增加文本的復雜性。使用集合可以輕松去除重復單詞。

tokens = ['Hello', 'World', 'Hello', 'Python', 'Python']
unique_tokens = list(set(tokens))
print(unique_tokens)  # 輸出: ['Hello', 'Python', 'World']

13. 去除短詞

短詞通常不會對文本的語義產(chǎn)生實質(zhì)性的影響??梢栽O置一個閾值來去除長度小于該閾值的單詞。

tokens = ['Hello', 'World', 'a', 'is', 'Python']
min_length = 3
filtered_tokens = [token for token in tokens if len(token) >= min_length]
print(filtered_tokens)  # 輸出: ['Hello', 'World', 'Python']

14. 去除罕見詞

罕見詞可能會增加文本的復雜性??梢栽O置一個頻率閾值來去除出現(xiàn)次數(shù)少于該閾值的單詞。

from collections import Counter

tokens = ['Hello', 'World', 'Hello', 'Python', 'Python', 'test', 'test', 'test']
word_counts = Counter(tokens)
min_frequency = 2
filtered_tokens = [token for token in tokens if word_counts[token] >= min_frequency]
print(filtered_tokens)  # 輸出: ['Hello', 'Hello', 'Python', 'Python', 'test', 'test', 'test']

15. 使用正則表達式進行復雜清洗

正則表達式是一種強大的工具,可以用于復雜的文本清洗任務。例如,去除特定模式的字符串。

text = "Hello, World! 123-456-7890"
clean_text = re.sub(r'\d{3}-\d{3}-\d{4}', 'PHONE', text)
print(clean_text)  # 輸出: Hello, World! PHONE

實戰(zhàn)案例:清洗社交媒體評論

假設你有一個包含社交媒體評論的數(shù)據(jù)集,需要對其進行清洗和預處理。我們將綜合運用上述技術來完成這個任務。

import pandas as pd
import re
import string
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from bs4 import BeautifulSoup

# 下載必要的NLTK資源
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')

# 示例數(shù)據(jù)
data = {
    'comment': [
        "Check out this link: https://example.com",
        "Hello, @World! #Python $123",
        "<html><body><h1>Hello, World!</h1></body></html>",
        "Running jumps easily 123-456-7890"
    ]
}

df = pd.DataFrame(data)

def clean_text(text):
    # 去除HTML標簽
    text = BeautifulSoup(text, 'html.parser').get_text()
    
    # 去除URL
    text = re.sub(r'http\S+|www.\S+', '', text)
    
    # 去除特殊字符
    text = re.sub(r'[^\w\s]', '', text)
    
    # 去除數(shù)字
    text = re.sub(r'\d+', '', text)
    
    # 轉(zhuǎn)換為小寫
    text = text.lower()
    
    # 分詞
    tokens = word_tokenize(text)
    
    # 去除停用詞
    stop_words = set(stopwords.words('english'))
    tokens = [token for token in tokens if token not in stop_words]
    
    # 詞形還原
    lemmatizer = WordNetLemmatizer()
    tokens = [lemmatizer.lemmatize(token) for token in tokens]
    
    # 去除短詞
    tokens = [token for token in tokens if len(token) >= 3]
    
    # 去除罕見詞
    word_counts = Counter(tokens)
    min_frequency = 2
    tokens = [token for token in tokens if word_counts[token] >= min_frequency]
    
    return ' '.join(tokens)

# 應用清洗函數(shù)
df['cleaned_comment'] = df['comment'].apply(clean_text)
print(df)

總結(jié)

本文詳細介紹了15項Python文本清洗和預處理技術,包括去除空白字符、轉(zhuǎn)換為小寫、去除標點符號、分詞、去除停用詞、詞干提取、詞形還原、去除數(shù)字、去除特殊字符、去除HTML標簽、去除URL、去除重復單詞、去除短詞、去除罕見詞以及使用正則表達式進行復雜清洗。通過實際代碼示例,我們展示了如何應用這些技術來清洗和預處理文本數(shù)據(jù)。最后,我們通過一個實戰(zhàn)案例,綜合運用這些技術對社交媒體評論進行了清洗和預處理。

責任編輯:趙寧寧 來源: 手把手PythonAI編程
相關推薦

2021-03-28 08:57:57

Python 文本數(shù)據(jù)

2020-12-23 11:08:10

Python代碼文本

2019-02-22 08:25:19

數(shù)據(jù)清洗預處理機器學習

2021-11-03 09:00:00

深度學習自然語言機器學習

2020-11-06 17:20:14

PythonBAT代碼

2024-12-03 16:39:41

2017-08-24 09:35:06

深度學習向量化Hash Trick

2018-03-13 12:51:12

Python數(shù)據(jù)函數(shù)

2018-06-07 15:58:52

Python函數(shù)數(shù)據(jù)

2024-05-15 15:27:39

2024-01-03 16:01:23

2016-12-20 16:07:13

Python數(shù)據(jù)預處理

2019-01-28 17:42:33

Python數(shù)據(jù)預處理數(shù)據(jù)標準化

2016-12-18 15:03:57

Python Scikit Lea數(shù)據(jù)

2021-07-17 22:41:53

Python數(shù)據(jù)技術

2020-04-29 16:49:33

機器學習人工智能計算機

2025-03-07 08:00:00

數(shù)據(jù)數(shù)據(jù)集集神經(jīng)網(wǎng)絡數(shù)據(jù)預處理

2024-01-31 08:09:53

預處理器代碼C++

2009-08-07 17:41:40

C#預處理

2009-08-07 17:45:29

C#預處理
點贊
收藏

51CTO技術棧公眾號