字符串精确分词

题目描述

给定一个连续不包含空格的字符串,该字符串仅包含英文小写字母及英文标点符号(逗号、分号、句号),同时给定词库,对该字符串进行精确分词。

说明:

精确分词:字符串分词后,不会出现重叠。即"ilovechina",不同词库可分割为"i,love,china",“ilove,china”,不能分割出现重叠的"i,ilove,china",i 出现重叠

标点符号不成词,仅用于断句

词库:根据外部知识库统计出来的常用词汇例:dictionary = [“i”, “love”, “china”, “lovechina”, “ilove”]

分词原则:采用分词顺序优先且最长匹配原则

“ilovechina”,假设分词结果 [i,ilove,lo,love,ch,china,lovechina],则输出 [ilove,china]

错误输出:[i,lovechina],原因:“ilove” > 优先于 “lovechina” 成词

错误输出:[i,love,china],原因:“ilove” > "i"遵循最长匹配原则

输入描述

第一行输入待分词语句 “ilovechina”

字符串长度限制:0 < length < 256
第二行输入中文词库 “i,love,china,ch,na,ve,lo,this,is,this,word”

词库长度限制:1 < length < 100000

输出描述

按顺序输出分词结果 “i,love,china”

例1

输入
ilovechina
i,love,china,ch,na,ve,lo,this,is,the,word
输出
i,love,china

例2

输入
iat
i,love,china,ch,na,ve,lo,this,is,the,word,beauti,tiful,ful
输出
i,a,t
说明
单个字母,

不在词库中且不成词则输出单个字母

例3

输入
ilovechina,thewordisbeautiful
i,love,china,ch,na,ve,lo,this,is,the,word,beauti,tiful,ful
输出
i,love,china,the,word,is,beauti,ful
说明
标点符号为英文标点符号

Python 代码实现

class Node:
    def __init__(self):
        self.pas = 0
        self.end = 0
        self.childs = {}

# 前缀树
class Trie:
    def __init__(self):
        self.root = Node()

    def insert(self, word):
        if len(word) == 0:
            return
        node = self.root
        node.pas += 1
        for char in word:
            if char not in node.childs:
                node.childs[char] = Node()
            node = node.childs[char]
            node.pas += 1
        node.end += 1

    def prefixNums(self, word):
        if len(word) == 0:
            return 0
        node = self.root
        for char in word:
            if char not in node.childs:
                return 0
            node = node.childs[char]
        return node.pas


def get_words(sentence, dictionay):
    trie = Trie()
    # 生成前缀树
    for word in dictionay:
        trie.insert(word)
    res = []
    L = 0
    while L < len(sentence):
        if sentence[L].isalpha():
            R = L
            while R <= len(sentence):
                # R 碰到非字母字符必发生最长前缀越界
                if trie.prefixNums(sentence[L:R+1]) == 0:
                    # 这个char不在root.child里
                    if R == L:
                        res.append(sentence[L])
                        L += 1
                        break
                    # R > L 的情况
                    else:
                        # 因为是sentence[L:R+1] 刚越界,所以前面sentence[L:R]就是最长前缀了
                        res.append(sentence[L:R])
                        L = R
                        break
                # R 刚越界,但sentence[L:R] 有这个前缀,直接将sentence[L:R]加入到结果中,并且立马返回res
                elif R == len(sentence) and trie.prefixNums(sentence[L:R]) != 0:
                    res.append(sentence[L:R])
                    return ','.join(res)
                R += 1
        else:
            L += 1
    return ','.join(res)


sentence = 'ilovechina,thewordisbeautiful'
dictionay = 'i,love,china,ch,na,ve,lo,this,is,the,word,beauti,tiful,ful'.split(',')

print(get_words(sentence, dictionay))

如果这篇文章对您有所帮助
❤️求点赞!!!求收藏!!!求关注!!!❤️
如果有什么不懂也可以评论区留言一起讨论。

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小莫同学吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值