Skip to content

fix(deps): upgrade sentence-splitter@3 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: test
on: [push, pull_request]
env:
CI: true
jobs:
test:
name: "Test on Node.js ${{ matrix.node-version }}"
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10, 12, 14]
steps:
- name: checkout
uses: actions/checkout@v2
- name: setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install
run: yarn install
- name: Test
run: yarn test
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# textlint-rule-max-ten [![Build Status](https://travis-ci.org/textlint-ja/textlint-rule-max-ten.svg?branch=master)](https://travis-ci.org/textlint-ja/textlint-rule-max-ten) [![Gitter](https://badges.gitter.im/textlint-ja/textlint-ja.svg)](https://gitter.im/textlint-ja/textlint-ja)
# textlint-rule-max-ten [![Actions Status: test](https://github.com/textlint-ja/textlint-rule-max-ten/workflows/test/badge.svg)](https://github.com/textlint-ja/textlint-rule-max-ten/actions?query=workflow%3A"test")

[textlint](https://github.com/textlint/textlint "textlint") rule is that limit maximum ten(、) count of sentence.

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@
"textlint-scripts": "^3.0.0"
},
"dependencies": {
"kuromojin": "^2.0.0",
"sentence-splitter": "^2.0.0",
"kuromojin": "^2.1.1",
"sentence-splitter": "^3.2.0",
"structured-source": "^3.0.2",
"textlint-rule-helper": "^2.0.0"
"textlint-rule-helper": "^2.0.0",
"textlint-util-to-string": "^3.1.1"
}
}
61 changes: 23 additions & 38 deletions src/max-ten.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,42 @@
// LICENSE : MIT
"use strict";
import {RuleHelper} from "textlint-rule-helper"
import {getTokenizer} from "kuromojin";
import {split as splitSentences} from "sentence-splitter";
import Source from "structured-source";
import { RuleHelper } from "textlint-rule-helper"
import { getTokenizer } from "kuromojin";
import { splitAST, Syntax as SentenceSyntax } from "sentence-splitter";
import { StringSource } from "textlint-util-to-string";

const defaultOptions = {
max: 3, // 1文に利用できる最大の、の数
strict: false // 例外ルールを適応するかどうか
};

function isSandwichedMeishi({
before,
token,
after
}) {
before,
token,
after
}) {
if (before === undefined || after === undefined || token === undefined) {
return false;
}
return before.pos === "名詞" && after.pos === "名詞";
}
/**
* add two positions.
* note: line starts with 1, column starts with 0.
* @param {Position} base
* @param {Position} relative
* @return {Position}
*/
function addPositions(base, relative) {
return {
line: base.line + relative.line - 1, // line 1 + line 1 should be line 1
column: relative.line == 1 ? base.column + relative.column // when the same line
: relative.column // when another line
};
}

/**
* @param {RuleContext} context
* @param {object} [options]
*/
module.exports = function(context, options = {}) {
module.exports = function (context, options = {}) {
const maxLen = options.max || defaultOptions.max;
const isStrict = options.strict || defaultOptions.strict;
let helper = new RuleHelper(context);
let {Syntax, RuleError, report, getSource} = context;
const helper = new RuleHelper(context);
const { Syntax, RuleError, report, getSource } = context;
return {
[Syntax.Paragraph](node){
[Syntax.Paragraph](node) {
if (helper.isChildNode(node, [Syntax.BlockQuote])) {
return;
}
let sentences = splitSentences(getSource(node), {
charRegExp: /[。\?\!?!]/,
newLineCharacters: "\n\n"
});
const resultNode = splitAST(node);
const sentences = resultNode.children.filter(childNode => childNode.type === SentenceSyntax.Sentence);
/*
<p>
<str><code><img><str>
Expand All @@ -65,10 +51,10 @@ module.exports = function(context, options = {}) {
*/
return getTokenizer().then(tokenizer => {
sentences.forEach(sentence => {
let text = sentence.value;
let source = new Source(text);
const source = new StringSource(sentence);
const text = source.toString();
const tokens = tokenizer.tokenizeForSentence(text);
let currentTenCount = 0;
let tokens = tokenizer.tokenizeForSentence(text);
let lastToken = null;
tokens.forEach((token, index) => {
let surface = token.surface_form;
Expand All @@ -92,11 +78,10 @@ module.exports = function(context, options = {}) {
}
// report
if (currentTenCount >= maxLen) {
let positionInSentence = source.indexToPosition(lastToken.word_position - 1);
let positionInNode = addPositions(sentence.loc.start, positionInSentence);
let ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
line: positionInNode.line - 1,
column: positionInNode.column
const positionInSentence = source.originalIndexFromIndex(lastToken.word_position - 1);
const index = sentence.range[0] + positionInSentence;
const ruleError = new context.RuleError(`一つの文で"、"を${maxLen}つ以上使用しています`, {
index
});
report(node, ruleError);
currentTenCount = 0;
Expand Down
Loading