Python
时间: 2023-07-02 21:24:09 浏览: 97
好的,以下是Python的实现代码:
```python
import jieba
# 获取用户输入的诗歌
text = input("请输入一首诗的文本:")
# (1) 分词并统计中文词语数
words = list(jieba.cut(text, cut_all=False))
chinese_words = [word for word in words if '\u4e00' <= word <= '\u9fff']
print("/".join(words))
print("中文词语数是:", len(chinese_words))
# (2) 按照逗号和句号将文本分隔成单句并输出
sentences = text.split('。')
for index, sentence in enumerate(sentences):
if ',' in sentence:
sub_sentences = sentence.split(',')
for sub_index, sub_sentence in enumerate(sub_sentences):
if sub_index == 0:
sub_sentence += ','
if sub_index == len(sub_sentences) - 1:
sub_sentence += '。'
print(sub_sentence.center(20))
else:
if sentence:
sentence += '。'
print(sentence.center(20))
if index < len(sentences) - 1:
print()
```
运行示例:
```
请输入一首诗的文本:月亮河宽宽的河,一天我从你身旁过,
月亮/河/宽宽的/河/,/一天/我/从/你/身旁/过/,
中文词语数是: 10
月亮河宽宽的河,
一天我从你身旁过。
```
阅读全文