This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
music.go
98 lines (88 loc) · 1.81 KB
/
music.go
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
96
97
98
package language
import (
"strings"
)
// SpotifyKeyword is the map for having the music keywords in different languages
var SpotifyKeyword = map[string]SpotifyKeywords{
"en": {
Play: "play",
From: "from",
On: "on",
},
"de": {
Play: "spiele",
From: "von",
On: "auf",
},
"fr": {
Play: "joue",
From: "de",
On: "sur",
},
"es": {
Play: "Juega",
From: "de",
On: "en",
},
"ca": {
Play: "Juga",
From: "de",
On: "a",
},
"it": {
Play: "suona",
From: "da",
On: "a",
},
"tr": {
Play: "Başlat",
From: "dan",
On: "kadar",
},
"nl": {
Play: "speel",
From: "van",
On: "op",
},
"el": {
Play: "αναπαραγωγή",
From: "από",
On: "στο",
},
}
// SpotifyKeywords are the keywords used to get music name
type SpotifyKeywords struct {
Play string
From string
On string
}
// SearchMusic returns a music title and artist found from the given sentence
func SearchMusic(locale, sentence string) (music, artist string) {
words := strings.Split(sentence, " ")
// Iterate through the words of the sentence
playAppeared, fromAppeared, onAppeared := false, false, false
for _, word := range words {
// If "on" appeared
if word == SpotifyKeyword[locale].On {
onAppeared = true
}
// Add the current word if its between from and on
if fromAppeared && !onAppeared {
artist += word + " "
}
// If "from" appeared
if LevenshteinDistance(word, SpotifyKeyword[locale].From) < 2 {
fromAppeared = true
}
// Add the current word if its between play and from
if playAppeared && !fromAppeared && !onAppeared {
music += word + " "
}
// If "play" appeared
if LevenshteinDistance(word, SpotifyKeyword[locale].Play) < 2 {
playAppeared = true
}
}
// Trim the spaces and return
return strings.TrimSpace(music), strings.TrimSpace(artist)
}