切分算法

def fully_segment(text,dic):
    word_list = []
    for i in range(len(text)):
        for j in range(i+1,len(text)+1):
            word = text[i:j]
            if word in dic:
                word_list.append(word)
    return word_list



def forward_segment(text,dic):
    word_list = []
    i = 0
    while i < len(text):
        longest_word = text[i]
        for j in range(i+1,len(text)+1):
            word = text[i:j]
            if word in dic:
                if len(word) > len(longest_word):
                    longest_word = word
        word_list.append(longest_word)
        i += len(longest_word)
    return word_list




def backward_segment(text,dic):
    word_list = []
    i = len(text) - 1
    while i >= 0:
        longest_word = text[i]
        for j in range(0,i):
            word = text[j:i+1]
            if word in dic:
                if len(word) > len(longest_word):
                    longest_word = word
                    break
        word_list.insert(0,longest_word)
        i -= len(longest_word)
    return word_list




def count_single_char(word_list:list):
    return sum(1 for word in word_list if len(word) == 1)
def bidirectional_segment(text,dic):
    f = forward_segment(text,dic)
    b = backward_segment(text,dic)
    if len(f) < len(b):
        return f
    elif len(f) > len(b):
        return b
    else:
        if count_single_char(f) < count_single_char(b):
            return f
        else:
            return b




dic=['商','商品','品','和','和服','服','服务','务',
     '就','就读','读','北','北京','北京大学','京','大','大学','学',
     '研究','研究生','生','生命','起源',
     '欢','欢迎','新','迎新','老','老师','师生','生前','前来','来','就餐',
     '项','项目','目的','的','研究',
     '当','当下','下雨天','雨天','地面','积水',
     '结婚','和尚','尚未','未']
while 1:
    print('请输入句子:')
    text=input()
    print('完全切分:\n',fully_segment(text,dic))
    print('最长匹配:\n',forward_segment(text,dic))
    print('逆向最长匹配:\n',backward_segment(text,dic))
    print('双向最长匹配:\n',bidirectional_segment(text,dic))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值