Skip to content
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

"、"を間隔値としてカウントするように #3

Merged
merged 4 commits into from
Feb 18, 2016
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
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ Dependencies

## Usage

Via `.textlintrc`(推奨)

```js
{
"rules": {
"no-doubled-joshi": {
"min_interval" : 1,
"strict": false
}
}
}
```

Via CLI

```
textlint --rule no-doubled-joshi README.md
```


### Options

`.textlintrc` options.
Expand All @@ -39,18 +59,18 @@ Dependencies
}
```

- `min_interval` : 助詞の最低間隔値
- `min_interval`(default: 1) : 助詞の最低間隔値
- 指定した間隔値以下で同じ助詞が出現した場合エラーが出力されます。
- `strict`: (default: false) 例外もエラーとするかどうか
- `strict`(default: false) :例外もエラーとするかどうか
- 下記参照。例外としているものもエラーとするかどうか

> 私**は**彼**は**好き

この場合の、**は**と**は**の間隔値は1

> 既存**の**文章**の**修正
> 既存**の**文と絵**の**修正

この場合の、**の**と**の**の間隔値は2
この場合の、**の**と**の**の間隔値は2(間に**の**がある)

## 判定処理

Expand All @@ -72,20 +92,25 @@ Dependencies

設定が `{ strict: true }` ならばエラーとするが、デフォルトでは`{ strict: false }` となっている。

#### 助詞:連体化 "の"
### 助詞:連体化 "の"

"の" の重なりは例外として許可する。

- [第8回:読みやすさへの工夫 3(てにおは助詞) - たくみの匠](http://www.asca-co.com/takumi/2010/07/3.html "第8回:読みやすさへの工夫 3(てにおは助詞) - たくみの匠")
- [作文入門](http://www.slideshare.net/takahi-i/ss-13429892 "作文入門")
- "の" の消し方について

#### 助詞:格助詞 "を"
### 助詞:格助詞 "を"

> オブジェクトを返す関数を公開する

"を" の重なりは例外として許可する。

### "、"での区切り

> 右がiPhone、左がAndroidです。

"、"を間隔値+1としてカウントするため、上記の文章はデフォルトでは許容される。

## Tests

Expand Down
38 changes: 25 additions & 13 deletions src/no-doubled-joshi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ import {RuleHelper} from "textlint-rule-helper";
import {getTokenizer} from "kuromojin";
import splitSentences, {Syntax as SentenceSyntax} from "sentence-splitter";
import StringSource from "textlint-util-to-string";
// 助詞どうか
const is助詞Token = token => {
return token.pos === "助詞";
};
const is読点Token = token => {
return token.surface_form === "、" && token.pos === "名詞";
};
/**
* create a object that
* map ={
* // these token.surface_form === "Hoge"
* "Hoge" [token, token]
* Create token map object
* {
* "で": [token, token],
* "の": [token, token]
* }
* @param tokens
* @returns {*}
*/
function createSurfaceKeyMap(tokens) {
return tokens.reduce((keyMap, token) => {
// 助詞のみを対象とする
return tokens.filter(is助詞Token).reduce((keyMap, token) => {
// "は" : [token]
if (!keyMap[token.surface_form]) {
keyMap[token.surface_form] = [];
Expand Down Expand Up @@ -43,7 +51,6 @@ const defaultOptions = {
strict: false
};


/*
1. Paragraph Node -> text
2. text -> sentences
Expand Down Expand Up @@ -74,11 +81,16 @@ export default function (context, options = {}) {
return getTokenizer().then(tokenizer => {
const checkSentence = (sentence) => {
let tokens = tokenizer.tokenizeForSentence(sentence.raw);
const isJoshiToken = token => {
return token.pos === "助詞";
};
let joshiTokens = tokens.filter(isJoshiToken);
let joshiTokenSurfaceKeyMap = createSurfaceKeyMap(joshiTokens);
let countableTokens = tokens.filter(token => {
if (isStrict) {
return is助詞Token(token);
}
// デフォルトでは、"、"を間隔値の距離としてカウントする
// "、" があると助詞同士の距離が開くようにすることで、並列的な"、"の使い方を許容する目的
// https://github.com/azu/textlint-rule-no-doubled-joshi/issues/2
return is助詞Token(token) || is読点Token(token);
});
let joshiTokenSurfaceKeyMap = createSurfaceKeyMap(countableTokens);
/*
# Data Structure

Expand All @@ -102,8 +114,8 @@ export default function (context, options = {}) {
// if found differenceIndex less than
// tokes are sorted ascending order
tokens.reduce((prev, current) => {
let startPosition = joshiTokens.indexOf(prev);
let otherPosition = joshiTokens.indexOf(current);
let startPosition = countableTokens.indexOf(prev);
let otherPosition = countableTokens.indexOf(current);
// if difference
let differenceIndex = otherPosition - startPosition;
if (differenceIndex <= minInterval) {
Expand Down
9 changes: 8 additions & 1 deletion test/no-doubled-joshi-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ tester.run("no-double-joshi", rule, {
"私は彼が好きだ",
"既存のコードの利用", // "の" の例外
"オブジェクトを返す関数を公開した", // "を" の例外
"私は彼の鼻は好きだ"
"私は彼の鼻は好きだ",
// 、 tokenを距離 + 1 として考える
"右がiPhone、左がAndroidです。",
"ナイフで切断した後、ハンマーで破砕した。"
],
invalid: [
{
Expand Down Expand Up @@ -70,7 +73,11 @@ tester.run("no-double-joshi", rule, {
]
},
{
// 、 で間隔値が+1されるが、strictでは+されない
text: "彼女は困り切った表情で、小声で尋ねた。",
options: {
strict: true
},
errors: [
{
message: `一文に二回以上利用されている助詞 "で" がみつかりました。`,
Expand Down