Skip to content

Commit

Permalink
Add search command
Browse files Browse the repository at this point in the history
  • Loading branch information
nehemiaharchives committed Sep 30, 2022
1 parent 4cf6cf7 commit ef10d70
Show file tree
Hide file tree
Showing 6,005 changed files with 1,545 additions and 45 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
10 changes: 10 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions .idea/markdown.xml

This file was deleted.

14 changes: 10 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
lucene_version = '9.3.0'
kotlin_version = '1.7.10'
lucene_version = '9.3.0'
tinlylog_version = '2.5.0'
}
}
Expand All @@ -15,21 +15,27 @@ plugins {
}

group = 'org.gnit.bible'
version = '1.1'
version = '1.2'

repositories {
mavenCentral()
}

dependencies {
/*implementation "org.apache.lucene:lucene-queryparser:$lucene_version"
implementation "org.apache.lucene:lucene-analyzers-common:$lucene_version"*/
implementation "com.github.ajalt.clikt:clikt:3.5.0"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0"
implementation 'org.slf4j:slf4j-api:1.7.36'
implementation "org.tinylog:slf4j-tinylog:$tinlylog_version"
implementation "org.tinylog:tinylog-impl:$tinlylog_version"

//search
implementation 'com.google.jimfs:jimfs:1.2'
implementation "org.apache.lucene:lucene-queryparser:$lucene_version"
implementation "org.apache.lucene:lucene-analysis-common:$lucene_version"
implementation "org.apache.lucene:lucene-analysis-smartcn:$lucene_version"
implementation "org.apache.lucene:lucene-analysis-kuromoji:$lucene_version"
implementation "org.apache.lucene:lucene-analysis-nori:$lucene_version"

//test
implementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
requires com.github.ajalt.clikt;
requires kotlin.stdlib;
requires kotlin.test;
requires kotlinx.serialization.core;
requires kotlinx.serialization.json;
requires org.slf4j;
requires org.tinylog.api.slf4j;
requires org.tinylog.api;
requires org.tinylog.impl;
requires org.apache.lucene.core;
requires org.apache.lucene.queryparser;
requires org.apache.lucene.analysis.smartcn;
requires org.apache.lucene.analysis.nori;
requires org.apache.lucene.analysis.kuromoji;
requires jimfs;
requires kotlin.stdlib.jdk7;
requires jdk.management;
exports org.gnit.bible;
}
41 changes: 41 additions & 0 deletions src/main/kotlin/org/gnit/bible/Chapters.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.gnit.bible

