Skip to content

Commit d9cfe07

Browse files
Move morphological generation to wordforms route
Closes #12
1 parent 3bc03e0 commit d9cfe07

File tree

6 files changed

+91
-101
lines changed

6 files changed

+91
-101
lines changed

app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ app.use('/sources', require('./routes/sources'))
114114
app.use('/feedback', require('./routes/feedback'))
115115
app.use('/logs', require('./routes/logs'))
116116
app.use('/i18n', require('./routes/i18n'))
117-
app.use('/morpho', require('./routes/morpho'))
118117

119118
// http://stackoverflow.com/a/27464258/98600
120119
// app.use('/json-editor', express.static(__dirname + '/node_modules/json-editor/dist/'))

public/javascripts/generate.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ new Vue({
5151
if (!this.lexeme) return
5252
this.working = true
5353
this.error = null
54-
let url = `${baseURL}/morpho/generate/${this.paradigm}`
55-
axios.post(url, this.lexeme)
54+
let url = `${baseURL}/wordforms/generate/${this.paradigm}/${this.lexemeID}`
55+
axios.post(url, {
56+
lemma: this.lexeme.lemma,
57+
commit: false
58+
})
5659
.then(response => {
5760
this.wordforms = response.data
5861
this.tested = true
@@ -70,8 +73,11 @@ new Vue({
7073
if (!this.lexeme) return
7174
this.working = true
7275
this.error = null
73-
let url = `${baseURL}/morpho/generate/${this.paradigm}/${this.lexemeID}`
74-
axios.post(url, this.lexeme)
76+
let url = `${baseURL}/wordforms/generate/${this.paradigm}/${this.lexemeID}`
77+
axios.post(url, {
78+
lemma: this.lexeme.lemma,
79+
commit: true
80+
})
7581
.then(response => {
7682
window.location = `${baseURL}/search?id=${this.lexemeID}`
7783
})

public/markdown/api-admin.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ Non-public API for editing. These methods generally require authorisation.
1616

1717
## Wordforms
1818

19-
| Method | URL | Description | Payload* | Return Value |
20-
|:---------|:--------------------------------|:----------------------------|:--------------------------------------------------------|:---------------------------|
21-
| `POST` | `/wordforms/` | Create | Entire document | Document |
22-
| `GET` | `/wordforms/:id` | Read | - | Document |
23-
| `POST` | `/wordforms/:id` | Update | Entire document | Document |
24-
| `DELETE` | `/wordforms/:id` | Delete | - | - |
25-
| `POST` | `/wordforms/replace/:lexeme_id` | Search/replace in wordforms | `{search: (regex), replace: (string), commit: boolean}` | List of affected documents |
19+
| Method | URL | Description | Payload* | Return Value |
20+
|:---------|:--------------------------------------------|:---------------------------------------------------------------|:-----------------------------------------------------------|:--------------------|
21+
| `POST` | `/wordforms/` | Create | Entire document | Document |
22+
| `GET` | `/wordforms/:id` | Read | - | Document |
23+
| `POST` | `/wordforms/:id` | Update | Entire document | Document |
24+
| `DELETE` | `/wordforms/:id` | Delete | - | - |
25+
| `POST` | `/wordforms/replace/:lexeme_id` | Search/replace in wordforms | `{search: (string), replace: (string), commit: (boolean)}` | Affected documents |
26+
| `POST` | `/wordforms/generate/:paradigm/:lexeme_id?` | Generate inflections (`lexeme_id` is required when committing) | `{lemma: (string), commit: (boolean)}` | Generated documents |
2627

2728
## Roots
2829

@@ -46,10 +47,3 @@ Non-public API for editing. These methods generally require authorisation.
4647
| Method | URL | Description | Payload* | Return Value |
4748
|:-------|:--------------------|:------------------------------------|:----------------------------------------------------------------------|:-------------|
4849
| `POST` | `/feedback/suggest` | Add suggestion (checks for matches) | Entire document (only the fields `lemma`, `gloss` and `pos` are used) | Document |
49-
50-
## Morphological generation
51-
52-
| Method | URL | Description | Payload* | Return Value |
53-
|:-------|:----------------------------------------|:------------------------------|:-------------------|:-------------|
54-
| `POST` | `/morpho/generate/:paradigm` | Generate inflections | `{ lemma: '...' }` | Documents |
55-
| `POST` | `/morpho/generate/:paradigm/:lexeme_id` | Generate & insert inflections | `{ lemma: '...' }` | - |

routes/morpho.js

Lines changed: 0 additions & 82 deletions
This file was deleted.

routes/wordforms.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,79 @@ var monk = require('monk')
88
var log = require('../logger').makeLogger('wordforms')
99
var sortWordforms = require('./helpers/sort-wordforms')
1010

11+
// -- Morphological generation -----------------------------------------------
12+
13+
/* Generate */
14+
/* Content-Type: application/json */
15+
router.post('/generate/:paradigm/:lexeme_id?',
16+
passport.authenticate('basic', {
17+
session: false
18+
}),
19+
function (req, res, next) {
20+
// Load inflector dynamically
21+
var par = req.params.paradigm
22+
var mg
23+
switch (par) {
24+
case 'loan-verb':
25+
case 'adjective':
26+
mg = require('../morpho/' + par)
27+
break
28+
default:
29+
res.status(400).send('Unknown paradigm ' + par)
30+
return
31+
}
32+
33+
mg.inflect(req.body, function (err, forms) {
34+
if (err) {
35+
res.status(400).send(err)
36+
return
37+
}
38+
39+
var lexeme_id = req.params.lexeme_id
40+
if (lexeme_id && req.body.commit === true) {
41+
// Insert into DB
42+
var lemma = req.body.lemma // 'ipparkja'
43+
req.db.get('lexemes').findOne(lexeme_id, function (err, data) {
44+
if (err) {
45+
res.status(400).send('find error')
46+
return
47+
}
48+
if (!data) {
49+
res.status(400).send('lexeme ' + lexeme_id + ' not found')
50+
return
51+
}
52+
if (data.lemma !== lemma) {
53+
res.status(400).send('Lemma mis-match: ' + lemma + ' / ' + data.lemma)
54+
return
55+
}
56+
57+
var coll = req.db.get('wordforms')
58+
async.each(forms,
59+
function (wf, cb) {
60+
wf['lexeme_id'] = data._id
61+
wf['generated'] = true
62+
coll.insert(wf, function (err, data) {
63+
log(req, data._id, wf, 'modified')
64+
cb(err)
65+
})
66+
},
67+
function (err) {
68+
if (err) {
69+
res.status(400).send(err)
70+
return
71+
}
72+
res.json(forms)
73+
}
74+
)
75+
})
76+
} else {
77+
// Just return forms
78+
res.json(forms)
79+
}
80+
})
81+
}
82+
)
83+
1184
// -- Advanced manipulation -------------------------------------------------
1285

1386
/* Replace */

scripts/node/create-indexes.js

100644100755
File mode changed.

0 commit comments

Comments
 (0)