Python实现一个文本处理系统,支持功能如下: 1. 接受用户输入的文本(必须>=5句话); 2. 排版文本格式并输出,每一个整句均换行显示; 3. 根据用户需求(用户输入)替换文本中的内容; 4. 根据用户需求(用户输入感兴趣的词),统计并输出词频,且打印该词出现在哪一句的什么位置; 5. 记录用户操作历史,保持文本和具体的操作对应(操作以功能序号记录即可),统计用户操作的文本数,每种功能使用的频率并排序。
时间: 2023-12-03 09:44:57 浏览: 46
好的,这是一个比较复杂的项目,需要分步骤实现。我们可以先从最基础的部分开始,逐步实现每个功能。下面是一个可能的实现过程:
1. 接受用户输入的文本
```python
text = input("请输入文本(至少5句话):")
sentences = text.split("。") # 将文本按句子分割
if len(sentences) < 5:
print("输入文本不符合要求,请重新输入。")
else:
for i, s in enumerate(sentences):
print(f"{i+1}. {s}。") # 输出排版后的文本
```
2. 排版文本格式并输出
```python
def format_text(sentences):
formatted = ""
for i, s in enumerate(sentences):
formatted += f"{i+1}. {s}。\n"
return formatted
text = input("请输入文本(至少5句话):")
sentences = text.split("。")
if len(sentences) < 5:
print("输入文本不符合要求,请重新输入。")
else:
formatted_text = format_text(sentences)
print(formatted_text)
```
3. 根据用户需求替换文本中的内容
```python
def replace_text(sentences, old_word, new_word):
replaced = []
for s in sentences:
replaced.append(s.replace(old_word, new_word))
return replaced
text = input("请输入文本(至少5句话):")
sentences = text.split("。")
if len(sentences) < 5:
print("输入文本不符合要求,请重新输入。")
else:
formatted_text = format_text(sentences)
print(formatted_text)
choice = input("是否需要替换文本?(y/n)")
if choice == "y":
old_word = input("请输入需要替换的单词或短语:")
new_word = input("请输入替换后的单词或短语:")
replaced_sentences = replace_text(sentences, old_word, new_word)
replaced_text = format_text(replaced_sentences)
print(replaced_text)
```
4. 根据用户需求统计词频
```python
def count_word_freq(sentences, word):
freq = 0
positions = []
for i, s in enumerate(sentences):
words = s.split(" ")
if word in words:
freq += words.count(word)
positions.append((i+1, words.index(word)+1))
return freq, positions
text = input("请输入文本(至少5句话):")
sentences = text.split("。")
if len(sentences) < 5:
print("输入文本不符合要求,请重新输入。")
else:
formatted_text = format_text(sentences)
print(formatted_text)
choice = input("是否需要替换文本?(y/n)")
if choice == "y":
old_word = input("请输入需要替换的单词或短语:")
new_word = input("请输入替换后的单词或短语:")
replaced_sentences = replace_text(sentences, old_word, new_word)
replaced_text = format_text(replaced_sentences)
print(replaced_text)
sentences = replaced_sentences # 更新句子列表
choice = input("是否需要统计词频?(y/n)")
if choice == "y":
word = input("请输入要统计的单词或短语:")
freq, positions = count_word_freq(sentences, word)
print(f"{word}出现的次数为:{freq}")
if freq > 0:
print(f"{word}出现的位置为:")
for p in positions:
print(f"第{p[0]}句,第{p[1]}个词")
```
5. 记录用户操作历史并统计使用频率
```python
def log_operation(history, op_num, text):
history.append((op_num, text))
return history
def count_operation_freq(history):
freq_dict = {}
for h in history:
op_num = h[0]
if op_num in freq_dict:
freq_dict[op_num] += 1
else:
freq_dict[op_num] = 1
sorted_freq = sorted(freq_dict.items(), key=lambda x: x[1], reverse=True)
return sorted_freq
text = input("请输入文本(至少5句话):")
sentences = text.split("。")
if len(sentences) < 5:
print("输入文本不符合要求,请重新输入。")
else:
formatted_text = format_text(sentences)
print(formatted_text)
history = []
while True:
print("请选择要进行的操作:")
print("1. 排版文本并输出")
print("2. 替换文本")
print("3. 统计词频")
print("4. 查看操作历史")
print("5. 退出程序")
op_num = input()
if op_num == "1":
print(formatted_text)
history = log_operation(history, 1, text)
elif op_num == "2":
old_word = input("请输入需要替换的单词或短语:")
new_word = input("请输入替换后的单词或短语:")
replaced_sentences = replace_text(sentences, old_word, new_word)
replaced_text = format_text(replaced_sentences)
print(replaced_text)
history = log_operation(history, 2, f"{old_word} -> {new_word}")
sentences = replaced_sentences # 更新句子列表
elif op_num == "3":
word = input("请输入要统计的单词或短语:")
freq, positions = count_word_freq(sentences, word)
print(f"{word}出现的次数为:{freq}")
if freq > 0:
print(f"{word}出现的位置为:")
for p in positions:
print(f"第{p[0]}句,第{p[1]}个词")
history = log_operation(history, 3, word)
elif op_num == "4":
print("操作历史:")
for i, h in enumerate(history):
print(f"{i+1}. {h[0]}: {h[1]}")
sorted_freq = count_operation_freq(history)
print("操作使用频率:")
for f in sorted_freq:
print(f"{f[0]}: {f[1]}")
history = log_operation(history, 4, "")
elif op_num == "5":
break
else:
print("无效的操作编号,请重新选择。")
```
以上是一个可能的实现过程,其中可能还有改进的空间,可以根据具体需求进行优化。希望可以帮到你~
阅读全文