🌐 AI搜索 & 代理 主页
Skip to content

Commit 3784edd

Browse files
authored
Merge pull request #23 from opensource9ja/more-docs
Updates to the docs generation process. Updates to Readme
2 parents 22aedb2 + 2f8f2b0 commit 3784edd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2320
-654
lines changed

README.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,59 @@
11
# scikit.js
22

3-
JavaScript package for predictive data analysis and machine learning. Generic math operations are powered by Tensorflowjs core layer.
3+
JavaScript package for predictive data analysis and machine learning.
44

5-
<img width="596" alt="135392530-81ed4901-10fc-4d74-9fec-da8c968573f5" src="https://user-images.githubusercontent.com/29900845/137105982-f1a51ad5-9adb-46c3-9dfc-d3a23e36d93f.png" />
5+
Aims to be a Typescript port of the [scikit-learn](https://scikit-learn.org) python library.
66

7-
> We are modeling the Scikit.js API after the original [Scikit-learn Python](https://scikit-learn.org/) library.
7+
This library is for users who wish to train or deploy their models to JS environments (browser, mobile) but with a familiar API.
88

9-
## Contributing Guide
9+
Generic math operations are powered by [Tensorflow.js](https://www.tensorflow.org/js) core layer for faster calculation.
1010

11-
See guide [here](https://github.com/opensource9ja/scikit.js/blob/dev/CONTRIBUTING_GUIDE.md)
11+
Documentation site: [www.scikitjs.org](https://www.scikitjs.org)
12+
13+
<img width="396" alt="135392530-81ed4901-10fc-4d74-9fec-da8c968573f5" src="https://user-images.githubusercontent.com/29900845/137105982-f1a51ad5-9adb-46c3-9dfc-d3a23e36d93f.png" />
14+
15+
# Installation
16+
17+
### Frontend Users
18+
19+
For use with modern bundlers in a frontend application, simply
20+
21+
```js
22+
yarn add scikitjs
23+
```
24+
25+
### Backend Users
26+
27+
For Node.js users who wish to bind to the Tensorflow C++ library, simply
28+
29+
```js
30+
yarn add scikitjs-node
31+
```
1232

13-
## Documentation
33+
### Script src
1434

15-
TODO
35+
For those that wish to use script src tags, simply
36+
37+
```js
38+
<script src="https://unpkg.com/scikitjs/dist/scikit.min.js"></script>
39+
```
40+
41+
## Simple Example
42+
43+
```js
44+
import { LinearRegression } from 'scikitjs'
45+
46+
const lr = LinearRegression({ fitIntercept: false })
47+
const X = [[1], [2]] // 2D Matrix with a single column vector
48+
const y = [10, 20]
49+
50+
await lr.fit(X, y)
51+
52+
lr.predict([[3, 4]]) // roughly [30, 40]
53+
console.log(lr.coef)
54+
console.log(lr.intercept)
55+
```
56+
57+
## Contributing Guide
58+
59+
See guide [here](https://github.com/opensource9ja/scikit.js/blob/dev/CONTRIBUTING_GUIDE.md)

docs/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ docs/api
1919
npm-debug.log*
2020
yarn-debug.log*
2121
yarn-error.log*
22+
23+
# Build files for the src code jsdoc comments
24+
out.json
25+
26+
# Autogenerated docs file from source code
27+
docs/Api.md

docs/convert.js

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
function getText(obj) {
2+
let example = obj?.tags
3+
?.filter((el) => el.tag === 'example')
4+
?.map((el) => el.text)
5+
?.join('\n')
6+
7+
if (example?.length > 0) {
8+
example = `\n#### Example ${example}\n`
9+
}
10+
return `${obj?.shortText || ''}${obj?.text || ''}${example || ''}`
11+
}
12+
13+
function toTableParameter(obj, bigObj) {
14+
if (obj?.comment?.shortText) {
15+
return `${obj.name} | ${getTypeName(
16+
obj.type,
17+
bigObj
18+
)} | ${obj.comment.shortText.replace(/\n/g, ' ')}`
19+
}
20+
return `${obj.name} | ${getTypeName(obj.type, bigObj)}`
21+
}
22+
23+
function generateTable(parameters, bigObj, isObject) {
24+
if (!parameters) {
25+
return ''
26+
}
27+
if (parameters.length === 0) {
28+
return ''
29+
}
30+
let noDescriptionHeader = `#### ${isObject ? 'Object ' : ''} Parameters
31+
32+
Name | Type
33+
:----| :----`
34+
35+
let someDescriptionHeader = `#### ${isObject ? 'Object ' : ''} Parameters
36+
37+
Name | Type | Description
38+
:----| :---- | :---------`
39+
40+
let hasDescription =
41+
parameters.map((el) => el?.comment?.shortText).filter((el) => Boolean(el))
42+
.length > 0
43+
44+
return `
45+
${hasDescription ? someDescriptionHeader : noDescriptionHeader}
46+
${parameters.map((el) => toTableParameter(el, bigObj)).join('\n')}
47+
`
48+
}
49+
50+
function generateMarkdownFromMethod(obj, className, bigObj) {
51+
let signature = obj.signatures[0]
52+
let parameters = signature.parameters
53+
let md = `\`\`\`typescript\n${obj.name}(${parameters
54+
?.map((el) => `${el.name}${el?.flags?.isOptional ? '?' : ''}`)
55+
?.join(', ')}): ${
56+
signature.type.name === 'default'
57+
? className
58+
: getTypeName(signature.type, bigObj)
59+
}\n\`\`\`\n\n${signature?.comment?.shortText || ''}\n${generateTable(
60+
parameters,
61+
bigObj
62+
)}
63+
`
64+
return md
65+
}
66+
67+
function getInterfaceForClass(jsonClass, bigObj) {
68+
let constructor = jsonClass.children.filter(
69+
(el) => el.kindString === 'Constructor'
70+
)[0]
71+
72+
// Generate function call
73+
74+
const signatures = constructor?.signatures
75+
const sig = signatures && signatures[0]
76+
const parameters = sig?.parameters
77+
78+
const param = parameters && parameters[0]
79+
const type = param?.type
80+
if (type) {
81+
let interface = bigObj.children.filter(
82+
(el) => el.id === type.id && el.kindString === 'Interface'
83+
)[0]
84+
85+
return interface
86+
}
87+
return undefined
88+
}
89+
90+
function generateConstructor(jsonClass, bigObj) {
91+
let constructor = jsonClass.children.filter(
92+
(el) => el.kindString === 'Constructor'
93+
)[0]
94+
95+
let interface = getInterfaceForClass(jsonClass, bigObj)
96+
const signatures = constructor?.signatures
97+
const sig = signatures && signatures[0]
98+
const parameters = sig?.parameters
99+
const noArgs = !parameters
100+
let constructorInvocation = `\`\`\`js\nnew ${jsonClass.name}(${
101+
noArgs ? '' : '{ object }'
102+
})\n\`\`\``
103+
104+
if (interface && interface.children) {
105+
constructorInvocation = `\`\`\`js\nnew ${
106+
jsonClass.name
107+
}({ ${interface.children
108+
?.map((el) => `${el.name}${el?.flags?.isOptional ? '?' : ''}`)
109+
?.join(', ')}})\n\`\`\``
110+
111+
return `### Constructor
112+
${constructorInvocation}
113+
${noArgs ? '' : generateTable(interface.children, {}, true)}`
114+
}
115+
116+
return `### Constructor
117+
${constructorInvocation}
118+
${
119+
noArgs ? '' : generateMarkdownFromMethod(constructor, jsonClass.name, bigObj)
120+
}
121+
`
122+
}
123+
124+
function generateAllMethods(jsonClass) {
125+
return `
126+
### Methods
127+
${jsonClass.children
128+
.filter((el) => el.kindString === 'Method' && el?.flags?.isPublic)
129+
.map((el) => generateMarkdownFromMethod(el, jsonClass.name))
130+
.join('\n---\n')}
131+
`
132+
}
133+
134+
function readableStringsForCommonTypes({ id, name }, bigObj) {
135+
if (name === 'Scikit1D') {
136+
return 'number[] \\| string[] \\| boolean[] \\| TypedArray \\| Tensor1D \\| Series'
137+
}
138+
if (name === 'Scikit2D') {
139+
return '(number \\| string \\| boolean)[][] \\| TypedArray[] \\| Tensor2D \\| Dataframe'
140+
}
141+
142+
// if (id && bigObj && bigObj.children) {
143+
// let interface = bigObj.children.filter(
144+
// (el) => el.id === id && el.kindString === 'Interface'
145+
// )[0]
146+
147+
// if (interface && interface.children) {
148+
// return generateTable(interface.children)
149+
// }
150+
// }
151+
152+
return name
153+
}
154+
155+
function getTypeName({ id, type, name, value, elementType, types }, bigObj) {
156+
if (type === 'reference' || type === 'intrinsic') {
157+
return readableStringsForCommonTypes({ id, name }, bigObj)
158+
}
159+
if (type === 'literal') {
160+
if (typeof value === 'string') {
161+
return `"${value}"`
162+
}
163+
return value
164+
}
165+
166+
if (type === 'array') {
167+
let typeName = getTypeName(elementType, bigObj)
168+
if (typeName.includes('|')) {
169+
return `(${typeName})[]`
170+
}
171+
return `${typeName}[]`
172+
}
173+
if (type === 'union') {
174+
return types.map((el) => getTypeName(el, bigObj)).join(' \\| ')
175+
}
176+
177+
return ''
178+
}
179+
180+
function generateProperties(jsonClass, bigObj) {
181+
// console.log(jsonClass.children)
182+
let interface = getInterfaceForClass(jsonClass, bigObj)
183+
let allConstructorArgs = []
184+
if (interface && interface.children) {
185+
allConstructorArgs = interface.children.map((el) => el.name)
186+
}
187+
let properties = jsonClass.children.filter(
188+
(el) =>
189+
el.kindString === 'Property' && !allConstructorArgs.includes(el.name)
190+
)
191+
let propertiesText = properties
192+
.map((el) => {
193+
return `**\`${el.name}\`**: ${getTypeName(el.type, bigObj)}\n\n${
194+
el?.comment?.shortText || ''
195+
}`
196+
})
197+
.join('\n\n')
198+
return `
199+
### Properties
200+
201+
${
202+
properties.length === 0
203+
? `The only properties are the arguments defined above.\n`
204+
: `All of the constructor arguments above are class properties as well as \n`
205+
}
206+
207+
${propertiesText}
208+
`
209+
}
210+
211+
function writeClass(jsonClass, bigObj) {
212+
return `
213+
## ${jsonClass.name}
214+
215+
${getText(jsonClass.comment)}
216+
${generateConstructor(jsonClass, bigObj)}
217+
${generateProperties(jsonClass, bigObj)}
218+
${generateAllMethods(jsonClass)}
219+
220+
`
221+
}
222+
223+
function doTheWholeThing(bigObj) {
224+
let result = bigObj.children
225+
.filter((el) => el.kindString === 'Class')
226+
.map((el) => writeClass(el, bigObj))
227+
.join('\n')
228+
229+
return `---
230+
sidebar_position: 2
231+
---
232+
233+
# API
234+
235+
${result}
236+
`
237+
}
238+
239+
let myObj = require('./out.json')
240+
console.log(doTheWholeThing(myObj))

0 commit comments

Comments
 (0)