Skip to content

Commit d3879fb

Browse files
authored
Merge pull request #8 from codingpot/refactor-4-paper-method-list
feat: implement PaperMethodList()
2 parents d507620 + f98156f commit d3879fb

File tree

6 files changed

+101
-37
lines changed

6 files changed

+101
-37
lines changed

client.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package paperswithcode_go
22

33
import (
4-
"context"
54
"encoding/json"
65
"errors"
76
"fmt"
87
"github.com/codingpot/paperswithcode-go/internal/transport"
98
"net/http"
10-
"net/url"
119
"time"
1210
)
1311

@@ -53,38 +51,6 @@ type errorResponse struct {
5351
Message string `json:"message"`
5452
}
5553

56-
// Method list used by Paper's ID
57-
type MethodList struct {
58-
Count int `json:"count"`
59-
Methods []Method `json:"results"`
60-
}
61-
62-
type Method struct {
63-
ID string `json:"id"`
64-
Name string `json:"name"`
65-
FullName string `json:"full_name"`
66-
Description string `json:"description"`
67-
Paper string `json:"paper"`
68-
}
69-
70-
func (c *Client) GetMethodList(ctx context.Context, id string) (*MethodList, error) {
71-
fmt.Println(id)
72-
url := fmt.Sprintf("%s/papers/%s/methods", c.BaseURL, url.QueryEscape(id))
73-
req, err := http.NewRequest("GET", url, nil)
74-
if err != nil {
75-
return nil, err
76-
}
77-
78-
req = req.WithContext(ctx)
79-
80-
res := MethodList{}
81-
if err := c.sendRequest(req, &res); err != nil {
82-
return nil, err
83-
}
84-
85-
return &res, nil
86-
}
87-
8854
func (c *Client) sendRequest(req *http.Request, v interface{}) error {
8955
req.Header.Set("Content-Type", "application/json; charset=utf-8")
9056
req.Header.Set("Accept", "application/json; charset=utf-8")

cmd/client/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"context"
54
"fmt"
65
"github.com/codingpot/paperswithcode-go"
76
"os"
@@ -15,7 +14,6 @@ func main() {
1514
}
1615

1716
c := paperswithcode_go.NewClient(paperswithcode_go.WithAPIToken(token))
18-
ctx := context.Background()
1917
paper := "generative adversarial networks"
2018
paper = strings.ReplaceAll(paper, " ", "-")
2119

@@ -25,7 +23,7 @@ func main() {
2523
fmt.Println()
2624
fmt.Println()
2725

28-
methodList, _ := c.GetMethodList(ctx, paper)
26+
methodList, _ := c.PaperMethodList(paper)
2927
fmt.Println(methodList)
3028
fmt.Println()
3129
fmt.Println()

dummy/paper_method_list_response.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"count": 2,
3+
"next": null,
4+
"previous": null,
5+
"results": [
6+
{
7+
"id": "gan",
8+
"name": "GAN",
9+
"full_name": "Generative Adversarial Network",
10+
"description": "A **GAN**, or **Generative Adversarial Network**, is a generative model that simultaneously trains\r\ntwo models: a generative model $G$ that captures the data distribution, and a discriminative model $D$ that estimates the\r\nprobability that a sample came from the training data rather than $G$.\r\n\r\nThe training procedure for $G$ is to maximize the probability of $D$ making\r\na mistake. This framework corresponds to a minimax two-player game. In the\r\nspace of arbitrary functions $G$ and $D$, a unique solution exists, with $G$\r\nrecovering the training data distribution and $D$ equal to $\\frac{1}{2}$\r\neverywhere. In the case where $G$ and $D$ are defined by multilayer perceptrons,\r\nthe entire system can be trained with backpropagation. \r\n\r\n(Image Source: [here](http://www.kdnuggets.com/2017/01/generative-adversarial-networks-hot-topic-machine-learning.html))",
11+
"paper": "generative-adversarial-networks"
12+
},
13+
{
14+
"id": "convolution",
15+
"name": "Convolution",
16+
"full_name": "Convolution",
17+
"description": "A **convolution** is a type of matrix operation, consisting of a kernel, a small matrix of weights, that slides over input data performing element-wise multiplication with the part of the input it is on, then summing the results into an output.\r\n\r\nIntuitively, a convolution allows for weight sharing - reducing the number of effective parameters - and image translation (allowing for the same feature to be detected in different parts of the input space).\r\n\r\nImage Source: [https://arxiv.org/pdf/1603.07285.pdf](https://arxiv.org/pdf/1603.07285.pdf)",
18+
"paper": null
19+
}
20+
]
21+
}

models/methods.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package models
2+
3+
type Method struct {
4+
ID string `json:"id"`
5+
Name string `json:"name"`
6+
FullName string `json:"full_name"`
7+
Description string `json:"description"`
8+
Paper *string `json:"paper"`
9+
}
10+
11+
// MethodList represents methods used in the paper.
12+
type MethodList struct {
13+
Count int `json:"count"`
14+
Next interface{} `json:"next"`
15+
Previous interface{} `json:"previous"`
16+
Results []*Method `json:"results"`
17+
}

paper_method_list.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package paperswithcode_go
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/codingpot/paperswithcode-go/models"
7+
"net/http"
8+
"net/url"
9+
)
10+
11+
func (c *Client) PaperMethodList(paperID string) (*models.MethodList, error) {
12+
pURL := fmt.Sprintf("%s/papers/%s/methods", c.BaseURL, url.QueryEscape(paperID))
13+
response, err := http.Get(pURL)
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
var methodList models.MethodList
19+
err = json.NewDecoder(response.Body).Decode(&methodList)
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
return &methodList, err
25+
}

