Phi*_*ses 11 html php mysql algorithm
我正在开发一个新的网站,而不是生成公司/产品名称.有人可以来到网站并输入一些你可能希望以某种方式包含在产品含义中的单词.
即你刚刚发明了一种清除漏油的机器人.你输入一个单词列表:机器人,石油,溢出,自主,智能等.代码将采用单词,找到所有这些单词的同义词,前缀和后缀,并尝试以一种很酷的方式将它们混合在一起.
石油将产生同义词石油和前缀石油.与机器人一起捣碎会给"Petrobot".或者,对于新版本的闹钟,列表:"智能,闹钟,时钟,感知,连接"可以产生产品名称"Cognizant Clock".
该网站将提供一个拼凑的单词列表,您可以从最佳名称中选择.
我的问题是这个.关于如何生成这些捣碎的单词的任何想法?现在,我将搜索同义词,前缀和后缀,并将它们存储在一个数组中.然后我将搜索单词之间的共同字母,并尽可能重叠它们.即Direct TV成为DirecTV.这种蛮力搜索看起来有点不雅.
是否还有其他方法可以生成您能想到的产品名称,或者更容易实现我建议的产品名称?
只是想看看是否还有其他人可以想到的方法.当然,这个网站将是免费和开放的,我将在该网站的关于页面链接到这个主题,所以请不要认为这篇文章是因为我从社区中获利.
我会将单词的所有前缀存储在多哈希图中。要检查一个单词是否以“bot”开头,您只需在前缀映射中进行一次查找。
之后,它只是对“可连接”单词的“图”进行广度优先遍历。
像这样的东西:
import java.util.*;
public class WordMasher {
int maxWordLen = 0;
Set<String> words = new HashSet<String>();
HashMap<String, Set<String>> prefixes = new HashMap<String, Set<String>>();
public WordMasher(String... words) {
for (String word : words) {
this.words.add(word);
maxWordLen = Math.max(maxWordLen, word.length());
for (int i = 0; i < word.length() - 1; i++)
putPrefix(word.substring(0, i), word);
}
}
private void putPrefix(String pref, String word) {
getPrefixSet(pref).add(word);
}
public Set<String> getMashes() {
Set<String> mashes = new HashSet<String>();
for (String word : words) {
Set<String> newWordsLeft = new HashSet<String>(words);
newWordsLeft.remove(word);
mashes.addAll(getMashes(word, newWordsLeft));
}
return mashes;
}
private Set<String> getPrefixSet(String prefix) {
if (!prefixes.containsKey(prefix))
prefixes.put(prefix, new HashSet<String>());
return prefixes.get(prefix);
}
private Set<String> getMashes(String prefix, Set<String> wordsLeft) {
Set<String> mashes = new HashSet<String>();
int prefLen = prefix.length();
for (int n = Math.min(prefLen, maxWordLen); n >= 1; n--) {
String toMatch = prefix.substring(prefLen - n, prefLen);
List<String> alts = new ArrayList<String>(getPrefixSet(toMatch));
alts.retainAll(wordsLeft);
for (String alt : alts) {
String newPrefix = prefix + alt.substring(n);
mashes.add(newPrefix);
Set<String> newWordsLeft = new HashSet<String>(wordsLeft);
newWordsLeft.remove(alt);
for (String tail : getMashes(newPrefix, newWordsLeft))
mashes.add(tail);
}
}
return mashes;
}
public static void printProductNames(String... words) {
System.out.println("Products for " + Arrays.toString(words) + ":");
for (String product : new WordMasher(words).getMashes())
System.out.println(" " + product);
System.out.println();
}
public static void main(String[] args) {
printProductNames("robot", "liquid", "oil", "cleaner", "spill", "turbo" );
printProductNames("world", "domination", "yellow",
"monkey", "donkey", "banana");
}
}
Run Code Online (Sandbox Code Playgroud)
印刷:
Products for [robot, liquid, oil, cleaner, spill, turbo]:
turboiliquid
oiliquid
spilliquid
cleanerobot
roboturbo
cleaneroboturboil
roboturboiliquid
cleaneroboturbo
cleaneroboturboiliquid
turboil
roboturboil
Products for [world, domination, yellow, monkey, donkey, banana]:
monkeyellow
yelloworldonkey
donkeyelloworld
donkeyelloworldomination
worldonkey
monkeyelloworldomination
yelloworldomination
worldomination
monkeyelloworldonkey
yelloworld
monkeyelloworld
donkeyellow
worldonkeyellow
Run Code Online (Sandbox Code Playgroud)
如果速度是一个问题,您可能需要将Strings 更改为StringBuilders。