JavaFx BarChart( "Ghafla Kenya News/music Title Search Terms")

I was working on a project that involved the New York times article search api ,its the best API around the project was to have a look of who is mentioned in most of the Kenyan articles.Everything was just fine but most of the name are of politicians and athletes and i was very much interested in musicians.I had to look for more  local content,searching around  i came across ghafla!Kenya has 9380 Kenyan articles  under music category at the time of writing this post.
Getting all the 9380 titles was very easy with the help of kimono i was able to crawl 7855 titles/9380 titles.Still trying to crawl the rest 1525 pages.
7855 pages was really significant for my project.I downloaded the data in csv format from my kimono API page.

NB:the data is under news/music

Searching companies in Kenya i.e safaricom,airtel,tuskys,nakumatt

Companies result search

Searching politicians  i.e uhuru,ruto,raila,sonko

Searching socialite  i.e huddah,sidika,risper

Searching musicians  i.e octopizzo,p-unit,sauti sol,elani,stl,jaguar,wahu,nameless

Parsing the csv file to return only titles with quotes omitted the methods returns an observableList

public ObservableList<String>  getHeadlines(){
        try {

            path=Paths.get("list.csv");

            s=Files.readAllLines(path);

           } catch (IOException ex) {
           
        }
         for(String st:s){
         
            String rry[]=st.split(",");

            //do away with the quote marks
            String title=rry[1].substring(1, rry[1].length()-1).toLowerCase();
           
            list.add(title);
        }

        return list;
    }

Getting the keyword count method

public int getKeywordFrequency(){
   
    for(String line:list){
     
        if(line.contains(keyword)){
            count++;
        }
    }

    return count;
}

The main Class

NB:/The search button when pressed will continue to add to the results.

TextField name_txt;
Button search_btn;
Button prev_btn;
ListView<String> listView;
Text txt;


HeadlineList headlines;

StackPane root = new StackPane();

CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis(0,250,2);
BarChart<String,Number> bc =new BarChart<>(xAxis,yAxis);

    @Override
    public void start(Stage primaryStage) throws IOException  {

        bc.setTitle("Ghafla Kenya News Title\n \tSearch Terms ");
        xAxis.setLabel("Names");
        yAxis.setLabel("Word Frequency");
        yAxis.setTickLabelFill(javafx.scene.paint.Color.WHITE);
        xAxis.setTickLabelFill(javafx.scene.paint.Color.WHITE);
        xAxis.setTickLabelRotation(45);
        xAxis.setTickLabelFont(Font.font("castellar", FontWeight.EXTRA_LIGHT, 11));
     
        name_txt=new TextField();
        name_txt.setMaxSize(200, 43);
       
        StackPane.setAlignment(name_txt, Pos.TOP_LEFT);
        StackPane.setMargin(name_txt, new Insets(5,0,0,10));
       
        search_btn=new Button("",new ImageView("file:search.png"));
        StackPane.setAlignment(search_btn, Pos.TOP_LEFT);
        StackPane.setMargin(search_btn, new Insets(5,0,0,215));
       
        listView =new ListView();
        listView.setMaxSize(400, 900);
        StackPane.setAlignment(listView, Pos.TOP_LEFT);
        StackPane.setMargin(listView, new Insets(77,0,5,10));
       
        prev_btn=new Button("results :");
        StackPane.setAlignment(prev_btn, Pos.TOP_LEFT);
        StackPane.setMargin(prev_btn,new Insets(50,0,0,10));
       
        txt=new Text("");
        StackPane.setAlignment(txt, Pos.TOP_LEFT);
        StackPane.setMargin(txt,new Insets(52,0,0,250));
       
        StackPane.setAlignment(bc, Pos.TOP_LEFT);
        StackPane.setMargin(bc,new Insets(77,0,5,415));

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

            @Override
            public void handle(ActionEvent event) {
               
               String s=name_txt.getText();
               int freq;
               int results=0;
               ObservableList<String> list=FXCollections.observableArrayList();
             
               if(!s.contains(",")){
                   headlines=new HeadlineList(s);
                   list.addAll(headlines.getHeadlines());
                   listView.setItems(list);
                   txt.setText("Total of :"+list.size()+"/7855");
                   freq=headlines.getKeywordFrequency();
                 
                  XYChart.Series series=new XYChart.Series<>();
                  series.getData().add(new XYChart.Data(s, freq));
                  bc.getData().add(series);
                  root.getChildren().add(bc);
               
               }else{

                   String rry[]=s.split(",");

                   for(String word:rry){

                        headlines=new HeadlineList(word);
                        list.addAll(headlines.getHeadlines());
                        listView.setItems(list);
                        results+=list.size();
                        freq=headlines.getKeywordFrequency();
                        XYChart.Series series=new XYChart.Series<>();
                        series.getData().add(new XYChart.Data(word, freq));
                        bc.getData().add(series);

                   }
                     txt.setText("Total of :"+results+"/7855");

               }
            }
       
    });
       
        root.getChildren().addAll(name_txt,search_btn,listView,prev_btn,txt,bc);
       
        Scene scene = new Scene(root, 1210, 650);
        scene.getStylesheets().add(this.getClass().getResource("style.css").toExternalForm());
        primaryStage.setTitle("Ghafla");
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

Video Sample





Comments

Popular Posts