<commit_before>package sel

import (
	"os"
	"flag"
	"fmt"
	"log"
	"encoding/json"
	"link-select/types"
)

func SelectLink(arg *flag.Flag) {
	fmt.Fprintf(os.Stdout, "Selecting %s from json file\n", arg.Value)

	readFile, err := os.Open("files/read.json")
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error while opening read JSON file\n")
		os.Exit(-1)
	}

	var readList types.ReadList
	
	jsonParser := json.NewDecoder(readFile)
	if err = jsonParser.Decode(&readList); err != nil {
		fmt.Fprintf(os.Stderr, "Error while parsing read JSON file\n")
		log.Fatal(err)
		os.Exit(-1)
	}

	//for i, v := range articles {
	//fmt.Fprintf("title: %s, link: %s\n", article.Title, article.Link)
}<commit_msg>Implement first version of selectLink<commit_after>package sel

import (
	"os"
	"os/exec"
	"flag"
	"fmt"
	"log"
	"time"
	"math/rand"
	"encoding/json"
	"link-select/types"
)

func SelectLink(arg *flag.Flag, file string, browser string) {
	fmt.Fprintf(os.Stdout, "Selecting from %s\n", file)

	// open JSON file
	readFile, err := os.Open(file)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error while opening %s\n", file)
		os.Exit(-1)
	}

	// decode JSON file into custom type ReadList
	var readList types.ReadList
	jsonParser := json.NewDecoder(readFile)
	if err = jsonParser.Decode(&readList); err != nil {
		fmt.Fprintf(os.Stderr, "Error while parsing %s\n", file)
		log.Fatal(err)
		readFile.Close()
		os.Exit(-1)
	}

	// set new seed to get non-deterministic random number generation
	randSrc := rand.NewSource(time.Now().UnixNano())
	r := rand.New(randSrc)

	// select random article
	rndArticle := r.Intn(len(readList))
	fmt.Fprintf(os.Stdout, "Selected \"%s\"\n", readList[rndArticle].Title)
	
	// open link in browser
	cmd := exec.Command(browser, readList[rndArticle].Link)
	err = cmd.Run()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error while opening %s in %s\n",
			readList[rndArticle], browser)
		log.Fatal(err)
		readFile.Close()
		os.Exit(-1)
	}

	// remove selected article from list
	readList = append(readList[:rndArticle], readList[rndArticle + 1:]...)

	// write back JSON file without selected article
	readFile.Close()
	readFile, err = os.Create(file)
	jsonWriter := json.NewEncoder(readFile)
	if err = jsonWriter.Encode(&readList); err != nil {
		fmt.Fprintf(os.Stderr, "Error while writing back %s\n", file)
		log.Fatal(err)
		readFile.Close()
		os.Exit(-1)
	}

	readFile.Close()
}
{'fraction_non_alphanumeric': 0.15804837364470392, 'fraction_numerical': 0.0029190992493744786, 'mean_word_length': 3.8959183673469386, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2695831', 'n_tokens_mistral': 949, 'n_tokens_neox': 898, 'n_words': 217}