/**
* Created by Joel on 10/22/2014.
*/
object Chapters {
fun maxChapter(book: Int): Int = when (book) {
19 -> 150
23 -> 66
24 -> 52
1 -> 50
26 -> 48
18 -> 42
2 -> 40
4, 14 -> 36
5 -> 34
9, 20 -> 31
13 -> 29
40, 44 -> 28
3 -> 27
12 -> 25
6, 10, 42 -> 24
11, 66 -> 22
7, 43 -> 21
41, 45, 46 -> 16
28, 38 -> 14
16, 47, 58 -> 13
21, 27 -> 12
15, 17 -> 10
30 -> 9
22 -> 8
33 -> 7
48, 49, 54 -> 6
25, 52, 59, 60, 62 -> 5
8, 32, 39, 50, 51, 55 -> 4
29, 34, 35, 36, 53, 56, 61 -> 3
37 -> 2
31, 57, 63, 64, 65 -> 1
else -> 50
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/org/gnit/bible/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import org.slf4j.LoggerFactory

@Serializable
data class Config(
val translation: String = DEFAULT_TRANSLATION
val translation: Translation = DEFAULT_TRANSLATION,
val searchResult: Int = 100
)

const val dataDirName = ".bbl"
Expand Down
113 changes: 93 additions & 20 deletions src/main/kotlin/org/gnit/bible/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ import com.github.ajalt.clikt.core.subcommands
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.default
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.options.option
import org.slf4j.LoggerFactory

const val DEFAULT_TRANSLATION = "webus"
enum class Translation { webus, kjv, cunp, krv, jc }

val logger = LoggerFactory.getLogger("bbl")
val DEFAULT_TRANSLATION = Translation.webus

val logger = LoggerFactory.getLogger("bbl.cli")!!

data class VersePointer(
var translation: String = DEFAULT_TRANSLATION,
var translation: Translation = DEFAULT_TRANSLATION,
val book: Int = 0,
val chapter: Int = 0,
val startVerse: Int? = null,
val endVerse: Int? = null
)

fun parse(translation: String, book: List<String>, chapterVerse: String): VersePointer {
fun parse(translation: Translation, book: List<String>, chapterVerse: String): VersePointer {

val bookString = book.joinToString(separator = " ") { it.lowercase() }

val bookNumber = parseBook(bookString)
val bookNumber = bookNumber(bookString)

val chapterVerseSplit = chapterVerse.split(":")

Expand All @@ -44,14 +47,8 @@ fun parse(translation: String, book: List<String>, chapterVerse: String): VerseP
)
}

fun readFromResources(versePointer: VersePointer): String {
val path =
"/data/${versePointer.translation}/${versePointer.translation}.${versePointer.book}.${versePointer.chapter}.txt"

val text = object {}.javaClass.getResourceAsStream(path)?.use { it.reader(Charsets.UTF_8).readText() }

return text!!
}
fun chapterTextPath(versePointer: VersePointer) =
"texts/${versePointer.translation}/${versePointer.translation}.${versePointer.book}.${versePointer.chapter}.txt"

fun splitChapterToVerses(aChapter: String): Array<String> {
return aChapter.substring(2).split("\\n\\d{1,3} ".toRegex()).toTypedArray()
Expand Down Expand Up @@ -95,13 +92,13 @@ class Bbl(val config: Config) : CliktCommand(invokeWithoutSubcommand = true) {

override fun run() {

val translation = config.translation
versePointer = parse(translation, book, chapterVerse)
versePointer = parse(config.translation, book, chapterVerse)

val subcommand = currentContext.invokedSubcommand
if (subcommand == null) {

chapterText = readFromResources(versePointer)
val path = chapterTextPath(versePointer)
chapterText = getResourceReader().readText(path)
selectedVerses = selectVerses(versePointer, chapterText)
echo(selectedVerses)

Expand All @@ -112,18 +109,94 @@ class Bbl(val config: Config) : CliktCommand(invokeWithoutSubcommand = true) {
}
}

class In : CliktCommand(){
class In : CliktCommand() {

val translationOverride: String by argument()
val versePointer by requireObject<VersePointer>()

lateinit var selectedVerses: String

override fun run() {
versePointer.translation = translationOverride
selectedVerses = selectVerses(versePointer, readFromResources(versePointer))
versePointer.translation = Translation.valueOf(translationOverride)
val path = chapterTextPath(versePointer)
selectedVerses = selectVerses(versePointer, getResourceReader().readText(path))
echo(selectedVerses)
}
}

fun main(args: Array<String>) = Bbl(readConfigFromFileSystem()).subcommands(In()).main(args)
data class BookChapterFilter(
val book: Int? = null,
val startChapter: Int? = null,
val endChapter: Int? = null,
val term: String
)

class Search(val config: Config) : CliktCommand() {

val numberOfSearchResultVersesOverride by option("-r", "--result", help = "number of search result verses")
val searchInputs: List<String> by argument().multiple()

fun getTranslationFrom(searchInput: String): Translation? {

var overridden: Translation? = null

Translation.values().forEach { translation ->
if (searchInput.endsWith("in $translation")) {
overridden = translation
}
}

return overridden
}

lateinit var term: String
lateinit var translation: Translation
lateinit var result: List<String>

override fun run() {
val searchInput = searchInputs.joinToString(separator = " ")

// bbl search God in kjv -> "in kjv" will be detected
val overrideTranslation = getTranslationFrom(searchInput)
if (overrideTranslation == null) {
translation = config.translation
term = searchInput
} else {
translation = overrideTranslation
term = searchInput.replace("in $overrideTranslation", "").trim()
}

// bbl search Gdo in gen, in gen 1, or in gen 2-4 will be detected
val bookChapterFilter = filterByBookChapter(term)
term = bookChapterFilter.term

// bbl search God in kjv in gen 1 -> "in kjv" will be detected
val overrideTranslationInMiddle = getTranslationFrom(term)
if (overrideTranslationInMiddle != null) {
translation = overrideTranslationInMiddle
term = term.replace("in $overrideTranslationInMiddle", "").trim()
}

result = search(
term = term,
bookNumber = bookChapterFilter.book,
startChapter = bookChapterFilter.startChapter,
endChapter = bookChapterFilter.endChapter,
verses = numberOfSearchResultVersesOverride?.toInt() ?: config.searchResult,
translation = translation
)

result.forEach { verse ->
echo(verse)
}
}
}

fun main(args: Array<String>) {
val config = readConfigFromFileSystem()

Bbl(config).subcommands(
In(),
Search(config)
).main(args)
}
Loading

0 comments on commit ef10d70

Please sign in to comment.