Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab3;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- import javafx.application.Application;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.ListView;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.BorderPane;
- import javafx.scene.layout.HBox;
- import javafx.stage.Stage;
- import textproc.GeneralWordCounter;
- import textproc.TextProcessor;
- import textproc.WordCountComparator;
- public class BookReaderController extends Application {
- @Override
- public void start(Stage primaryStage) throws Exception {
- BorderPane root = new BorderPane();
- Scene scene = new Scene(root, 500, 500);
- primaryStage.setTitle("BookReader");
- primaryStage.setScene(scene);
- primaryStage.show();
- // Från holgersson
- Set<String> stopWords = new HashSet<String>();
- GeneralWordCounter gW = new GeneralWordCounter(stopWords);
- Scanner scan = new Scanner(new File("undantagsord.txt"));
- while (scan.hasNext()) {
- stopWords.add(scan.next().toLowerCase());
- }
- scan.close();
- Scanner s = new Scanner(new File("nilsholg.txt"));
- s.findWithinHorizon("\uFEFF", 1);
- s.useDelimiter("(\\s|,|\\.|:|;|!|\\?|'|\\\")+"); // se handledning
- while (s.hasNext()) {
- String word = s.next().toLowerCase();
- gW.process(word);
- }
- s.close();
- //Skapa LifeView + ObservableList
- ObservableList<Map.Entry<String, Integer>> words = FXCollections.observableArrayList(gW.getWords());
- ListView<Map.Entry<String, Integer>> listView = new ListView<Map.Entry<String, Integer>>(words);
- root.setCenter(listView);
- //HBox
- HBox hbox = new HBox(0);
- Button alfa = new Button("Alphabetic");
- Button freq = new Button("Frequency");
- Button find = new Button("Find");
- TextField textField = new TextField();
- hbox.getChildren().addAll(alfa, freq);
- hbox.getChildren().add(textField);
- hbox.getChildren().add(find);
- root.setBottom(hbox);
- alfa.setOnAction(event -> words.sort((o1, o2) -> o1.getKey().compareTo(o2.getKey())));
- freq.setOnAction(event -> words.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
- Button enter = new Button();
- enter.defaultButtonProperty();
- find.setDefaultButton(true);
- find.setOnAction(event -> { //Scroll to the entered word and select it
- String searchWord = textField.getText().toLowerCase().trim();
- for(Map.Entry<String, Integer> w: words) {
- if(w.getKey().equals(searchWord)) {
- listView.scrollTo(w);
- listView.getSelectionModel().clearAndSelect(words.indexOf(w));
- }
- }
- });
- }
- public static void main(String[] args) {
- Application.launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement