编写一个程序,接受用户输入的文本,并分析以下内容: 提取所有的单词,并统计每个单词的出现次数 查找并提取所有的价格(格式为¥XX.XX) 替换所有的数子为#符号
时间: 2024-10-15 08:23:45 浏览: 55
编写这样一个程序可以分为三个步骤:
1. **接收用户输入**:
```python
input_text = input("请输入文本:")
```
2. **文本预处理**:
- **分词并计数**:
```python
from collections import defaultdict
word_list = input_text.split()
word_count = defaultdict(int)
for word in word_list:
word_count[word] += 1
```
- **查找价格**:
```python
price_pattern = r"¥\d+\.\d+"
prices = re.findall(price_pattern, input_text)
```
- **替换数字**:
```python
import re
numbers = re.findall(r'\d+', input_text)
for num in numbers:
input_text = input_text.replace(str(num), "#")
```
3. **显示结果**:
- 显示单词频率:
```python
print("单词及其出现次数:")
for word, count in word_count.items():
print(f"{word}: {count}")
```
- 显示找到的价格:
```python
print("\n找到的价格:")
for price in prices:
print(price)
```
完整代码示例(Python):
```python
import re
from collections import defaultdict
def analyze_text():
input_text = input("请输入文本:")
# 分词并计数
word_list = input_text.split()
word_count = defaultdict(int)
for word in word_list:
word_count[word] += 1
# 查找价格
price_pattern = r"¥\d+\.\d+"
prices = re.findall(price_pattern, input_text)
# 替换数字
numbers = re.findall(r'\d+', input_text)
for num in numbers:
input_text = input_text.replace(str(num), "#")
print("单词及其出现次数:")
for word, count in word_count.items():
print(f"{word}: {count}")
print("\n找到的价格:")
for price in prices:
print(price)
analyze_text()
```
阅读全文
相关推荐


















