使用Python函数处理文本数据:开发Python文本处理应用的10个函数
1. 函数名称:读取文本文件
函数功能:读取指定路径下的文本文件内容,并将内容返回为字符串。
函数参数:文件路径
返回值:文本内容的字符串
def read_text_file(file_path):
with open(file_path, 'r') as file:
text = file.read()
return text
2. 函数名称:统计单词数量
函数功能:接受一个字符串作为参数,返回该字符串中单词的数量。
函数参数:字符串
返回值:字符串中单词的数量
def count_words(text):
words = text.split()
return len(words)
3. 函数名称:统计字符数量
函数功能:接受一个字符串作为参数,返回该字符串中字符的数量。
函数参数:字符串
返回值:字符串中字符的数量
def count_characters(text):
return len(text)
4. 函数名称:查找替换
函数功能:接受一个字符串、要查找的单词和替换的单词作为参数,并返回替换后的字符串。
函数参数:字符串,要查找的单词,替换的单词
返回值:替换后的字符串
def replace_word(text, old_word, new_word):
return text.replace(old_word, new_word)
5. 函数名称:删除标点符号
函数功能:接受一个字符串作为参数,返回删除标点符号后的字符串。
函数参数:字符串
返回值:删除标点符号后的字符串
import string
def remove_punctuation(text):
return text.translate(str.maketrans('', '', string.punctuation))
6. 函数名称:统计单词频率
函数功能:接受一个字符串作为参数,返回每个单词及其出现的频率。
函数参数:字符串
返回值:字典,键为单词,值为频率
from collections import Counter
def count_word_frequency(text):
words = text.split()
word_freq = Counter(words)
return word_freq
7. 函数名称:提取关键词
函数功能:接受一个字符串作为参数,返回该字符串中出现频率最高的前n个关键词。
函数参数:字符串,关键词数量n
返回值:包含关键词的列表
from collections import Counter
def extract_keywords(text, n):
words = text.split()
word_freq = Counter(words)
top_n_words = word_freq.most_common(n)
return [word[0] for word in top_n_words]
8. 函数名称:统计句子数量
函数功能:接受一个字符串作为参数,返回该字符串中句子的数量。
函数参数:字符串
返回值:句子的数量
import re
def count_sentences(text):
sentences = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', text)
return len(sentences)
9. 函数名称:转换为大写
函数功能:接受一个字符串作为参数,返回转换为大写后的字符串。
函数参数:字符串
返回值:转换为大写后的字符串
def convert_to_uppercase(text):
return text.upper()
10. 函数名称:提取URL链接
函数功能:接受一个字符串作为参数,返回该字符串中的URL链接列表。
函数参数:字符串
返回值:URL链接的列表
import re
def extract_urls(text):
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', text)
return urls
