1.小寫化
將文本轉換為小寫?text.lower()
2.去除標點符號
使用正則表達式去除文本中的標點符號。
3.去除多余空格
將多個空格替換為單個空格囊卜,并去除首尾空格 ' '.join(text.split())
4.去除數(shù)字
用正則表達式去除文本中的數(shù)字半火。
5.替換縮寫
將常見的縮寫替換為完整形式越妈。text = text.replace("I'm","I am")
結合以上方法,對文本進行系統(tǒng)的正則化處理钮糖。
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from bs4 import BeautifulSoup
def normalize_text(text):
? ? # 移除HTML標簽
? ? soup = BeautifulSoup(text, "html.parser")
? ? text = soup.get_text()
? ? # 轉換為小寫
? ? text = text.lower()
? ? # 去除標點符號
? ? text = re.sub(r'[^\w\s]', ' ', text)
? ? # 去除多余空格
? ? text = ' '.join(text.split())
? ? # 去除數(shù)字
? ? text = re.sub(r'\d+', '', text)
? ? # 去除停用詞
? ? stop_words = set(stopwords.words('english'))
? ? word_tokens = word_tokenize(text)
? ? text = ' '.join(word for word in word_tokens if word not in stop_words)
? ? # 詞形還原
? ? lemmatizer = WordNetLemmatizer()
? ? word_tokens = word_tokenize(text)
? ? text = ' '.join(lemmatizer.lemmatize(word) for word in word_tokens)
? ? return text
# 示例文本
text = "<html><body><h1>Example Title</h1><p>This is an example sentence, with numbers 123 and HTML tags!</p></body></html>"
normalized_text = normalize_text(text)
print(normalized_text)? # 輸出: "example title example sentence number html tag"
正則表達式的常見用法
#### 1. 匹配任意字符
`.`: 匹配任意單個字符(除了換行符)梅掠。
import re
result = re.findall(r'a.b', 'aab abb acb adb')
print(result)? # 輸出: ['aab', 'abb', 'acb', 'adb']
#### 2. 字符類
- `[abc]`: 匹配字符 'a', 'b' 或 'c'。
- `[a-z]`: 匹配任何小寫字母店归。
- `[A-Z]`: 匹配任何大寫字母阎抒。
- `[0-9]`: 匹配任何數(shù)字。
- `[^abc]`: 匹配除 'a', 'b', 'c' 之外的任意字符消痛。
result = re.findall(r'[a-c]', 'abcxyz')
print(result)? # 輸出: ['a', 'b', 'c']
#### 3. 預定義字符類
- `\d`: 匹配任何數(shù)字且叁,等價于 `[0-9]`。
- `\D`: 匹配任何非數(shù)字字符秩伞。
- `\w`: 匹配任何字母逞带、數(shù)字或下劃線,等價于 `[a-zA-Z0-9_]`纱新。
- `\W`: 匹配任何非字母展氓、數(shù)字、下劃線字符脸爱。
- `\s`: 匹配任何空白字符(空格遇汞、制表符、換行符)簿废。
- `\S`: 匹配任何非空白字符空入。
result = re.findall(r'\d+', 'There are 123 apples and 45 bananas')
print(result)? # 輸出: ['123', '45']
#### 4. 邊界匹配
- `^`: 匹配字符串的開頭。
- `$`: 匹配字符串的結尾族檬。
- `\b`: 匹配單詞邊界执庐。
- `\B`: 匹配非單詞邊界。
```python
result = re.findall(r'\bword\b', 'a word in a sentence')
print(result)? # 輸出: ['word']
#### 5. 量詞
- `*`: 匹配前面的字符零次或多次导梆。
- `+`: 匹配前面的字符一次或多次。
- `?`: 匹配前面的字符零次或一次迂烁。
- `{n}`: 匹配前面的字符恰好 n 次看尼。
- `{n,}`: 匹配前面的字符至少 n 次。
- `{n,m}`: 匹配前面的字符至少 n 次盟步,但不超過 m 次藏斩。
result = re.findall(r'\d{2,4}', '123 1234 12345')
print(result)? # 輸出: ['123', '1234', '1234']
#### 6. 分組和捕獲
- `()`: 用于分組和捕獲匹配的子字符串。
result = re.findall(r'(\d+)-(\d+)-(\d+)', '123-456-7890')
print(result)? # 輸出: [('123', '456', '7890')]
#### 7. 或運算
- `|`: 表示“或”運算却盘,匹配符號前后任意一個正則表達式狰域。
result = re.findall(r'apple|orange', 'I like apple and orange')
print(result)? # 輸出: ['apple', 'orange']
#### 8. 轉義字符
- `\`: 用于轉義元字符媳拴,使其作為普通字符使用。
result = re.findall(r'\$[0-9]+', 'The price is $100')
print(result)? # 輸出: ['$100']
### 綜合示例
結合多個正則表達式操作來處理文本兆览。
import re
text = "Hello, World! This is a test. 123-456-7890. Email: test@example.com"
# 1. 小寫化
text = text.lower()
# 2. 去除標點符號
text = re.sub(r'[^\w\s]', ' ', text)
# 3. 去除多余空格
text = ' '.join(text.split())
# 4. 去除數(shù)字
text = re.sub(r'\d+', '', text)
# 5. 找出所有單詞
words = re.findall(r'\b\w+\b', text)
print(text)? # 輸出: "hello world this is a test email test example com"
print(words)? # 輸出: ['hello', 'world', 'this', 'is', 'a', 'test', 'email', 'test', 'example', 'com']
通過掌握這些常見的正則表達式操作屈溉,您可以有效地處理和規(guī)范化文本數(shù)據(jù)。