paper_method_list_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package paperswithcode_go
2+
3+
import (
4+
"github.com/codingpot/paperswithcode-go/models"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func TestClient_PaperMethodList(t *testing.T) {
10+
c := NewClient()
11+
paperID := "generative-adversarial-networks"
12+
got, err := c.PaperMethodList(paperID)
13+
assert.NoError(t, err)
14+
15+
expected := &models.MethodList{
16+
Count: 2,
17+
Next: nil,
18+
Previous: nil,
19+
Results: []*models.Method{
20+
{
21+
ID: "gan",
22+
Name: "GAN",
23+
FullName: "Generative Adversarial Network",
24+
Description: "A **GAN**, or **Generative Adversarial Network**, is a generative model that simultaneously trains\r\ntwo models: a generative model $G$ that captures the data distribution, and a discriminative model $D$ that estimates the\r\nprobability that a sample came from the training data rather than $G$.\r\n\r\nThe training procedure for $G$ is to maximize the probability of $D$ making\r\na mistake. This framework corresponds to a minimax two-player game. In the\r\nspace of arbitrary functions $G$ and $D$, a unique solution exists, with $G$\r\nrecovering the training data distribution and $D$ equal to $\\frac{1}{2}$\r\neverywhere. In the case where $G$ and $D$ are defined by multilayer perceptrons,\r\nthe entire system can be trained with backpropagation. \r\n\r\n(Image Source: [here](http://www.kdnuggets.com/2017/01/generative-adversarial-networks-hot-topic-machine-learning.html))",
25+
Paper: &paperID,
26+
},
27+
{
28+
ID: "convolution",
29+
Name: "Convolution",
30+
FullName: "Convolution",
31+
Description: "A **convolution** is a type of matrix operation, consisting of a kernel, a small matrix of weights, that slides over input data performing element-wise multiplication with the part of the input it is on, then summing the results into an output.\r\n\r\nIntuitively, a convolution allows for weight sharing - reducing the number of effective parameters - and image translation (allowing for the same feature to be detected in different parts of the input space).\r\n\r\nImage Source: [https://arxiv.org/pdf/1603.07285.pdf](https://arxiv.org/pdf/1603.07285.pdf)",
32+
Paper: nil,
33+
},
34+
},
35+
}
36+
assert.Equal(t, expected, got)
37+
}

0 commit comments

Comments
 (0)