首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >产品命名算法

产品命名算法
EN

Stack Overflow用户
提问于 2010-11-15 07:12:02
回答 2查看 897关注 0票数 11

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

也就是说,你刚刚发明了一个清理溢油的机器人。你输入了一个单词列表:机器人,石油,溢出,自主,智能等等。代码会把这些单词取下来,找出所有这些单词的同义词、前缀和后缀,然后尝试用一种冷静的方式将它们混合在一起。

石油将产生石油的同义词和前缀石油。把它和机器人混在一起就能得到“Petrobot”了。或者,对于一个新版本的闹钟,名单:“智能,闹钟,时钟,感知,连接”可能产生产品名称“认知时钟”。

该网站将提供一个列表的混合词,你可以从最好的名字。

我的问题是这个。对于如何产生这些混在一起的单词,有什么想法吗?现在,我将搜索同义词、前缀和后缀,并将它们存储在数组中。然后,我将寻找单词之间的普通字母,并尽可能地重叠它们。即直接电视变成DirecTV。这次蛮力搜索似乎有点不雅。

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-15 07:22:05

我会把单词的所有前缀都存储在一个多哈希映射中。要检查一个单词是否以"bot“开头,您只需在前缀映射中进行一次查找。

在此之后,它只是“可连接”单词的“图”的第一次遍历。

就像这样:

代码语言:javascript
复制
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");
    }
}

指纹:

代码语言:javascript
复制
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

如果速度是一个问题,您可能需要将Strings更改为StringBuilders。

票数 3
EN

Stack Overflow用户

发布于 2010-11-15 07:32:47

后缀树可能是为有效支持各种操作而寻找的数据结构:- http://en.wikipedia.org/wiki/Suffix_tree

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4182176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档