Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
m80126colin committed Sep 5, 2018
2 parents 467a503 + ade4517 commit 36d3342
Show file tree
Hide file tree
Showing 16 changed files with 4,899 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
dist/
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Hsu Karinsu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
hakka-dict-toolkit
===

[![NPM version](https://badge.fury.io/js/badge-list.svg)][npm]
[![Open Source Love](https://badges.frapsoft.com/os/mit/mit.svg?v=102)][repo]

A toolkit for [Taiwan Hakka Dictionary][site].

Installation
---

```
yarn add hakka-dict-toolkit
```

or

```
npm install hakka-dict-toolkit --save
```

Usage
---

``` js
var hakka = require('hakka-dict-toolkit')

hakka.entry(10377)
```

License
---

MIT

[site]: https://hakka.dict.edu.tw/hakkadict/
[repo]: https://github.com/m80126colin/hakka-dict-toolkit/
[npm]: https://www.npmjs.com/package/hakka-dict-toolkit
3 changes: 2 additions & 1 deletion lib/api/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import * as extracter from '../extracter';
import * as filter from '../filter';
import * as formatter from '../formatter';
import { host } from '../util';
import { WordEntry, CharacterEntry } from '../formatter/_type';
/**
* Provide access to the entry of MOE Hakka Dictionary by index and returns a promise.
*
* @param {number} idx index of entry
* @returns a promise with either a word or a character
*/
const entry = (idx : number) => {
const entry = (idx : number) : Promise<WordEntry | CharacterEntry> => {
const options = {
method: 'GET',
uri: `${host}/result_detail.jsp?n_no=${idx}&soundtype=0&sample=[`
Expand Down
4 changes: 2 additions & 2 deletions lib/extracter/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const extract = (context : string) : ExtractData[][] => {
const $ = cheerio.load(context)
const rows = $('font > table.t14 > tbody > tr', context)
const data = _.map(rows, (row, idx) => {
if (idx !== 12)
if (idx !== 11 && idx !== 12)
return _.flatMap($('tr > td', row), data => partial.extract(data))
// Extract special from of field 'multiple accents (多音字)'
// Extract special from for field 'another accents (又音)' and 'multiple accents (多音字)'
return _.concat(
partial.extract($('tr > td', row)[0]),
_.flatMap($('tr', $(row)), data => partial.extract(data)))
Expand Down
2 changes: 1 addition & 1 deletion lib/filter/character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { replace_list } from '../util';
*/
const filter : Filter = (context : string) : string => _.reduce(
replace_list,
(str, pair) => _.replace(str, new RegExp(pair[0], 'ug'), pair[1]),
(str, pair) => _.replace(str, new RegExp(`&#x${pair[0]};`, 'ug'), pair[1]),
context)

export default filter
2 changes: 1 addition & 1 deletion lib/filter/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { image_table } from '../util';
*/
const filter : Filter = (context : string) : string => {
const $ = cheerio.load(context)
$('img').each(node => {
_.map($('img'), node => {
const link = $(node).attr('src')
$(node).replaceWith(image_table[link])
})
Expand Down
18 changes: 16 additions & 2 deletions lib/formatter/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ const makeAnother = (data : ExtractData[][]) : Sound[] => {
.tail()
.flatMap(acc => partial.another([acc]))
.value()
// another accents
const another = _.chain(data[11])
.tail()
.chunk(2)
.flatMap(partial.another)
.value()
// multiple accents
const multi_accents = _.chain(data[12])
.tail()
.chunk(2)
.flatMap(partial.another)
.value()
return _.concat([], vunpag, multi_accents)
return _.concat([], vunpag, another, multi_accents)
}
/**
* Format synonym or antonym into an array of EntryItem.
Expand Down Expand Up @@ -55,7 +61,15 @@ const makeBasicEntry = (data : ExtractData[][]) : BasicEntry => {
let basic = {
title: partial.item(data[0][1]).text,
type: (data[0][2].text.match('詞性') === null) ? 'character' : 'word',
sounds: _.map(_.range(1, 1 + 6), idx => partial.sound(data[idx])),
sounds: _.chain(_.range(1, 1 + 6))
.map(idx => {
const res = partial.sound(_.tail(data[idx]))
if (_.toPairs(res).length === 0)
return undefined
return _.merge(res, { type: data[idx][0].text })
})
.compact()
.value(),
meaning: data[7][1].text
}
// another accents
Expand Down
10 changes: 7 additions & 3 deletions lib/formatter/partial/_accents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ const pattern = _.chain(list).map(o => o.tag).join('').value()
* @returns {Sound[]} an array of accents
*/
const format = (text : string) : Sound[] => {
const temp = text.match(new RegExp(`([${pattern}]|[0-9a-zA-Z ]+)`, 'ug'))
let state : { vunpag? : number, accent? : number } = {}
const regexp = new RegExp(`([${pattern}]|[0-9a-zA-Z ]+)`, 'ug')
const temp = _.flatMap(text.match(/([^、]+)/ug), str => _.chain(str.match(regexp))
.map(_.trim)
.compact()
.value())
let state : { vunpag? : number, type? : number } = {}
return _.chain(temp)
.map(value => _.chain(value)
.split(/\s+/)
Expand All @@ -40,7 +44,7 @@ const format = (text : string) : Sound[] => {
}
const idx_accent = _.findIndex(list, o => o.tag === value)
if (idx_accent > -1) {
state.accent = idx_accent
state.type = idx_accent
return undefined
}
const result : Sound = _.chain(state)
Expand Down
22 changes: 12 additions & 10 deletions lib/formatter/partial/another.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ import * as accents from './_accents';
import { host } from '../../util';
import { Sound } from '../_type';
import { ExtractData, ExtractDataType } from '../../extracter/_type';

const reducer = (current : Sound[], ext : ExtractData) => {
switch (ext.type) {
case ExtractDataType.Text:
return _.chain(accents.format(ext.text)).concat(current).value()
case ExtractDataType.Link:
return _.map(current, c => _.assign(c, { related: `${host}/${ext.link}` }))
default:
return current
}
}
/**
* Format another accent fields different from the main 6 accents.
*
Expand All @@ -12,16 +23,7 @@ import { ExtractData, ExtractDataType } from '../../extracter/_type';
*/
const formatter = (data : ExtractData[]) : Sound[] => {
let init : Sound[] = []
const s = _.reduce(data, (current, ext) => {
switch (ext.type) {
case ExtractDataType.Text:
return _.chain(accents.format(ext.text)).concat(current).value()
case ExtractDataType.Link:
return _.map(current, c => _.assign(c, { related: `${host}/${ext.link}` }))
default:
return current
}
}, init)
const s = _.reduce(data, reducer, init)
return s
}

Expand Down
2 changes: 0 additions & 2 deletions lib/formatter/partial/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ const formatter = (data : ExtractData[]) : Sound => {
case ExtractDataType.Media:
return { media: `${host}/${ext.link}` }
case ExtractDataType.Text:
if (idx === 0)
return { type: ext.text }
return accents.format(ext.text)[0]
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion lib/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const unicode_table = [
['䯋', 'koupng/F448.png'],
['⿺皮卜', 'koupng/F545.png']]

export const host = 'http://hakka.dict.edu.tw/hakkadict'
export const host = 'https://hakka.dict.edu.tw/hakkadict'

export const image_table = _
.chain(unicode_table)
Expand Down
Loading

0 comments on commit 36d3342

Please sign in to comment.