Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
smori1983 committed Oct 8, 2023
1 parent a21589c commit 888bcc8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/components/Debug.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<template>
<pre class="json">{{ searchData }}</pre>
<div>
<p>Content of <code>@dynamic/vuepress-plugin-flexsearch/dictionary</code></p>
<pre class="json">{{ dictionaryData }}</pre>

<p>Content of <code>@dynamic/vuepress-plugin-flexsearch/data</code></p>
<pre class="json">{{ searchData }}</pre>
</div>
</template>

<script>
import data from '@dynamic/vuepress-plugin-flexsearch/data';
import dictionary from '@dynamic/vuepress-plugin-flexsearch/dictionary';
export default {
data () {
return {
searchData: data,
dictionaryData: dictionary,
};
},
};
Expand Down
14 changes: 12 additions & 2 deletions src/components/database-mixin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import data from '@dynamic/vuepress-plugin-flexsearch/data';
import dictionary from '@dynamic/vuepress-plugin-flexsearch/dictionary';

import Database from './database';
import excerpt from './excerpt';
Expand Down Expand Up @@ -29,12 +30,21 @@ export default {
* @return {{title: string, path: string, excerpt: string}[]}
*/
databaseSearch(queryRow) {
const query = queryRow.trim();
const queryTrimmed = queryRow.trim();

if (query.length === 0) {
if (queryTrimmed.length === 0) {
return [];
}

let dictQueries = [];
Object.keys(dictionary).forEach((key) => {
if (queryTrimmed.indexOf(key) >= 0) {
dictQueries = dictQueries.concat(dictionary[key]);
}
});

const query = [queryTrimmed].concat(dictQueries).join(' ');

const localePath = this.$localePath;
const queryForSearch = ngram.createForSearch(query, FLEX_SEARCH_NGRAM_SIZE);
const limit = this.$site.themeConfig.searchMaxSuggestions || FLEX_SEARCH_MAX_SUGGESTIONS;
Expand Down
27 changes: 27 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ module.exports = (options, ctx) => {
excerptTailText = ' ...',
tokenizerType = 'kuromoji.default',
ngramSize = 3,
/** @type {string[][]} */
dictionarySet = [],
} = options;

/**
Expand All @@ -36,6 +38,26 @@ module.exports = (options, ctx) => {
}
})(searchPaths);

/** @type {Map<string, Set<string>>} */
const dictMap = new Map();
dictionarySet.forEach((dictionaryItem) => {
for (let i = 0; i < dictionaryItem.length; i++) {
if (!dictMap.has(dictionaryItem[i])) {
dictMap.set(dictionaryItem[i], new Set());
}
for (let j = 0; j < dictionaryItem.length; j++) {
if (i !== j) {
dictMap.get(dictionaryItem[i]).add(dictionaryItem[j]);
}
}
}
});

const dictData = {};
dictMap.forEach((dictSet, key) => {
dictData[key] = Array.from(dictSet);
})

const flexSearchData = {};

return {
Expand Down Expand Up @@ -91,6 +113,7 @@ module.exports = (options, ctx) => {

const pageData = await tokenizer.use(tokenizerType, page, {
ngramSize: ngramSize,
dictMap: dictMap,
});

localeBasedPageData.get(localePath).set(page.key, {
Expand All @@ -116,6 +139,10 @@ module.exports = (options, ctx) => {
name: 'vuepress-plugin-flexsearch/data.js',
content: `export default ${JSON.stringify(flexSearchData, null, 2)}`,
},
{
name: 'vuepress-plugin-flexsearch/dictionary.js',
content: `export default ${JSON.stringify(dictData, null, 2)}`,
},
];
},
};
Expand Down
12 changes: 11 additions & 1 deletion src/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const tokenizers = new Map();
/**
* @typedef {Object} TokenizerUseOption
* @property {number} [ngramSize]
* @property {Map<string, Set<string>>} dictMap
*/

/**
Expand All @@ -29,13 +30,22 @@ const use = async (type, page, option) => {

const {
ngramSize = 3,
dictMap,
} = option || {};

const tokenizer = tokenizers.get(type);
const data = await tokenizer.create(page);

let dict = [];
const tokenLowerCase = data.tokens.join('').toLowerCase();
dictMap.forEach((dictSet, key) => {
if (tokenLowerCase.indexOf(key.toLowerCase()) >= 0) {
dict = dict.concat(Array.from(dictSet));
}
});

return {
dataForSearch: ngram.createForTokens(data.tokens, ngramSize),
dataForSearch: ngram.createForTokens(data.tokens.concat(dict), ngramSize),
dataForExcerpt: data.excerpt,
};
};
Expand Down

0 comments on commit 888bcc8

Please sign in to comment.