Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
referent: "https://gist.githubusercontent.com/alexcesaro/c9c47c638252e21bd82c/raw/bd031237a56ae6691145b4df5617c385dffe930d/list.txt" | |
port: 8080 | |
files: | |
file1: "https://gist.githubusercontent.com/alexcesaro/4ebfa5a9548d053dddb2/raw/abb8525774b63f342e5173d1af89e47a7a39cd2d/file1.txt" | |
file2: "https://gist.githubusercontent.com/alexcesaro/249cde1332f9b2979140/raw/951e43186f14f9c386918d75d715ee49390ebc54/file2.txt" | |
file3: "https://gist.githubusercontent.com/alexcesaro/f99d72a1d1a1f140b27f/raw/e506ed86336ea8561027d9f8cd4007d1f691d835/file3.txt" | |
file4: "https://gist.githubusercontent.com/alexcesaro/77c12bfd58a0d1156d77/raw/e40a381a4bfade72c8fe85e97d83736a48f091e6/file4.txt" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"gopkg.in/yaml.v2" | |
"log" | |
"net/http" | |
) | |
type Config struct { | |
Referent string | |
Port int | |
Files map[string]string | |
} | |
func main() { | |
// Configuration filename in parameter | |
filename := flag.String("cfg", "config.yml", "Configuration file name") | |
flag.Parse() | |
// Read configuration file | |
file, err := ioutil.ReadFile(*filename) | |
if err != nil { | |
log.Fatalln("unable to load configuration file:", err) | |
} | |
// Load configuration from yml file | |
var config Config | |
err = yaml.Unmarshal(file, &config) | |
if err != nil { | |
log.Fatalln("unable to load configuration file:", err) | |
} | |
// Retrieve referent file | |
referent, err := retrieveContent(config.Referent) | |
if err != nil { | |
log.Fatalln("unable to retrieve content of referent file:", err) | |
} | |
// Loop on files | |
results := map[string]int{} | |
for name, address := range(config.Files) { | |
// retrieve content of file | |
raw, err := retrieveContent(address) | |
if err != nil { | |
log.Printf("unable to load file %s:%s\n", name, err) | |
continue | |
} | |
// Get intersect between referent and raw | |
results[name] = intersect(referent, raw) | |
} | |
// Convert results in json | |
display, err := json.Marshal(results) | |
if err != nil { | |
log.Fatalln("unable to format in json:", err) | |
} | |
// Display result | |
fmt.Printf("%s\n", display) | |
} | |
// Calcul interset between s1 and s2 | |
func intersect(s1, s2 map[string]bool) int { | |
// Change smaller list to s1 if needed | |
if len(s1) > len(s2) { | |
s1, s2 = s2, s1 | |
// Revert changes | |
defer func(s1, s2 map[string]bool) { | |
s1, s2 = s2, s1 | |
}(s1, s2) | |
} | |
// Check presence of element of s1 in s2 | |
var count int | |
for val := range s1 { | |
if s2[val] { | |
count++ | |
} | |
} | |
// Retourn summeary of interset | |
return count | |
} | |
// Retrieve content of file at address | |
func retrieveContent(address string) (map[string]bool, error) { | |
// Get file | |
file, err := http.Get(address) | |
if err != nil { | |
return nil, err | |
} | |
// Read file and save words in map | |
data := map[string]bool{} | |
scanner := bufio.NewScanner(file.Body) | |
for scanner.Scan() { | |
data[scanner.Text()] = true | |
} | |
return data, scanner.Err() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment