Skip to content

Commit

Permalink
Use @types/nlcst
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 27, 2021
1 parent ce2ae68 commit a86b65b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist').Node} Node
* @typedef {import('nlcst').Root} Root
* @typedef {import('nlcst').Sentence} Sentence
* @typedef {import('nlcst').Word} Word
* @typedef {import('nlcst').SentenceContent} SentenceContent
*
* @typedef Options
* Configuration.
Expand All @@ -25,13 +27,13 @@
* @property {string} stem
*
* @typedef PhraseMatch
* @property {Node[]} nodes
* @property {Parent} parent
* @property {SentenceContent[]} nodes
* @property {Sentence} parent
*
* @typedef KeywordMatch
* @property {Node} node
* @property {Word} node
* @property {number} index
* @property {Parent} parent
* @property {Sentence} parent
*/

import {stemmer} from 'stemmer'
Expand All @@ -43,7 +45,7 @@ const own = {}.hasOwnProperty
/**
* Plugin to extract keywords and key-phrases.
*
* @type {import('unified').Plugin<[Options?]>}
* @type {import('unified').Plugin<[Options?]|[], Root>}
*/
export default function retextKeywords(options = {}) {
const maximum = options.maximum || 5
Expand All @@ -58,17 +60,17 @@ export default function retextKeywords(options = {}) {
/**
* Get following or preceding important words or white space.
*
* @param {Parent} parent
* @param {Sentence} parent
* @param {number} index
* @param {number} offset
*/
function findPhraseInDirection(parent, index, offset) {
const children = parent.children
/** @type {Node[]} */
/** @type {SentenceContent[]} */
const nodes = []
/** @type {string[]} */
const stems = []
/** @type {Node[]} */
/** @type {SentenceContent[]} */
const queue = []

while (children[(index += offset)]) {
Expand Down Expand Up @@ -97,7 +99,7 @@ function findPhraseInDirection(parent, index, offset) {
function getKeyphrases(results, maximum) {
/** @type {Record<string, Keyphrase>} */
const stemmedPhrases = {}
/** @type {Node[]} */
/** @type {Word[]} */
const initialWords = []
/** @type {string} */
let keyword
Expand All @@ -112,7 +114,7 @@ function getKeyphrases(results, maximum) {
while (++index < matches.length) {
const phrase = findPhrase(matches[index])
const stemmedPhrase = stemmedPhrases[phrase.value]
const first = phrase.nodes[0]
const first = /** @type {Word} */ (phrase.nodes[0])
const match = {nodes: phrase.nodes, parent: matches[index].parent}

// If we've detected the same stemmed phrase somewhere.
Expand Down Expand Up @@ -260,13 +262,14 @@ function merge(previous, current, next) {
/**
* Get most important words in `node`.
*
* @param {Node} node
* @param {Root} node
*/
function getImportantWords(node) {
/** @type {Record<string, Keyword>} */
const words = {}

visit(node, 'WordNode', (word, index, parent) => {
visit(node, 'WordNode', (word, index, parent_) => {
const parent = /** @type {Sentence} */ (parent_)
if (parent && index !== null && important(word)) {
const stem = stemNode(word)
const match = {node: word, index, parent}
Expand Down Expand Up @@ -309,7 +312,7 @@ function cloneMatches(words) {
/**
* Check if `node` is important.
*
* @param {Node} node
* @param {SentenceContent} node
* @returns {boolean}
*/
function important(node) {
Expand All @@ -336,7 +339,7 @@ function uppercase(value) {
/**
* Get the stem of a node.
*
* @param {Node} node
* @param {SentenceContent} node
* @returns {string}
*/
function stemNode(node) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"index.js"
],
"dependencies": {
"@types/nlcst": "^1.0.0",
"nlcst-to-string": "^3.0.0",
"stemmer": "^2.0.0",
"unified": "^10.0.0",
Expand Down
1 change: 0 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import test from 'tape'
import {retext} from 'retext'
// @ts-expect-error: To type.
import retextPos from 'retext-pos'
import retextKeywords from './index.js'

Expand Down

0 comments on commit a86b65b

Please sign in to comment.