Php 产品命名算法

Php 产品命名算法,php,mysql,html,algorithm,Php,Mysql,Html,Algorithm,我正在开发一个新的网站,可以生成公司/产品名称。有人可以来到这个网站,输入一堆你可能希望包含在产品含义中的单词 i、 你刚刚发明了一个机器人来清理漏油。您输入一个单词列表:robot、oil、spill、autonomy、intelligent等。代码将获取这些单词,查找所有这些单词的同义词、前缀和后缀,并尝试以冷静的方式将它们混合在一起 石油将产生同义词石油和前缀石油。将其与robot混合在一起将产生“Petrobot”。或者,对于新版闹钟,列表“智能、闹钟、时钟、感知、连接”可以产生产品名称

我正在开发一个新的网站,可以生成公司/产品名称。有人可以来到这个网站,输入一堆你可能希望包含在产品含义中的单词

i、 你刚刚发明了一个机器人来清理漏油。您输入一个单词列表:robot、oil、spill、autonomy、intelligent等。代码将获取这些单词,查找所有这些单词的同义词、前缀和后缀,并尝试以冷静的方式将它们混合在一起

石油将产生同义词石油和前缀石油。将其与robot混合在一起将产生“Petrobot”。或者,对于新版闹钟,列表“智能、闹钟、时钟、感知、连接”可以产生产品名称“认知时钟”

该网站将提供一个拼凑在一起的单词列表,你可以从最好的名字中挑选

我的问题是。有没有关于如何生成这些混搭词的想法?现在,我将搜索同义词、前缀和后缀,并将它们存储在一个数组中。然后我将搜索单词之间的常用字母,并尽可能多地重叠它们。i、 直接电视变成了直接电视。这种暴力搜索似乎有点不雅

有没有其他生成产品名称的方法,或者我建议的方法更简单?


只是想看看是否还有其他方法可以让人们想到。当然,这个网站是免费开放的,我会在网站的“关于”页面上链接到这个主题,所以请不要认为这篇文章是我从社区中获利。

我会将单词的所有前缀存储在一个多哈希映射中。要检查一个单词是否以“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");
    }
}

如果此处存在速度问题,您可能需要将
字符串
s更改为
StringBuilder
s。

后缀树可能是您正在寻找的数据结构,以有效支持各种操作:-

仍在构建中。可能会在假期期间建造。我会通知你的。@Phil,关于这个网站有什么更新吗?
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