takahi_i from ATL here. To date, it has been possible to use Java to mount functions to RedPen, but it has required compiling, and so was not an environment conducive to the mounting of functions. Thus, we supported the adding of JavaScript functions from v1.3. Adding JavaScript-based functions does not require compilation.
The JavaScript functions expansion is described in the Clear Code blog. Reading the blog, you will see that there is little freedom in creating general function expansions. Firstly, anyone wanting to create functions using RedPen should make sure to read it once through.
Here, I will explain in the first half the methods (including those used infrequently) that bundle using function expansions. In the second half, I will add functions based on these methods.
Mounting Methods
The methods used (in bundling) when expanding functions using RedPen are broadly divided into two: validate and preValidate.
validate Method
There are two validate methods. Their names depend on their arguments: validateSentence and validateSection. Both methods search entries (sentences or sections) in the pursuit of errors.
RedPen converts all sentences (or sections) within a document to arguments and calls the validate method. Pursuing errors uses the addError method. To bundle a function, it is enough just to mount either of the validate methods.
validateSentence Method
validateSentence holds sentences as arguments.
validateSection Method
validateSection takes sections as arguments. Used for checks on a larger scale than sentences. For example, a function that detects sections that are remarkably similar (DuplicatedSection) is bundled using the method that validates by section as its unit.
preValidate Method
preValidate is a method that implements pre-processing before the validate method. Specifically, before the validate method is called, the preValidate method is applied to all documents. There are two preValidate methods in the same way as there are two validate methods: preValidateSentence and preValidateSection.
- preValidateSentence (this preValidate method takes the sentence as the argumen
- preValidateSection (this preValidate method takes the section as the argument)
Not many functions are required by the current preValidate methods. The handful of functions that use preValidate with the functions supplied by RedPen include JapaneseStyle (in Java, but…) The JapaneseStyle function searches to see whether the “desu/-masu” forms and “de aru” forms have been mixed. Bundling JapaneseStyle uses the preValidate method, and counts the number of sentences in which the “desu/-masu” forms and “de aru” forms appear. An error is output if the validate method finds sentences that use the less frequent number of appearances.
In the next section, we will try creating functions using validateSentence and preValidateSentence.
NotTooKanji (Too Many Chinese Characters) Function
Co-authors who had written documents in the past had warned that “you use too many Chinese characters in your sentences“. The reason being that too many Chinese characters make reading difficult. “Thou shalt not use too many Chinese characters” is a comparatively general rule. Actually Googling “do not use too many Chinese characters” returns countless hits.
NotTooKanji, which was developed on this occasion, outputs an error if the percentage of Chinese characters used in a document is above a threshold value. The preValidateSentence method counts the number of Chinese characters used and all the characters in a sentence. The validateSentence method processes an error output if the percentage of Chinese characters is over 50%.
The addError method is used to add errors. (Error messages in the arguments, and sentences that contain errors.) Also, NotTooKanji used a flag called “flushed” so as not to output an error once only.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
var THRESHOLD = 0.5 var kanji_count = 0 var moji_count = 0 var flushed = false function preValidateSentence(sentence) { var content = sentence.getContent(); for (var i = 0; i <= content.length - 1; i++) { if(/^[u4e00-u9faf]+$/.test(content[i])) { kanji_count++ } moji_count++ } } function validateSentence(sentence) { if ((parseFloat(kanji_count) / moji_count) > THRESHOLD && flushed == false) { addError("文書内に漢字多すぎ。。。><", sentence); flushed = true } } |
Let’s Try!
Add a JavaScript function to the settings as described below.
1 2 3 4 5 6 7 |
<redpen-conf lang="ja"> <validators> <validator name="JavaScript" /> </validators> </redpen-conf> |
Next, save the JavaScript file described above under js directly under the RedPen home directory using the name NotTooKanji.js.
Save the following sentence as an entered document using the file name “too-much-kanji.txt”. (Apologies for the several artificial examples.)
1 2 |
貴方の文書で漢字が多過ぎる点、気を付け給え。 |
So, let’s try!
1 2 3 4 |
$bin/redpen -c conf/redpen-conf-ja.xml too-much-kanji.txt ... too-much-kanji.txt:1: ValidationError[JavaScript], [NotTooKanji.js] 文書内に漢字多すぎ。。。>< at line: 貴方の文書で漢字が多過ぎる点、気を付け給え。 |
An error is output as expected.
Conclusion
This article explained the methods used when creating RedPen function expansions using JavaScript. Next, we tried actually using the methods to add functions.