【问题标题】:python: word segmentation based on dictionarypython:基于字典的分词
【发布时间】:2017-09-20 16:10:10
【问题描述】:

我有一本字典

dict = ["as", "ass", "share", "rest"]

和一个字符串输入

string = "xassharest"

我想显示所有可能的词可以基于这样的字典:

[('x', 'as', 's', 'h', 'a', 'rest'), ('x', 'as', 'share', 's', 't'), ('x', 'ass', 'h', 'a', 'rest')]

实际上我已经尝试过使用所有字符串组合(使用库 itertools),但它需要很长时间。这是我的代码:

def getallpossiblewords(string):
    allwords = preprocessingcorpus("corpus.txt")
    temp = []
    for i in range(0, len(string)):
        for j in range(1, len(string) + 1):
            if string[i:j] in allwords:
                temp += [string[i:j]]

    allposwords = sorted(temp, key=len, reverse=True)
    #print(allposwords)
    return allposwords

def wordseg(string):
    a = string
    b = getallpossiblewords(string)
    cuts = []
    allpos = []
    for i in range(0,len(a)):
        cuts.extend(combinations(range(1,len(a)),i))
    for i in cuts:
        last = 0
        output = []
        for j in i:
            output.append(a[last:j])
            last = j
        output.append(a[last:])
        for x in range(len(output)):
            if output[x] in b:
                allpos += [output]
                #print(output)
    #print(allpos)

    fixallpos = list()
    for sublist in allpos:
        if sublist not in fixallpos:
            fixallpos.append(sublist)

我需要最快的算法来解决这个问题,因为字符串的输入可能会更长。

谁能解决我的问题?

【问题讨论】:

  • 这看起来像是一道作业题。你应该说如果是这种情况。您还应该展示您迄今为止尝试过的内容,并提及您遇到的确切问题。见:stackoverflow.com/help/how-to-ask
  • 你所说的字典:dict=["a","as","ass","share","re​​st"] 不是 Python 中的字典。您需要对您的预期问题的主题进行一些研究,尝试自己回答问题,然后来这里寻求帮助
  • jdv : 谢谢你的建议,我已经编辑了我的帖子。顺便说一句,你能帮我吗? t 博士:举例来说,那个字典,是的,我现在正在研究,我已经使用 CRF(基于机器学习)解决了这个问题,但我需要其他算法(基于字典)。也许你能帮帮我?
  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。您发布的代码仅定义了两个例程并在不执行任何活动命令的情况下停止。
  • 为什么你给定的解决方案不包括 ["a", "ssh", "a", "rest"] ?为什么是否包含它包括[“a”,“s”,“sha”,“rest”]:我看不出有理由拆分“ssha”,或者不分离第二个“a” ”。我们中的一个人还不明白这个问题。

标签: python nlp


【解决方案1】:

这似乎是对str.partition() 的完美递归使用。下面是我的示例实现,我不会声称它解决了所有问题(因为实际上没有测试用例),而是尝试在这种特定方法上做销售工作:

def segmented(string):

    segmentations = set()

    for word in words:
        before, match, after = string.partition(word)

        if not match:
            continue

        prefixes = segmented(before) or [before]
        suffixes = segmented(after) or [after]

        if prefixes and suffixes:
            for prefix in prefixes:
                for suffix in suffixes:
                    segmentations.add((*prefix, word, *suffix))
        elif prefixes:
            for prefix in prefixes:
                    segmentations.add((*prefix, word, *suffixes))
        elif suffixes:
            for suffix in suffixes:
                    segmentations.add((*prefixes, word, suffix))
        else:
            segmentations.add((*prefixes, word, *suffixes))

    return segmentations

words = ["as", "ass", "share", "rest"]

print(segmented("xassharest"))

输出

% python3 test.py
{('x', 'as', 's', 'h', 'a', 'rest'), ('x', 'as', 'share', 's', 't'), ('x', 'ass', 'h', 'a', 'rest')}
%

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    相关资源
    最近更新 更多