結合された文字列から単語を検出して分割するにはどうすればよいですか?
例:
"cdimage" -> ["cd", "image"]
"filesaveas" -> ["file", "save", "as"]
これが動的プログラミングソリューションです(メモ化された関数として実装されています)。頻度のある単語の辞書が与えられると、全体的に最も可能性の高いフレーズを与える位置で入力テキストが分割されます。実際の単語リストを見つける必要がありますが、簡単なテストのためにいくつかの構成された頻度を含めました。
WORD_FREQUENCIES = {
'file': 0.00123,
'files': 0.00124,
'save': 0.002,
'ave': 0.00001,
'as': 0.00555
}
def split_text(text, word_frequencies, cache):
if text in cache:
return cache[text]
if not text:
return 1, []
best_freq, best_split = 0, []
for i in xrange(1, len(text) + 1):
word, remainder = text[:i], text[i:]
freq = word_frequencies.get(word, None)
if freq:
remainder_freq, remainder = split_text(
remainder, word_frequencies, cache)
freq *= remainder_freq
if freq > best_freq:
best_freq = freq
best_split = [word] + remainder
cache[text] = (best_freq, best_split)
return cache[text]
print split_text('filesaveas', WORD_FREQUENCIES, {})
--> (1.3653e-08, ['file', 'save', 'as'])
そのためのライブラリは知りませんが、基本的な機能を実装するのは難しいことではありません。
words
。例:
これを行うライブラリはわかりませんが、単語のリストがあれば書くのはそれほど難しくありません。
wordList = file('words.txt','r').read().split()
words = set( s.lower() for s in wordList )
def splitString(s):
found = []
def rec(stringLeft, wordsSoFar):
if not stringLeft:
found.append(wordsSoFar)
for pos in xrange(1, len(stringLeft)+1):
if stringLeft[:pos] in words:
rec(stringLeft[pos:], wordsSoFar + [stringLeft[:pos]])
rec(s.lower(), [])
return found
これにより、文字列を指定された単語に分割するためのすべての可能な方法が返されます。
例:
>>> splitString('filesaveas')
[['file', 'save', 'as'], ['files', 'ave', 'as']]
この例を見ることができます:しかし、それはscalaで書かれています。これにより、文の間にスペースがない場合に、必要なものを分割できます。
この質問がPython用にマークされていることは知っていますが、JavaScriptの実装が必要でした。以前の回答から離れて、コードを共有すると思いました。きちんと動作するようです。
function findWords(input){
input = input.toLowerCase().replace(/\s/g, ""); //Strip whitespace
var index = 0;
var validWords = [];
for (var len = input.length; len > 0; len--){ //Go backwards as to favor longer words
var testWord = input.substr(index, len);
var dictIndex = _dictionary.indexOf(testWord.replace(/[^a-z\']/g, "")); //Remove non-letters
if (dictIndex != -1){
validWords.push(testWord);
if (len == input.length){
break; //We are complete
}
var nextWords = findWords(input.substr(len, input.length - len)); //Recurse
if (!nextWords.words.length){ //No further valid words
validWords.pop();
}
validWords = validWords.concat(nextWords.words);
if (nextWords.complete === true){
break; //Cascade complete
}
}
}
return {
complete:len > 0, //We broke which indicates completion
words:validWords
};
}
注:「_ dictionary」は、頻度でソートされた単語の配列であると想定されています。ProjectGutenbergのワードリストを使用しています。