由用户输入文件路径,读取对应文件中的文本进行操作,将用户操作历史写入文件存储实现一个文本处理系统,支持功能如下: 1. 接受用户输入的文本(必须>=5句话); 2. 排版文本格式并输出,每一个整句均换行显示; 3. 根据用户需求(用户输入)替换文本中的内容; 4. 根据用户需求(用户输入感兴趣的词),统计并输出词频,且打印该词出现在哪一句的什么位置; 5. 记录用户操作历史,保持文本和具体的操作对应(操作以功能序号记录即可),统计用户操作的文本数,每种功能使用的频率并排序。
时间: 2023-12-06 08:39:43 浏览: 46
好的,这个任务可以分成以下几个步骤来实现:
1. 接收用户输入的文件路径并读取文件中的文本。
2. 将文本排版并输出每一个整句。
3. 根据用户需求替换文本中的内容。
4. 根据用户需求统计词频并输出。
5. 记录用户操作历史,统计并输出操作频率。
下面我们一步一步来实现。
首先,我们需要导入必要的库:
```python
import os
import re
from collections import Counter
```
然后,我们可以定义一些常量,如最小句子长度、操作记录文件名称等:
```python
MIN_SENTENCE_LENGTH = 5
HISTORY_FILENAME = 'history.txt'
```
接下来,我们可以定义一个函数来读取文件中的文本:
```python
def read_file(filename):
if not os.path.exists(filename):
print(f'Error: {filename} does not exist.')
return None
with open(filename, 'r') as f:
text = f.read()
return text
```
该函数会检查文件是否存在,如果不存在则返回None,否则将文件中的文本读取并返回。
然后,我们可以定义一个函数来排版文本并输出每一个整句:
```python
def format_text(text):
sentences = re.split(r'[.!?]', text)
formatted_text = '\n'.join(sentence.strip() for sentence in sentences if len(sentence) >= MIN_SENTENCE_LENGTH)
print(formatted_text)
return formatted_text
```
该函数会使用正则表达式将文本分割成句子,然后过滤掉长度小于5的句子,最后将每个整句按照换行符连接起来并输出。该函数还会返回格式化后的文本。
接下来,我们可以定义一个函数来替换文本中的内容:
```python
def replace_text(text, old_word, new_word):
new_text = text.replace(old_word, new_word)
return new_text
```
该函数会将文本中的旧词替换为新词,并返回替换后的文本。
然后,我们可以定义一个函数来统计词频并输出:
```python
def count_words(text, word):
sentences = re.split(r'[.!?]', text)
word_positions = []
word_count = 0
for i, sentence in enumerate(sentences):
words = re.findall(r'\b\w+\b', sentence)
for j, w in enumerate(words):
if w.lower() == word.lower():
word_positions.append((i+1, j+1))
word_count += 1
print(f'The word "{word}" appears {word_count} times in the text.')
for pos in word_positions:
print(f' Sentence {pos[0]}, position {pos[1]}')
```
该函数会使用正则表达式将文本分割成句子,并使用findall函数找出每个句子中的单词。然后遍历每个单词,如果发现和用户输入的单词相同,则记录该单词出现的位置。最后输出单词出现的次数和位置。
最后,我们可以定义一个函数来记录用户操作历史:
```python
def write_history(filename, operation):
with open(filename, 'a') as f:
f.write(operation + '\n')
def print_history(filename):
if not os.path.exists(filename):
print('No history found.')
return
with open(filename, 'r') as f:
history = f.readlines()
operation_count = Counter(history)
unique_operations = list(set(history))
sorted_operations = sorted(unique_operations, key=lambda x: -operation_count[x])
print(f'Total number of operations: {len(history)}')
for op in sorted_operations:
count = operation_count[op]
freq = count / len(history)
print(f'{op.strip()}: {count} ({freq:.2%})')
```
该函数会将每个操作记录写入操作记录文件中,并提供一个函数来读取并输出操作记录的统计信息。统计信息包括操作次数、每个操作的使用频率等。
最后,我们可以将所有的函数组合起来,编写一个主程序来实现整个文本处理系统:
```python
def main():
filename = input('Enter the file path: ')
text = read_file(filename)
if text is not None:
print('Original text:')
formatted_text = format_text(text)
while True:
operation = input('Enter operation (1: replace, 2: count, q: quit): ')
if operation == 'q':
break
elif operation == '1':
old_word = input('Enter the word to replace: ')
new_word = input('Enter the new word: ')
text = replace_text(text, old_word, new_word)
formatted_text = format_text(text)
write_history(HISTORY_FILENAME, '1')
elif operation == '2':
word = input('Enter the word to count: ')
count_words(formatted_text, word)
write_history(HISTORY_FILENAME, '2')
print_history(HISTORY_FILENAME)
if __name__ == '__main__':
main()
```
该程序会首先接收用户输入的文件路径,并读取文件中的文本。如果读取成功,则输出原始文本,并进入一个循环中,每次循环会提示用户输入操作编号。如果用户输入q,则退出循环。如果用户输入1,则要求用户输入要替换的单词和新单词,并将文本中的旧词替换为新词,然后输出格式化后的文本,并记录操作历史。如果用户输入2,则要求用户输入要统计的单词,并统计该单词在文本中出现的次数和位置,并记录操作历史。最后,程序会输出所有的操作历史记录及其使用频率。
以上就是一个简单的文本处理系统的实现。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)