文本在向后拼写时搜索包含另一个单词的单词

时间:2017-07-02 18:15:48

标签: java

我正在构建一个程序,允许您上传文本文件,然后在拼写向后和向前拼写时搜索形成不同单词的单词。这就是我到目前为止所做的:

public class RWF extends Application {

private Window mainStage;
private FileChooser fileChooser;
private String path;
private File file;
public String x;
public TextArea textArea;
public Iterator m;
public String reversed;
public ArrayList al;
public String[] words1;
public String z;
public Object element;

public String text;
private String[] pa;
private String sa;
private Array palindromeArray;
private String longest;
public Button openButton2;
private StringBuilder stringBuilder;
private BufferedReader bufferedReader;
public StringBuilder sb;
private BufferedReader br;
private String[] xsplit;
private String[] xsplit2;
private String words;
private String word;
public String str;
public String str2;


public RWF(){

    sb = stringBuilder;
    str2 = "Hellow";


}


public void start(Stage stage) {

    TextArea textArea = new TextArea();
    textArea.setPrefColumnCount(100);
    textArea.setPrefRowCount(100);

    VBox vbox = new VBox(textArea);

    final Button openButton = new Button("Open a Text File");
    final Button openButton2 = new Button("Analyse Text File");
    Group root = new Group(openButton, openButton2, textArea);
    Scene scene = new Scene(root, 400, 300);

    FileChooser fileChooser = new FileChooser();
     fileChooser.setTitle("Open Resource File");
     fileChooser.getExtensionFilters().addAll(
             new ExtensionFilter("Text Files", "*.txt"),
             new ExtensionFilter("All Files", "*.*"));



    openButton.setLayoutX(250);
    openButton.setLayoutY(220);
    openButton2.setLayoutX(150);
    openButton2.setLayoutY(120);
    textArea.setLayoutX(450);
    textArea.setLayoutY(320);
       stage.setTitle("Text Analyser");
       stage.setScene(scene);
       stage.show();

       openButton.setOnAction(
               new EventHandler<ActionEvent>() {


                @Override
                   public void handle(final ActionEvent e) {
                       File file = fileChooser.showOpenDialog(stage);
                       if (file != null) {
                           try {
                            textArea.setText(readFile(file));
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                       }


                }



                private String readFile(File file) throws IOException {



StringBuilder sb1 = new StringBuilder();
                bufferedReader = new BufferedReader(new FileReader(file));


                    text = bufferedReader.readLine();
                    sb1.append(text);

                    System.out.println(sb1);





                    String u = sb1.toString();
                    xsplit2 = u.split(u);
                    for (String words : xsplit2){
                        System.out.println(words);
                    }

                    StringBuilder t = sb1.reverse();    
                String x = t.toString();
            xsplit = x.split(x);
            for (String word : xsplit){
                System.out.println(word);
            }

            for (int i=0; i<xsplit2.length; i++){
                boolean found = false;
                for (int j=0; j<xsplit.length; j++){
                    if ((xsplit2[i]).equals(xsplit[j])){
           found = true;




           str = Arrays.toString(xsplit);
           str2 = Arrays.toString(xsplit2);


              java.nio.file.Path path = Paths.get("Users/paulc/workspace5000/words.txt");
                byte[] readBytes = Files.readAllBytes(path);
                String wordListContents = new String(readBytes, "UTF-8");
                String[] words = wordListContents.split("\n");
                Set wordsSet = new HashSet<>();
                Collections.addAll(wordsSet, words);

                if (wordsSet.contains(str) && wordsSet.contains(str2)){
                    textArea.appendText(str);
                }


 openButton2.setOnAction(
           new EventHandler<ActionEvent>() {






            @Override
               public void handle(final ActionEvent e) {
                textArea.appendText(str2);
                    }

});
       }
                }
            }
            return str2;
                };
               });
}


public static void main(String[] args){
    Application.launch(args);


}



}

我的问题是你如何找到文件中的另一个单词是否是向后的另一个单词。

E.g。如果文件中有单词&#39; saw&#39;,我怎么会发现是否有单独的单词&#39;是&#39;这是&#39;锯&#39;倒退?

由于

2 个答案:

答案 0 :(得分:0)

制作额外的字符串,翻译全文,您可以在此处看到如何执行此操作:Reverse a string in Java

比原始字符串中的单词分割,如下所示:Split string into individual words Java

而不是我会在反向字符串中搜索分裂的单词:How to find a whole word in a String in java

答案 1 :(得分:0)

  

我的问题是你如何找到文件中的另一个单词是否是向后的另一个单词。   例如。如果文件中有单词&#39; saw&#39;,我怎么会发现是否有单独的单词&#39;是&#39;这是&#39;锯&#39;倒退?

您可以遵循此算法:

  • 创建一组空字符串,将单词放入其中
  • 对于文本中的每个单词
    • 检查相反的单词是否在集合中,并相应地对结果采取行动
    • 将单词添加到集合

注意大写/小写字母。 当您迭代输入并在集合中存储单词时,您可能希望小写所有单词。