Skip to content

Commit b179e66

Browse files
committed
initial commit
0 parents  commit b179e66

File tree

9 files changed

+271
-0
lines changed

9 files changed

+271
-0
lines changed

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["es2015"],
3+
"plugins": ["add-module-exports"]
4+
}

.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# from https://github.com/github/gitignore/blob/master/Node.gitignore
2+
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
13+
# Directory for instrumented libs generated by jscoverage/JSCover
14+
lib-cov
15+
16+
# Coverage directory used by tools like istanbul
17+
coverage
18+
19+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
20+
.grunt
21+
22+
# node-waf configuration
23+
.lock-wscript
24+
25+
# Compiled binary addons (http://nodejs.org/api/addons.html)
26+
build/Release
27+
28+
# Dependency directories
29+
node_modules
30+
jspm_packages
31+
32+
# Optional npm cache directory
33+
.npm
34+
35+
# Optional REPL history
36+
.node_repl_history
37+
38+
# Others
39+
/lib
40+

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src
2+
test
3+
node_modules

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2016 takahashim
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# textlint-rule-no-doubled-conjunction
2+
3+
This module is a textlint plugin to check duplicated same conjunctions.
4+
5+
同じ接続詞が連続して出現していないかどうかをチェックするための[textlint](https://github.com/textlint/textlint "textlint")ルールです。
6+
7+
ex)
8+
9+
> かな漢字変換により漢字が多用される傾向がある。しかし漢字の多用が読みにくさをもたらす側面は否定できない。しかし、平仮名が多い文は間延びした印象を与える恐れもある。
10+
11+
In this example, "**しかし**" are used sequentially.
12+
13+
## Installation
14+
15+
npm install textlint-rule-no-doubled-conjunction
16+
17+
### Require
18+
19+
- textlint 5.0 >=
20+
21+
### Dependencies
22+
23+
- [azu/kuromojin](https://github.com/azu/kuromojin): a wrapper of [kuromoji.js](https://github.com/takuyaa/kuromoji.js "kuromoji.js")
24+
- [azu/sentence-splitter](https://github.com/azu/sentence-splitter)
25+
26+
## Usage
27+
28+
textlint --rule no-doubled-conjunction sample.md
29+
30+
### Options
31+
32+
There's no options for this plugin.
33+
34+
## Tests
35+
36+
npm test
37+
38+
## Reference
39+
40+
- [textlint](https://github.com/textlint/textlint)
41+
- [textlint-rule-no-doubled-joshi](https://github.com/azu/textlint-rule-no-doubled-joshi): this plugin is based on it
42+
43+
## License
44+
45+
MIT

package.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"name": "textlint-rule-no-doubled-conjunction",
3+
"version": "1.0.0",
4+
"description": "textlint rule plugin to check duplicated conjunctions.",
5+
"main": "lib/no-doubled-conjunction.js",
6+
"directories": {
7+
"example": "example",
8+
"test": "test"
9+
},
10+
"dependencies": {
11+
"kuromojin": "^1.1.0",
12+
"sentence-splitter": "^1.2.0",
13+
"textlint-rule-helper": "^1.1.5",
14+
"textlint-util-to-string": "^1.2.0"
15+
},
16+
"devDependencies": {
17+
"babel-cli": "^6.5.1",
18+
"babel-plugin-add-module-exports": "^0.1.2",
19+
"babel-preset-es2015": "^6.5.0",
20+
"espower-babel": "^4.0.1",
21+
"mocha": "^2.4.5",
22+
"power-assert": "^1.2.0",
23+
"textlint": "^5.2.1",
24+
"textlint-tester": "^0.4.1"
25+
},
26+
"scripts": {
27+
"build": "babel src --out-dir lib --source-maps",
28+
"watch": "babel src --out-dir lib --watch --source-maps",
29+
"prepublish": "npm run --if-present build",
30+
"test": "mocha",
31+
"textlint": "textlint"
32+
},
33+
"keywords": [
34+
"textlint",
35+
"rule"
36+
],
37+
"author": "takahashim <takahashimm@gmail.com>",
38+
"license": "MIT",
39+
"repository": {
40+
"type": "git",
41+
"url": "https://github.com/takahashim/textlint-rule-no-doubled-conjunction.git"
42+
},
43+
"bugs": {
44+
"url": "https://github.com/takahashim/textlint-rule-no-doubled-conjunction/issues"
45+
},
46+
"homepage": "https://github.com/takahashim/textlint-rule-no-doubled-conjunction"
47+
}

src/no-doubled-conjunction.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// LICENSE : MIT
2+
"use strict";
3+
import {RuleHelper} from "textlint-rule-helper";
4+
import {getTokenizer} from "kuromojin";
5+
import splitSentences, {Syntax as SentenceSyntax} from "sentence-splitter";
6+
import StringSource from "textlint-util-to-string";
7+
8+
/*
9+
1. Paragraph Node -> text
10+
2. text -> sentences
11+
3. tokenize sentence
12+
4. report error if found word that match the rule.
13+
14+
TODO: need abstraction
15+
*/
16+
export default function (context, options = {}) {
17+
const helper = new RuleHelper(context);
18+
const {Syntax, report, getSource, RuleError} = context;
19+
return {
20+
[Syntax.Paragraph](node){
21+
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
22+
return;
23+
}
24+
const source = new StringSource(node);
25+
const text = source.toString();
26+
const isSentenceNode = (node) => node.type === SentenceSyntax.Sentence;
27+
let sentences = splitSentences(text, {
28+
charRegExp: /[\?\!]/
29+
}).filter(isSentenceNode);
30+
return getTokenizer().then(tokenizer => {
31+
const selectConjenction = (sentence) => {
32+
let tokens = tokenizer.tokenizeForSentence(sentence.raw);
33+
let conjunctionTokens = tokens.filter((token) => token.pos === "接続詞");
34+
return [sentence, conjunctionTokens];
35+
};
36+
let prev_token = null;
37+
sentences.map(selectConjenction).reduce((prev, current) => {
38+
let token = prev_token;
39+
let [sentence, current_tokens] = current;
40+
let [prev_sentence, prev_tokens] = prev;
41+
if (prev_tokens && prev_tokens.length > 0) {
42+
token = prev_tokens[0];
43+
}
44+
if (current_tokens.length > 0) {
45+
if (token && current_tokens[0].surface_form === token.surface_form) {
46+
let originalPosition = source.originalPositionFor({
47+
line: sentence.loc.start.line,
48+
column: sentence.loc.start.column + (current_tokens[0].word_position - 1)
49+
});
50+
// padding position
51+
var padding = {
52+
line: originalPosition.line - 1,
53+
// matchLastToken.word_position start with 1
54+
// this is padding column start with 0 (== -1)
55+
column: originalPosition.column
56+
};
57+
report(node, new RuleError(`同じ接続詞が連続して使われています。`, padding));
58+
}
59+
}
60+
prev_token = token;
61+
return current;
62+
});
63+
});
64+
}
65+
}
66+
};

test/mocha.opts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--compilers js:espower-babel/guess

test/no-doubled-conjunction.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import assert from "power-assert";
2+
import rule from "../src/no-doubled-conjunction";
3+
import TextLintTester from "textlint-tester";
4+
var tester = new TextLintTester();
5+
tester.run("no-doubled-conjunction", rule, {
6+
valid: [
7+
"朝起きた。そして、夜に寝た。",
8+
"そして朝起きた。けれど昼は仕事をした。そして夜に寝た。"
9+
],
10+
invalid: [
11+
{
12+
text: "朝起きた。そして昼は仕事をした。そして夜に寝た",
13+
errors: [
14+
{
15+
message: `同じ接続詞が連続して使われています。`,
16+
// last match
17+
line: 1,
18+
column: 17
19+
}
20+
]
21+
},
22+
{
23+
text: "そして朝起きた。昼は仕事をした。そして夜に寝た",
24+
errors: [
25+
{
26+
message: `同じ接続詞が連続して使われています。`,
27+
// last match
28+
line: 1,
29+
column: 17
30+
}
31+
]
32+
},
33+
{
34+
text: "かな漢字変換により漢字が多用される傾向がある。しかし漢字の多用が読みにくさをもたらす側面は否定できない。しかし、平仮名が多い文は間延びした印象を与える恐れもある。",
35+
errors: [
36+
{
37+
message: `同じ接続詞が連続して使われています。`,
38+
// last match
39+
line: 1,
40+
column: 53
41+
}
42+
]
43+
}
44+
45+
]
46+
});

0 commit comments

Comments
 (0)