【问题标题】:Generating word list from word从单词生成单词列表
【发布时间】:2011-04-15 03:58:59
【问题描述】:

寻找开始使用我称之为单词模糊器单词列表生成器的想法。

它需要一个字符串,例如“你好”,基本上看起来会从中产生更多相似词的可能性,即返回类似的东西:

  • h3ll0
  • he11o
  • HEL10
  • h3LLo
  • ...
  • ...

如您所见,我需要对上限敏感。

我只是在寻找可以开始的想法/方法。

也许第一遍做上限:

  • 你好
  • 你好
  • 你好
  • 你好
  • 你好
  • ...

然后将该列表/数组提供给子数字/符号的方法

我有信心并且很可能会使用 C#(至少开始)这个应用程序。

如果已经写了一些可用的东西来完成我所说的那种事情,那就更好了,我很想听听。

感谢阅读。

【问题讨论】:

    标签: c# random word


    【解决方案1】:

    这是一个太长的评论,但它不是一个真正的答案。只是一个建议。首先,考虑这个链接:

    http://ericlippert.com/2010/06/28/computing-a-cartesian-product-with-linq/

    您可以将您的问题视为计算一系列序列的笛卡尔积。仅考虑字母数字字符,它们具有 1 到 3 种状态,例如小写的原始字符(如果适用)、大写的(如果适用)和数字替换(同样,如果适用)。或者,如果您以数字开头,则数字以及大小写字母替换。如:

    A -> a, A, 4
    B -> b, B, 8
    C -> c, C
    D -> d, D
    // etc.
    1 -> 1, L, l
    2 -> 2
    3 -> 3, e, E
    // etc.
    

    每一个都是一个序列。因此,在您的问题中,您可能会将原始输入“hello”转换为一个过程,在该过程中您获取与字符串中每个字符对应的序列,然后获取这些序列并获取它们的笛卡尔积。 Eric Lippert 的链接博客中的方法将是一个很好的指南,可以从这里继续。

    【讨论】:

    • 谢谢你,很好的回答/评论 - 不幸的是我不能在 atm 投票
    • @tanxiong:如果这足以让您入门,您可以接受答案。
    【解决方案2】:

    此示例将 Anthony Pegram 的想法放入代码中。我对您的字母映射和输入进行了硬编码,但您可以轻松更改。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SO5672236
    {
        static class Program
        {
            static void Main()
            {
                // Setup your letter mappings first
                Dictionary<char,string[]> substitutions = new Dictionary<char, string[]>
                {
                    {'h', new[] {"h", "H"}},
                    {'e', new[] {"e", "E", "3"}},
                    {'l', new[] {"l", "L", "1"}},
                    {'o', new[] {"o", "O"}}
                };
    
                // Take your input
                const string input = "hello";
    
                // Get mapping for each letter in your input
                IEnumerable<string[]> letters = input.Select(c => substitutions[c]);
    
                // Calculate cortesian product
                var cartesianProduct = letters.CartesianProduct();
    
                // Concatenate letters
                var result = cartesianProduct.Select(x => x.Aggregate(new StringBuilder(), (a, s) => a.Append(s), b => b.ToString()));
    
                // Print out results
                result.Foreach(Console.WriteLine);
            }
    
            // This function is taken from 
            // http://blogs.msdn.com/b/ericlippert/archive/2010/06/28/computing-a-cartesian-product-with-linq.aspx
            static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences)
            {
                IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() };
                return sequences.Aggregate(
                  emptyProduct,
                  (accumulator, sequence) =>
                    from accseq in accumulator
                    from item in sequence
                    select accseq.Concat(new[] { item }));
            }
    
            // This is a "standard" Foreach helper for enumerables
            public static void Foreach<T>(this IEnumerable<T> enumerable, Action<T> action)
            {
                foreach (T value in enumerable)
                {
                    action(value);
                }
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      【讨论】:

        【解决方案4】:

        开头
        Dictionary:
            key:  letter    
            value:  List of alternate choices for that letter
        
        create a new empty word
        for each letter in the word,
            randomly choose an alternate choice and add it to the new word.
        

        【讨论】:

          猜你喜欢
          • 2013-12-17
          • 1970-01-01
          • 2016-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多