我正在开发一个可以生成公司/产品名称的新网站。有人可以访问该站点并输入一堆您可能希望以某种方式包含在产品含义中的单词。
即你刚刚发明了一个清理漏油的机器人。您输入一个单词列表:robot、oil、spill、autonomous、intelligent 等。代码将获取单词,找到所有这些单词的同义词、前缀和后缀,并尝试以一种很酷的方式将它们混合在一起。
石油将产生同义词石油和前缀石油。将它与机器人混合在一起会得到“Petrobot”。或者,对于新版本的闹钟,列表:“智能、闹钟、时钟、感知、连接”可以产生产品名称“认知时钟”。
该网站会显示一个混搭词的列表,您可以从最好的名字中挑选。
我的问题是这样的。关于如何生成这些混搭词的任何想法?现在,我将搜索同义词、前缀和后缀并将它们存储在一个数组中。然后我要搜索单词之间的常用字母,并尽可能地重叠它们。即 Direct TV 变为 DirecTV。这种蛮力搜索似乎有点不雅。
有没有其他你能想到的生成产品名称的方法,或者我建议的更简单的方法?
只是想看看有没有其他人能想到的方法。当然这个网站是免费开放的,我会在网站的about页面上链接到这个主题,所以请不要认为这篇文章是我从社区中获利。
最佳答案
我会将单词的所有前缀存储在多 HashMap 中。要检查一个词是否以“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");
}
}
打印:
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
如果速度是一个问题,您可能需要将 String
更改为 StringBuilder
。
关于php - 产品命名算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4182176/