【问题标题】:split sentence without space in python (nltk?)在python中拆分没有空格的句子(nltk?)
【发布时间】:2016-11-02 16:15:41
【问题描述】:

我有一组连接的单词,我想将它们拆分成数组

例如:

split_word("acquirecustomerdata")
=> ['acquire', 'customer', 'data']

我找到了pyenchant,但它不适用于 64 位 Windows。

然后我尝试将每个字符串拆分为子字符串,然后将它们与 wordnet 进行比较以找到等效的单词。 例如:

from nltk import wordnet as wn
def split_word(self, word):
    result = list()
    while(len(word) > 2):
        i = 1
        found = True
        while(found):
            i = i + 1
            synsets = wn.synsets(word[:i])
            for s in synsets:
                if edit_distance(s.name().split('.')[0], word[:i]) == 0:
                    found = False
                    break;
        result.append(word[:i])
        word = word[i:]
   print(result)

但是这个解决方案不确定而且太长了。 所以我正在寻求你的帮助。

谢谢

【问题讨论】:

  • 如果您进行单词检测,那么tome 可能会出现。我会说修复给你连接单词的数据源
  • 正如@cricket_007 所建议的那样,单词检测可能非常困难(通常需要机器学习和人工智能算法)并且会引入大量的自然语言歧义,因此您的数据源应该是固定的。
  • 他们俩说的。你为什么不解释一下你是如何得到需要拆分的粘在一起的单词的。可能有一种更简单的方法可以到达您要去的地方。
  • 其实我只是想清理我的数据。但如果不容易实现,我会手动完成。谢谢大家

标签: python nltk spell-checking wordnet


【解决方案1】:

如果你有一个所有可能单词的列表,你可以使用这样的东西:

import re

word_list = ["go", "walk", "run", "jump"]  # list of all possible words
pattern = re.compile("|".join("%s" % word for word in word_list))

s = "gowalkrunjump"
result = re.findall(pattern, s)

【讨论】:

  • "我有一组连接词"
【解决方案2】:

检查 - Word Segmentation Task 来自 Norvig 的工作。

from __future__ import division
from collections import Counter
import re, nltk

WORDS = nltk.corpus.brown.words()
COUNTS = Counter(WORDS)

def pdist(counter):
    "Make a probability distribution, given evidence from a Counter."
    N = sum(counter.values())
    return lambda x: counter[x]/N

P = pdist(COUNTS)

def Pwords(words):
    "Probability of words, assuming each word is independent of others."
    return product(P(w) for w in words)

def product(nums):
    "Multiply the numbers together.  (Like `sum`, but with multiplication.)"
    result = 1
    for x in nums:
        result *= x
    return result

def splits(text, start=0, L=20):
    "Return a list of all (first, rest) pairs; start <= len(first) <= L."
    return [(text[:i], text[i:]) 
            for i in range(start, min(len(text), L)+1)]

def segment(text):
    "Return a list of words that is the most probable segmentation of text."
    if not text: 
        return []
    else:
        candidates = ([first] + segment(rest) 
                      for (first, rest) in splits(text, 1))
        return max(candidates, key=Pwords)

print segment('acquirecustomerdata')
#['acquire', 'customer', 'data']

要获得比这更好的解决方案,您可以使用二元组/三元组。

更多示例:Word Segmentation Task

【讨论】:

    【解决方案3】:

    您可以使用一个名为“wordsegment”的库:http://www.grantjenks.com/docs/wordsegment/

    pip install wordsegment
    import wordsegment
    from wordsegment import load, segment
    load()
    segment("acquirecustomerdata")
    

    输出:

    ['acquire', 'customer', 'data']
    

    【讨论】:

      猜你喜欢
      • 2022-07-06
      • 2017-07-15
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 2014-06-04
      相关资源
      最近更新 更多