ref: 744f8222f90cdf4ce2500036f01071d2c53878ae DWCHelper/go/graveyard -rw-r--r-- 3.2 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// getAliases pulls the alias database from the repository or sets up
// a default one. It returns a map of alias strings to their possible term
func getAliases(terms []string) map[string]string { 
	//  map for storing possible aliases
	var aliases = make(map[string]string)
	
	// Try to pull the alias file from online
	resp, err := http.Get(aliasURL)
	if err != nil {
	 	fmt.Printf("Cannot pull aliases from upstream: %v\n", err.Error())
	 	fmt.Println("Defaulting to an automatically generated alias list...")
	} else { // Try to read the file
		contents, err := ioutil.ReadAll(resp.Body)
		defer resp.Body.Close()
		if err != nil {
	 		fmt.Println("Error: cannot read contents of aliasURL", err.Error())
	 		fmt.Println("Defaulting to an automatically generated alias list...")
			
	 	} else { // Try to parse using csv
			r := csv.NewReader(strings.NewReader(string(contents)))
			r.FieldsPerRecord = -1 // uneven fields numbers allowed
			rows, err := r.ReadAll()
			if err != nil {
				fmt.Println("Cannot read CSV data from alias file:", err.Error())
				fmt.Println("Defaulting to an automatically generated alias list...")
			} else {

				// If there were no errors pulling the aliases
				// from the online CSV file, add obvious
				// variations of the alias to the aliases map
				for _, row := range rows {
					term := row[0]
					for _, entry := range row[1:] {
						//					fmt.Println("running addAliases(",entry,"aliases",term)
						addAliases(entry, aliases, term)
						
					}
				}
			}
		}
		
	}

	// Generate default aliases from the DWC term names themselves
	// for _, term := range terms {
	// 	//		fmt.Println("running addAliases(",term,"aliases",term)

	// 	addAliases(term, aliases, term)
	// }
	return aliases
}

// addAliases is a helper function for getAliases. It takes a word in
// camelCase and maps all obvious variations to the given aliases map
func addAliases(word string, aliases map[string]string, term string) {
	words := camelcase.Split(word)
	aliases[strings.Join(words, "")] = term
	aliases[strings.Title(strings.Join(words, " "))] = term
	aliases[strings.ToLower(strings.Join(words, " "))] = term
	//	fmt.Println(strings.Join(words, " "))
}

// showAliases is a temporary function (for debugging) that shows all
// aliases mapped to a term
func showAliases(term string, aliases map[string]string) {
	for key, value := range aliases {
		if value == term {
			fmt.Print(key,", ")
		}
	}
	for _, word := range camelcase.Split(term) {
		fmt.Print(word, ", ")
	}
}

// database holds all of the variables and their data
type database struct {
	data  map[string]column // maps terms to data
	terms []string          // ordered list of terms
}

// column holds one column of the database (not including the name of the term)
type column struct {
	values                     []string // values are stored in strings
	varType, alias, definition string   // other metadata
	hasDifferentValues         bool     // whether the values change for each specimen (good indicator that it's a useful variable)
}

const referenceURL string = "http://rs.tdwg.org/dwc/terms/"

// showReference returns the URL for a term on the Darwin Core website,
// which includes a definition, comments and examples
func showReference(term string) string {
	return referenceURL + "#" + term
}