2012-12-11 45 views
1

我想在Java中使用正向遍历生成可能的令牌。例如,如果我有一个字符串“这是我的车”。我需要生成令牌以Java生成字符串令牌

  • “这是我的车”
  • “这是我的”
  • “这是”
  • “这”
  • “是我的车”
  • “我的”
  • “是”
  • “我的车”
  • “我”
  • “汽车”

这样做的最佳方法是什么?任何例子?谢谢。

+4

记号化字符串以'分裂( “”)',然后使用双重嵌套循环来生成。 – nhahtdh

+1

看起来,您的示例保留了从起始字符串的原始顺序:“是我的车”,而不是“本车”或“我的车”。那是故意的还是意外? – CPerkins

+0

另外,您生成令牌的顺序是否重要? – NPE

回答

5

这里是另一种解决方案分割和嵌套循环:

public static void main(String[] args) { 
    String original = "this is my car"; 

    String[] singleWords = original.split(" "); // split the String to get the single words 
    ArrayList<String> results = new ArrayList<String>(); // a container for all the possible sentences 
    for (int startWord = 0; startWord < singleWords.length; startWord++) { // starWords start with 0 and increment just until they reach the last word 
     for (int lastWord = singleWords.length; lastWord > startWord; lastWord--) { // last words start at the end and decrement just until they reached the first word 
      String next = ""; 
      for (int i = startWord; i != lastWord; i++) { // put all words in one String (starting with the startWord and ending with the lastWord) 
       next += singleWords[i] + " "; 
      } 
      results.add(next); // add the next result to your result list 
     } 
    } 

    // this is just to check the results. All your sentences are now stored in the ArrayList results 
    for (String string : results) { 
     System.out.println("" + string); 
    } 
} 

,这是我的结果,当我测试的方法:

this is my car 
this is my 
this is 
this 
is my car 
is my 
is 
my car 
my 
car 
2

使用Guava

String yourOriginalString = "This is my car"; 
final Set<String> originalWords = 
     Sets.newLinkedHashSet(
       Splitter.on(CharMatcher.WHITESPACE).trimResults().split(yourOriginalString)); 
final Set<Set<String>> variations = Sets.powerSet(originalWords); 
for (Set<String> variation : variations) { 
    System.out.println(Joiner.on(' ').join(variation)); 
} 

输出:

This 
is 
This is 
my 
This my 
is my 
This is my 
car 
This car 
is car 
This is car 
my car 
This my car 
is my car 
This is my car 
+0

谢谢。但是问题在于,它产生了乱序令牌。例如:“汽车我的”,“汽车我的这个”等 – M99

+0

好的,那么你需要过滤结果或使用这个作为你自己的结果灵感 –

+0

当然。谢谢你的帮助。 – M99

1

这里是一个可能的方式:

//Just a method that seperates your String into an array of words based on the spaces 
//I'll leave that for you to figure out how to make 
String[] array = getSeperatedWords(<yourword>); 
List<StringBuffer> bufferArray = new ArrayList<StringBuffer>(); 

for(int i = 0; i < array.length; i++){ 
    StringBuffer nowWord = array[i]; 
    for(int j = i; j < array.length; j++{ 
     nowWord.append(array[j]); 
    } 
    bufferArray.add(nowWord); 
} 
for(int i = 0; i < bufferArray.length; i++){ 
    System.out.print(bufferArray.get(i)); 
} 
+0

如果你从'i'迭代'j'到数组的末尾,会不会跳过类似“is my”的字符串? – irrelephant

1
import java.util.Arrays; 

public class Test { 

    public static void main(String[] args) { 

     String var = "This is my car"; 
     permute(var); 

    } 

    public static void permute(String var) { 

     if(var.isEmpty()) 
      return; 

     String[] arr = var.split(" "); 

     while(arr.length > 0) { 
      for(String str : arr) { 
       System.out.print(str + " "); 
      } 
      arr = (String[]) Arrays.copyOfRange(arr, 0, arr.length - 1); 
      System.out.println();    
     }  

     String[] original = var.split(" "); 
     permute(implodeArray((String[]) Arrays.copyOfRange(original, 1, original.length), " ")); 

    } 

    public static String implodeArray(String[] inputArray, String glueString) { 

     String output = ""; 

     if (inputArray.length > 0) { 
      StringBuilder sb = new StringBuilder(); 
      sb.append(inputArray[0]); 

      for (int i=1; i<inputArray.length; i++) { 
       sb.append(glueString); 
       sb.append(inputArray[i]); 
      } 

      output = sb.toString(); 

     } 

     return output; 

    }   

} 

阅读这本书,你将是递归的高手:http://mitpress.mit.edu/sicp/