| 35 | } |
| 36 | |
| 37 | function transform (word, language, data, { include }) { |
| 38 | return data |
| 39 | .map(e => e.entry) |
| 40 | .filter(e => e) |
| 41 | .reduce((accumulator, entry) => { |
| 42 | if (!entry.subentries) { return accumulator.push(entry) && accumulator; } |
| 43 | |
| 44 | let { subentries } = entry, |
| 45 | mappedSubentries; |
| 46 | |
| 47 | if (subentries.length > 1) { |
| 48 | utils.logEvent(word, language, 'subentries length is greater than 1', { data }); |
| 49 | } |
| 50 | |
| 51 | if (entry.sense_families) { |
| 52 | utils.logEvent(word, language, 'entry has subentries and sense families', { data }); |
| 53 | } |
| 54 | |
| 55 | if (entry.etymology) { |
| 56 | utils.logEvent(word, language, 'entry has subentries and etymology', { data }); |
| 57 | } |
| 58 | |
| 59 | mappedSubentries = subentries |
| 60 | .map((subentry) => { |
| 61 | if (subentry.sense_families) { |
| 62 | utils.logEvent(word, language, 'subentry has sense families', { data }); |
| 63 | } |
| 64 | |
| 65 | if (subentry.sense_family) { |
| 66 | subentry.sense_families = []; |
| 67 | subentry.sense_families.push(subentry.sense_family); |
| 68 | } |
| 69 | |
| 70 | return _.defaults(subentry, _.pick(entry, ['phonetics', 'etymology'])) |
| 71 | }) |
| 72 | |
| 73 | return accumulator.concat(mappedSubentries); |
| 74 | }, []) |
| 75 | .map((entry) => { |
| 76 | let { headword, lemma, phonetics = [], etymology = {}, sense_families = [] } = entry; |
| 77 | |
| 78 | return { |
| 79 | word: lemma || headword, |
| 80 | phonetic: _.get(phonetics, '0.text'), |
| 81 | phonetics: phonetics.map((e) => { |
| 82 | return { |
| 83 | text: e.text, |
| 84 | audio: e.oxford_audio |
| 85 | }; |
| 86 | }), |
| 87 | origin: _.get(etymology, 'etymology.text'), |
| 88 | meanings: sense_families.map((sense_family) => { |
| 89 | let { parts_of_speech, senses = []} = sense_family; |
| 90 | |
| 91 | // if parts of speech is empty at this level. |
| 92 | // Current hypothesis tells that it means only one sense is present |
| 93 | // We need to take out parts_of_speech from it and use it. |
| 94 | if (!parts_of_speech) { |