Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
smori1983 committed Sep 24, 2023
1 parent b400875 commit cc0f08b
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/SearchBoxFlexSearch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<script>
import { Document } from 'flexsearch'
import data from '@dynamic/vuepress-plugin-flexsearch/data'
import dictionary from '@dynamic/vuepress-plugin-flexsearch/dictionary'
import Database from './database';
import excerpt from './excerpt'
import ngram from '../tokenizer/ngram'
Expand Down Expand Up @@ -97,13 +98,22 @@ export default {
* @return {{title: string, path: string, excerpt: string}[]}
*/
suggestions () {
const query = this.query.trim();
const queryTrimmed = this.query.trim();
if (query.length === 0) {
if (queryTrimmed.length === 0) {
return [];
}
const localePath = this.$localePath;
let dictQueries = [];
Object.keys(dictionary).forEach((key) => {
if (queryTrimmed.indexOf(key) >= 0) {
dictQueries = dictQueries.concat(dictionary[key]);
}
});
const query = [queryTrimmed].concat(dictQueries).join(' ');
const queryForSearch = ngram.createForSearch(query, FLEX_SEARCH_NGRAM_SIZE);
const max = 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 @@ -25,6 +25,8 @@ module.exports = (options, ctx) => {
excerptTailText = ' ...',
tokenizerType = 'kuromoji.default',
ngramSize = 3,
/** @type {string[][]} */
dictionarySet = [],
} = options;

/**
Expand All @@ -38,6 +40,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);
})

return {
define: {
FLEX_SEARCH_HOTKEYS: searchHotKeys,
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 token = data.tokens.join('');
dictMap.forEach((dictSet, key) => {
if (token.indexOf(key) >= 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 cc0f08b

Please sign in to comment.