Skip to content

Commit

Permalink
#10 Add language tool for checking the grammar (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikialeksey authored Nov 25, 2018
1 parent 0bdfa8b commit 74b3dc3
Show file tree
Hide file tree
Showing 48 changed files with 371 additions and 192 deletions.
12 changes: 9 additions & 3 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ targetCompatibility = 1.8

dependencies {
implementation 'com.atlascopco:hunspell-bridj:1.0.4'
implementation 'org.languagetool:language-all:4.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
}

jacocoTestReport {
reports {
xml.enabled true
html.enabled false
xml.enabled = true
html.enabled = false
}
}

Expand All @@ -33,7 +34,7 @@ ext {
siteUrl = 'https://github.com/nikialeksey/arspell'
gitUrl = 'https://github.com/nikialeksey/arspell.git'

libraryVersion = '1.0.0'
libraryVersion = '2.0.0'

developerId = 'nikialeksey'
developerName = 'Alexey Nikitin'
Expand Down Expand Up @@ -100,6 +101,11 @@ if (project.rootProject.file('local.properties').exists()) {
dependencyNode.appendNode('groupId', 'com.atlascopco')
dependencyNode.appendNode('artifactId', 'hunspell-bridj')
dependencyNode.appendNode('version', '1.0.4')

dependencyNode = dependenciesNode.appendNode('dependency')
dependencyNode.appendNode('groupId', 'org.languagetool')
dependencyNode.appendNode('artifactId', 'language-all')
dependencyNode.appendNode('version', '4.0')
}
}
}
Expand Down
31 changes: 0 additions & 31 deletions lib/src/main/java/com/nikialeksey/arspell/AndroidStrings.kt

This file was deleted.

14 changes: 0 additions & 14 deletions lib/src/main/java/com/nikialeksey/arspell/HunspellCheck.kt

This file was deleted.

10 changes: 0 additions & 10 deletions lib/src/main/java/com/nikialeksey/arspell/IgnoreKeysStrings.kt

This file was deleted.

10 changes: 0 additions & 10 deletions lib/src/main/java/com/nikialeksey/arspell/IgnoreKeysWords.kt

This file was deleted.

10 changes: 0 additions & 10 deletions lib/src/main/java/com/nikialeksey/arspell/IgnoreValuesStrings.kt

This file was deleted.

6 changes: 0 additions & 6 deletions lib/src/main/java/com/nikialeksey/arspell/Sentence.kt

This file was deleted.

4 changes: 0 additions & 4 deletions lib/src/main/java/com/nikialeksey/arspell/Sentences.kt

This file was deleted.

20 changes: 0 additions & 20 deletions lib/src/main/java/com/nikialeksey/arspell/SimpleCheck.kt

This file was deleted.

26 changes: 0 additions & 26 deletions lib/src/main/java/com/nikialeksey/arspell/SimpleSentence.kt

This file was deleted.

7 changes: 0 additions & 7 deletions lib/src/main/java/com/nikialeksey/arspell/SimpleSentences.kt

This file was deleted.

10 changes: 0 additions & 10 deletions lib/src/main/java/com/nikialeksey/arspell/SimpleStrings.kt

This file was deleted.

5 changes: 0 additions & 5 deletions lib/src/main/java/com/nikialeksey/arspell/SpellCheck.kt

This file was deleted.

5 changes: 0 additions & 5 deletions lib/src/main/java/com/nikialeksey/arspell/Strings.kt

This file was deleted.

3 changes: 0 additions & 3 deletions lib/src/main/java/com/nikialeksey/arspell/Words.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.nikialeksey.arspell.checks

import com.nikialeksey.arspell.Error
import com.nikialeksey.arspell.dictionary.DictionaryError
import com.nikialeksey.arspell.dictionary.Dictionary
import com.nikialeksey.arspell.strings.Strings

class DictionaryCheck(
private val dictionary: Dictionary,
private val strings: Strings
) : SpellCheck {

override fun check(): List<Error> {
val result = mutableListOf<Error>()
strings.asList()[0].sentences().asList()[0].words()
for (string in strings.asList()) {
for (sentence in string.sentences().asList()) {
for (word in sentence.words()) {
val asString = word.asString()
if (!dictionary.isCorrect(asString)) {
result.add(DictionaryError(word.key(), asString, dictionary))
}
}
}
}
return result
}
}
23 changes: 23 additions & 0 deletions lib/src/main/java/com/nikialeksey/arspell/checks/HunspellCheck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nikialeksey.arspell.checks

import com.atlascopco.hunspell.Hunspell
import com.nikialeksey.arspell.Error
import com.nikialeksey.arspell.dictionary.HunspellDictionary
import com.nikialeksey.arspell.strings.Strings

class HunspellCheck(
private val origin: SpellCheck
) : SpellCheck {

constructor(hunspell: Hunspell, strings: Strings) : this(
DictionaryCheck(
HunspellDictionary(
hunspell
), strings
)
)

override fun check(): List<Error> {
return origin.check()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.nikialeksey.arspell.checks

import com.nikialeksey.arspell.Error
import com.nikialeksey.arspell.strings.Strings
import org.languagetool.JLanguageTool

class LanguageToolCheck(
private val languageTool: JLanguageTool,
private val strings: Strings
) : SpellCheck {
override fun check(): List<Error> {
val errors = mutableListOf<Error>()

for (string in strings.asList()) {
val rules = languageTool.check(string.asString())
for (rule in rules) {
errors.add(LanguageToolError(string.key(), rule))
}
}

return errors
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.nikialeksey.arspell.checks

import com.nikialeksey.arspell.Error
import org.languagetool.rules.RuleMatch

class LanguageToolError(
private val key: String,
private val rule: RuleMatch
) : Error {
override fun key(): String {
return key
}

override fun word(): String {
return rule.sentence.text.substring(rule.fromPos, rule.toPos)
}

override fun suggests(): List<String> {
return listOf(rule.message) + rule.suggestedReplacements
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nikialeksey.arspell.checks

import com.nikialeksey.arspell.Error

interface SpellCheck {
fun check(): List<Error>
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nikialeksey.arspell
package com.nikialeksey.arspell.dictionary

interface Dictionary {
fun isCorrect(word: String): Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.nikialeksey.arspell
package com.nikialeksey.arspell.dictionary

class SimpleError(
import com.nikialeksey.arspell.Error

class DictionaryError(
private val key: String,
private val word: String,
private val dictionary: Dictionary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nikialeksey.arspell
package com.nikialeksey.arspell.dictionary

class DictionaryGroup(
private val dictionaries: List<Dictionary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.nikialeksey.arspell
package com.nikialeksey.arspell.dictionary

import com.atlascopco.hunspell.Hunspell

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.nikialeksey.arspell.sentences

import com.nikialeksey.arspell.words.IgnoreValuesWords
import com.nikialeksey.arspell.words.Words

class IgnoreWordsSentence(
private val origin: Sentence,
private val words: Collection<String>
) : Sentence {
override fun key(): String {
return origin.key()
}

override fun words(): Words {
return IgnoreValuesWords(origin.words(), words)
}

}
Loading

1 comment on commit 74b3dc3

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 74b3dc3 Nov 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 10-5781f6ef discovered in readme.md and submitted as #17. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.