北省 松滋市 八宝镇 丝线湖村 五组

代码示例:

tags = ['B-prov', 'I-prov', 'E-prov', 'B-district', 'I-district', 'E-district', 'B-town', 'I-town', 'E-town', 'B-community', 'I-community', 'E-community', 'B-village_group', 'E-village_group']
text = '北省松滋市八宝镇丝线湖村五组'
result = []
start = 0
for i in range(len(text)):
    if text[i] == ' ':
        continue
    if tags[i][0] == 'B':
        if i != 0:
            result.append(text[start:i])
        start = i
    elif tags[i][0] == 'E':
        result.append(text[start:i+1])
        start = i+1
    elif tags[i][0] == 'I':
        if tags[i-1][0] != 'I':
            start = i
if start < len(text):
    result.append(text[start:])
print(' '.join(result))
``
根据标签提取词的代码例如湖 B-prov北 I-prov省 E-prov松 B-district滋 I-district市 E-district八 B-town宝 I-town镇 E-town丝 B-community线 I-community潮 I-community村 E-community五 B-village_group组 E-village_group提取出 北省 松滋市 八宝镇 丝线

原文地址: https://www.cveoy.top/t/topic/hwxQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录