mirror of
https://github.com/arnaucube/goRecommender.git
synced 2026-02-06 19:16:39 +01:00
added initial items, implemented route newUser, starting recommendations, added tests.sh file
This commit is contained in:
28
README.md
28
README.md
@@ -8,3 +8,31 @@ Applies Machine Learning to perform recommendations:
|
|||||||
|
|
||||||
- Random Forests
|
- Random Forests
|
||||||
- K Nearest Neighbours
|
- K Nearest Neighbours
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Add new user
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /user
|
||||||
|
{
|
||||||
|
id: "user1",
|
||||||
|
age: 30
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
- Get recommendations
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /r/{userid}/{nrec}
|
||||||
|
|
||||||
|
{userid}: is the userid
|
||||||
|
{nrec}: number of recommendations requested
|
||||||
|
```
|
||||||
|
|||||||
57
color.go
57
color.go
@@ -1,57 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
//Color struct, defines the color
|
|
||||||
type Color struct{}
|
|
||||||
|
|
||||||
var c Color
|
|
||||||
|
|
||||||
//DarkGray color
|
|
||||||
func (c Color) DarkGray(t string) {
|
|
||||||
fmt.Print("\x1b[30;1m") //dark gray
|
|
||||||
fmt.Println(t)
|
|
||||||
fmt.Print("\x1b[0m") //defaultColor
|
|
||||||
}
|
|
||||||
|
|
||||||
//Red color
|
|
||||||
func (c Color) Red(t string) {
|
|
||||||
fmt.Print("\x1b[31;1m") //red
|
|
||||||
fmt.Println(t)
|
|
||||||
fmt.Print("\x1b[0m") //defaultColor
|
|
||||||
}
|
|
||||||
|
|
||||||
//Green color
|
|
||||||
func (c Color) Green(t string) {
|
|
||||||
fmt.Print("\x1b[32;1m") //green
|
|
||||||
fmt.Println(t)
|
|
||||||
fmt.Print("\x1b[0m") //defaultColor
|
|
||||||
}
|
|
||||||
|
|
||||||
//Yellow color
|
|
||||||
func (c Color) Yellow(t string) {
|
|
||||||
fmt.Print("\x1b[33;1m") //yellow
|
|
||||||
fmt.Println(t)
|
|
||||||
fmt.Print("\x1b[0m") //defaultColor
|
|
||||||
}
|
|
||||||
|
|
||||||
//Blue color
|
|
||||||
func (c Color) Blue(t string) {
|
|
||||||
fmt.Print("\x1b[34;1m") //blue
|
|
||||||
fmt.Println(t)
|
|
||||||
fmt.Print("\x1b[0m") //defaultColor
|
|
||||||
}
|
|
||||||
|
|
||||||
//Purple color
|
|
||||||
func (c Color) Purple(t string) {
|
|
||||||
fmt.Print("\x1b[35;1m") //purple
|
|
||||||
fmt.Println(t)
|
|
||||||
fmt.Print("\x1b[0m") //defaultColor
|
|
||||||
}
|
|
||||||
|
|
||||||
//Cyan color
|
|
||||||
func (c Color) Cyan(t string) {
|
|
||||||
fmt.Print("\x1b[36;1m") //cyan
|
|
||||||
fmt.Println(t)
|
|
||||||
fmt.Print("\x1b[0m") //defaultColor
|
|
||||||
}
|
|
||||||
5
itemSamples.data
Normal file
5
itemSamples.data
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
item1
|
||||||
|
item2
|
||||||
|
item3
|
||||||
|
item4
|
||||||
|
item5
|
||||||
16
main.go
16
main.go
@@ -5,18 +5,30 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
mgo "gopkg.in/mgo.v2"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var userCollection *mgo.Collection
|
||||||
|
var itemCollection *mgo.Collection
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("starting")
|
fmt.Println("starting")
|
||||||
|
|
||||||
//mongodb start
|
//mongodb start
|
||||||
readMongodbConfig("./mongodbConfig.json")
|
readMongodbConfig("./mongodbConfig.json")
|
||||||
c := connectMongodb()
|
session, err := getSession()
|
||||||
fmt.Println(c)
|
check(err)
|
||||||
|
userCollection = getCollection(session, "users")
|
||||||
|
itemCollection = getCollection(session, "items")
|
||||||
color.Green("mongodb connected")
|
color.Green("mongodb connected")
|
||||||
|
|
||||||
|
//read items dataset
|
||||||
|
itemsDataset := readDataset("./itemSamples.data", "\n", ",")
|
||||||
|
items := getItemsFromDataset(itemsDataset)
|
||||||
|
datasetToMongodbIfNotExist(itemCollection, items)
|
||||||
|
|
||||||
//http server start
|
//http server start
|
||||||
readServerConfig("./serverConfig.json")
|
readServerConfig("./serverConfig.json")
|
||||||
color.Green("server running")
|
color.Green("server running")
|
||||||
|
|||||||
54
mongodb.go
54
mongodb.go
@@ -7,13 +7,13 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
|
|
||||||
mgo "gopkg.in/mgo.v2"
|
mgo "gopkg.in/mgo.v2"
|
||||||
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
//MongoConfig stores the configuration of mongodb to connect
|
//MongoConfig stores the configuration of mongodb to connect
|
||||||
type MongoConfig struct {
|
type MongoConfig struct {
|
||||||
Ip string `json:"ip"`
|
Ip string `json:"ip"`
|
||||||
Database string `json:"database"`
|
Database string `json:"database"`
|
||||||
Collection string `json:"collection"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var mongoConfig MongoConfig
|
var mongoConfig MongoConfig
|
||||||
@@ -42,17 +42,9 @@ func getSession() (*mgo.Session, error) {
|
|||||||
|
|
||||||
return session, err
|
return session, err
|
||||||
}
|
}
|
||||||
func getCollection(session *mgo.Session) *mgo.Collection {
|
func getCollection(session *mgo.Session, collection string) *mgo.Collection {
|
||||||
|
|
||||||
c := session.DB(mongoConfig.Database).C(mongoConfig.Collection)
|
c := session.DB(mongoConfig.Database).C(collection)
|
||||||
return c
|
|
||||||
}
|
|
||||||
func connectMongodb() *mgo.Collection {
|
|
||||||
session, err := getSession()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
c := getCollection(session)
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,3 +62,39 @@ func saveDataEntryToMongo(c *mgo.Collection, user UserModel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func saveItem(c *mgo.Collection, item ItemModel) {
|
||||||
|
//first, check if the item already exists
|
||||||
|
result := ItemModel{}
|
||||||
|
err := c.Find(bson.M{"id": item.ID}).One(&result)
|
||||||
|
if err != nil {
|
||||||
|
//item not found, so let's add a new entry
|
||||||
|
err = c.Insert(item)
|
||||||
|
check(err)
|
||||||
|
} else {
|
||||||
|
/*result.Data = append(result.Data, dataEntry)
|
||||||
|
err = c.Update(bson.M{"id": dataEntry.ContratoCOD}, result)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveUser(c *mgo.Collection, user UserModel) {
|
||||||
|
//first, check if the item already exists
|
||||||
|
result := UserModel{}
|
||||||
|
err := c.Find(bson.M{"id": user.ID}).One(&result)
|
||||||
|
if err != nil {
|
||||||
|
//item not found, so let's add a new entry
|
||||||
|
err = c.Insert(user)
|
||||||
|
check(err)
|
||||||
|
} else {
|
||||||
|
/*result.Data = append(result.Data, dataEntry)
|
||||||
|
err = c.Update(bson.M{"id": dataEntry.ContratoCOD}, result)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
{
|
{
|
||||||
"ip": "127.0.0.1",
|
"ip": "127.0.0.1",
|
||||||
"database": "goRecommend",
|
"database": "goRecommend"
|
||||||
"collection": "userHistory"
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
type UserModel struct {
|
type UserModel struct {
|
||||||
Userid string
|
ID string `json:"id"`
|
||||||
History []string
|
Age int `json:",string"`
|
||||||
|
Actions []string
|
||||||
}
|
}
|
||||||
|
type ItemModel struct {
|
||||||
|
ID string
|
||||||
|
TRecommended int
|
||||||
|
TActed int
|
||||||
|
}
|
||||||
|
|
||||||
|
var scores []float64
|
||||||
|
var ranking []ItemModel
|
||||||
|
|||||||
43
readItemSamples.go
Normal file
43
readItemSamples.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
mgo "gopkg.in/mgo.v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func readDataset(path string, lineSeparation string, valueSeparation string) [][]string {
|
||||||
|
var dataset [][]string
|
||||||
|
b, err := ioutil.ReadFile(path)
|
||||||
|
check(err)
|
||||||
|
str := string(b)
|
||||||
|
str = strings.Replace(str, "\r", "", -1)
|
||||||
|
lines := strings.Split(str, lineSeparation)
|
||||||
|
for _, v1 := range lines {
|
||||||
|
params := strings.Split(v1, valueSeparation)
|
||||||
|
var datasetLine []string
|
||||||
|
for _, v2 := range params {
|
||||||
|
datasetLine = append(datasetLine, v2)
|
||||||
|
}
|
||||||
|
dataset = append(dataset, datasetLine)
|
||||||
|
}
|
||||||
|
return dataset
|
||||||
|
}
|
||||||
|
func getItemsFromDataset(dataset [][]string) []ItemModel {
|
||||||
|
var items []ItemModel
|
||||||
|
for _, v := range dataset {
|
||||||
|
var newItem ItemModel
|
||||||
|
newItem.ID = v[0]
|
||||||
|
items = append(items, newItem)
|
||||||
|
}
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
|
func datasetToMongodbIfNotExist(c *mgo.Collection, items []ItemModel) {
|
||||||
|
fmt.Println(items)
|
||||||
|
for _, item := range items {
|
||||||
|
saveItem(c, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Routes []Route
|
type Routes []Route
|
||||||
@@ -15,18 +18,45 @@ var routes = Routes{
|
|||||||
Index,
|
Index,
|
||||||
},
|
},
|
||||||
Route{
|
Route{
|
||||||
"NewImage",
|
"Recommendations",
|
||||||
|
"GET",
|
||||||
|
"/r/{userid}/{nrec}",
|
||||||
|
Recommendations,
|
||||||
|
},
|
||||||
|
Route{
|
||||||
|
"NewUser",
|
||||||
"POST",
|
"POST",
|
||||||
"/image",
|
"/user",
|
||||||
NewImage,
|
NewUser,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
//ROUTES
|
//ROUTES
|
||||||
|
|
||||||
func Index(w http.ResponseWriter, r *http.Request) {
|
func Index(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, "send images to the /image path")
|
fmt.Fprintln(w, "ask for recommendations in /r")
|
||||||
}
|
}
|
||||||
func NewImage(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Fprintln(w, "response")
|
func NewUser(w http.ResponseWriter, r *http.Request) {
|
||||||
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
var newUser UserModel
|
||||||
|
err := decoder.Decode(&newUser)
|
||||||
|
check(err)
|
||||||
|
defer r.Body.Close()
|
||||||
|
|
||||||
|
saveUser(userCollection, newUser)
|
||||||
|
|
||||||
|
fmt.Println(newUser)
|
||||||
|
fmt.Fprintln(w, "new user added: ", newUser.ID)
|
||||||
|
}
|
||||||
|
func Recommendations(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
userid := vars["userid"]
|
||||||
|
nrec := vars["nrec"]
|
||||||
|
fmt.Println(userid)
|
||||||
|
fmt.Println(nrec)
|
||||||
|
|
||||||
|
//now, get recommendations
|
||||||
|
|
||||||
|
fmt.Fprintln(w, "recommendations")
|
||||||
}
|
}
|
||||||
|
|||||||
15
tests.sh
Normal file
15
tests.sh
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
echo "------------------"
|
||||||
|
echo "[Adding new users]"
|
||||||
|
|
||||||
|
echo http http://127.0.0.1:3056/user id="user1" age=23
|
||||||
|
http http://127.0.0.1:3056/user id="user1" age=23
|
||||||
|
|
||||||
|
echo http http://127.0.0.1:3056/user id="user2" age=32
|
||||||
|
http http://127.0.0.1:3056/user id="user2" age=32
|
||||||
|
|
||||||
|
echo "------------------"
|
||||||
|
|
||||||
|
echo "[Getting recommendations for user]"
|
||||||
|
|
||||||
|
echo http http://127.0.0.1:3056/user1/3
|
||||||
|
http http://127.0.0.1:3056/r/user1/3
|
||||||
Reference in New Issue
Block a user