Advertisement
Guest User

nils

a guest
Feb 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package lab3;
  2.  
  3. import java.io.File;
  4. import java.util.ArrayList;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Scanner;
  9. import java.util.Set;
  10.  
  11. import javafx.application.Application;
  12. import javafx.collections.FXCollections;
  13. import javafx.collections.ObservableList;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Button;
  16. import javafx.scene.control.ListView;
  17. import javafx.scene.control.TextField;
  18. import javafx.scene.layout.BorderPane;
  19. import javafx.scene.layout.HBox;
  20. import javafx.stage.Stage;
  21. import textproc.GeneralWordCounter;
  22. import textproc.TextProcessor;
  23. import textproc.WordCountComparator;
  24.  
  25. public class BookReaderController extends Application {
  26.  
  27. @Override
  28. public void start(Stage primaryStage) throws Exception {
  29. BorderPane root = new BorderPane();
  30. Scene scene = new Scene(root, 500, 500);
  31. primaryStage.setTitle("BookReader");
  32. primaryStage.setScene(scene);
  33. primaryStage.show();
  34.  
  35. // Från holgersson
  36. Set<String> stopWords = new HashSet<String>();
  37. GeneralWordCounter gW = new GeneralWordCounter(stopWords);
  38. Scanner scan = new Scanner(new File("undantagsord.txt"));
  39. while (scan.hasNext()) {
  40. stopWords.add(scan.next().toLowerCase());
  41. }
  42. scan.close();
  43. Scanner s = new Scanner(new File("nilsholg.txt"));
  44. s.findWithinHorizon("\uFEFF", 1);
  45. s.useDelimiter("(\\s|,|\\.|:|;|!|\\?|'|\\\")+"); // se handledning
  46. while (s.hasNext()) {
  47. String word = s.next().toLowerCase();
  48. gW.process(word);
  49. }
  50. s.close();
  51.  
  52. //Skapa LifeView + ObservableList
  53. ObservableList<Map.Entry<String, Integer>> words = FXCollections.observableArrayList(gW.getWords());
  54. ListView<Map.Entry<String, Integer>> listView = new ListView<Map.Entry<String, Integer>>(words);
  55. root.setCenter(listView);
  56.  
  57. //HBox
  58. HBox hbox = new HBox(0);
  59. Button alfa = new Button("Alphabetic");
  60. Button freq = new Button("Frequency");
  61. Button find = new Button("Find");
  62. TextField textField = new TextField();
  63. hbox.getChildren().addAll(alfa, freq);
  64. hbox.getChildren().add(textField);
  65. hbox.getChildren().add(find);
  66. root.setBottom(hbox);
  67. alfa.setOnAction(event -> words.sort((o1, o2) -> o1.getKey().compareTo(o2.getKey())));
  68. freq.setOnAction(event -> words.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
  69.  
  70. Button enter = new Button();
  71. enter.defaultButtonProperty();
  72.  
  73. find.setDefaultButton(true);
  74. find.setOnAction(event -> { //Scroll to the entered word and select it
  75. String searchWord = textField.getText().toLowerCase().trim();
  76. for(Map.Entry<String, Integer> w: words) {
  77. if(w.getKey().equals(searchWord)) {
  78. listView.scrollTo(w);
  79. listView.getSelectionModel().clearAndSelect(words.indexOf(w));
  80.  
  81. }
  82. }
  83. });
  84. }
  85. public static void main(String[] args) {
  86. Application.launch(args);
  87.  
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement