Python实现一个文本处理系统,支持功能如下: 1. 接受用户输入的文本(必须>=5句话); 2. 排版文本格式并输出,每一个整句均换行显示; 3. 根据用户需求(用户输入)替换文本中的内容; 4. 根据用户需求(用户输入感兴趣的词),统计并输出词频,且打印该词出现在哪一句的什么位置; 5. 记录用户操作历史,保持文本和具体的操作对应(操作以功能序号记录即可),统计用户操作的文本数,每种功能使用的频率并排序。
时间: 2023-12-03 16:45:02 浏览: 58
好的,这个任务可以分为以下几个步骤来实现:
1. 接受用户输入的文本,并进行初步处理,确保输入的文本长度>=5句话;
2. 对文本进行排版,每一个整句均换行显示;
3. 根据用户需求,替换文本中的内容;
4. 根据用户需求,统计并输出感兴趣词的词频,并打印出该词出现在哪一句的什么位置;
5. 记录用户操作历史,统计用户操作的文本数、每种功能使用的频率并排序。
下面我将分别介绍如何实现这些功能。
### 1. 接受用户输入的文本
可以使用 Python 内置的 input() 函数来接受用户输入的文本,然后使用 split() 函数将文本按照句子分隔开,然后判断句子的数量是否>=5,如果不足5句,则提示用户重新输入。
```python
while True:
text = input("请输入文本(至少5句话):")
sentences = text.split("。")
if len(sentences) >= 5:
break
else:
print("输入的文本句子数量不足5句,请重新输入!")
```
### 2. 排版文本格式并输出
可以使用 for 循环遍历句子列表 sentences,然后依次输出每个句子,并在每个句子后面加上换行符。
```python
print("排版后的文本:")
for sentence in sentences:
print(sentence + "。")
```
### 3. 根据用户需求,替换文本中的内容
可以使用 Python 内置的 replace() 函数来实现替换功能,这个函数接受两个参数,第一个参数是要被替换的字符串,第二个参数是替换后的字符串。
```python
while True:
choice = input("是否需要替换文本中的内容?(是/否):")
if choice == "是":
old_word = input("请输入要替换的单词或短语:")
new_word = input("请输入替换后的单词或短语:")
text = text.replace(old_word, new_word)
sentences = text.split("。")
break
elif choice == "否":
break
else:
print("输入有误,请重新输入!")
```
### 4. 根据用户需求,统计并输出感兴趣词的词频,并打印出该词出现在哪一句的什么位置
可以使用 Python 内置的 count() 函数来统计感兴趣词的词频。首先让用户输入感兴趣的词,然后遍历每个句子,使用 split() 函数将句子按照空格分隔开,然后使用 count() 函数统计感兴趣词在句子中出现的次数。如果某个句子中出现了感兴趣词,则打印出该词出现在哪一句的什么位置。
```python
while True:
choice = input("是否需要统计感兴趣词的词频?(是/否):")
if choice == "是":
word = input("请输入感兴趣的词:")
count = 0
for i, sentence in enumerate(sentences):
if word in sentence:
pos = sentence.index(word)
count += sentence.count(word)
print(f"感兴趣词“{word}”出现在第{i+1}句,位置为{pos}。")
print(f"感兴趣词“{word}”在文本中出现了{count}次。")
break
elif choice == "否":
break
else:
print("输入有误,请重新输入!")
```
### 5. 记录用户操作历史,统计用户操作的文本数、每种功能使用的频率并排序
可以使用字典来记录用户操作历史,其中键是功能序号,值是一个列表,列表中存储了该功能被使用的次数和使用该功能时文本的内容。每次用户使用某个功能时,就将该功能序号和文本内容添加到字典中。最后,可以按照每种功能使用的频率进行排序,并输出统计结果。
```python
history = {}
text_count = 1
while True:
print("="*20)
print("请选择要执行的功能:")
print("1. 接受用户输入的文本")
print("2. 排版文本格式并输出")
print("3. 替换文本中的内容")
print("4. 统计感兴趣词的词频")
print("0. 退出")
choice = input("请输入功能序号:")
if choice == "1":
while True:
text = input("请输入文本(至少5句话):")
sentences = text.split("。")
if len(sentences) >= 5:
history.setdefault(choice, []).append((text_count, text))
text_count += 1
break
else:
print("输入的文本句子数量不足5句,请重新输入!")
elif choice == "2":
print("排版后的文本:")
for sentence in sentences:
print(sentence + "。")
history.setdefault(choice, []).append((text_count, text))
elif choice == "3":
while True:
old_word = input("请输入要替换的单词或短语:")
new_word = input("请输入替换后的单词或短语:")
if old_word.strip() and new_word.strip():
text = text.replace(old_word, new_word)
sentences = text.split("。")
history.setdefault(choice, []).append((text_count, text))
text_count += 1
break
else:
print("输入不能为空,请重新输入!")
elif choice == "4":
while True:
word = input("请输入感兴趣的词:")
if word.strip():
count = 0
for i, sentence in enumerate(sentences):
if word in sentence:
pos = sentence.index(word)
count += sentence.count(word)
print(f"感兴趣词“{word}”出现在第{i+1}句,位置为{pos}。")
print(f"感兴趣词“{word}”在文本中出现了{count}次。")
history.setdefault(choice, []).append((text_count, word))
text_count += 1
break
else:
print("输入不能为空,请重新输入!")
elif choice == "0":
break
else:
print("输入有误,请重新输入!")
print("用户操作历史:")
for i, (key, value) in enumerate(sorted(history.items(), key=lambda x: len(x[1]), reverse=True)):
print(f"{i+1}. 功能{key} 共操作了{len(value)}次")
print(f"共输入了{text_count-1}段文本")