You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于句首起始词在Python中实现句子拆分的技术咨询

基于句首词的句子分类实现方案

核心思路

  • 先将长文本拆分为独立句子
  • 对每个句子去除首尾空白后提取首个单词,按首词分类存入对应变量

可直接运行的Python代码

import re

# 待处理文本(可替换为自己的文本)
raw_text = """
Consider a thin‐walled, metallic tube of length L = 1 m and inside diameter Di = 3 mm. Water enters the tube at ˙ m = 0.015 kg / s m˙=0.015 kg/s and Tm,i = 97°C. a. What is the outlet temperature of the water if the tube surface temperature is maintained at 27°C? ( b. If a 0.5‐mm‐thick layer of insulation of k = 0.05 W/m-K is applied to the tube and its outer surface is maintained at 27°C, what is the outlet temperature of the water? c. If the outer surface of the insulation is no longer maintained at 27°C but is allowed to exchange heat by free convection with ambient air at 27°C, what is the outlet temperature of the water? The free convection heat transfer coefficient is 5 W/m2 -K.
"""

# 步骤1:按句子结束标点拆分长文本为单个句子
sentences = re.split(r'[.?!]\s*', raw_text)

# 步骤2:初始化存储变量
find_sentences = []
consider_sentences = []

# 步骤3:遍历分类
for sent in sentences:
    # 去除首尾空白,跳过空句子
    clean_sent = sent.strip()
    if not clean_sent:
        continue
    # 提取首词
    first_word = clean_sent.split()[0]
    if first_word == 'Find':
        find_sentences.append(clean_sent)
    elif first_word == 'Consider':
        consider_sentences.append(clean_sent)

# 测试输出
print("Consider开头的句子:", consider_sentences)
print("Find开头的句子:", find_sentences)

关键细节说明

  • 用re.split(r'[.?!]\s*', raw_text)拆分句子,可以匹配以句号、问号、感叹号结尾的所有句子,自动忽略标点后的空白字符
  • 提取首词前先调用strip()去除句子首尾的换行、空格,避免因前导空白导致首词提取错误
  • 如果需要保留句子末尾的标点,可调整正则拆分规则为re.split(r'(?<=[.?!])\s*', raw_text)即可

示例文本翻译(对应Consider分类下的内容)

现有一根长度L=1m、内径Dᵢ=3mm的薄壁金属管。水以0.015kg/s的质量流量、97℃的入口温度流入管内。
a. 若管壁温度恒定维持在27℃,求水的出口温度?
b. 若在管壁外包裹一层厚度0.5mm、导热系数k=0.05W/(m·K)的保温层,且保温层外表面温度恒定维持在27℃,求水的出口温度?
c. 若保温层外表面不再维持27℃,而是和27℃的环境空气发生自然对流换热,自然对流换热系数为5W/(m²·K),求水的出口温度?

内容的提问来源于stack exchange,提问作者Asit Singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.16 22:24:05