Skip to content

Commit ff09cba

Browse files
authored
Merge pull request #33 from valencik/more-langs
Add More Languages
2 parents 70b594a + 3b2daa4 commit ff09cba

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

lucene/src/main/scala/textmogrify/lucene/AnalyzerBuilder.scala

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import org.apache.lucene.analysis.standard.StandardTokenizer
2222
import org.apache.lucene.analysis.en.PorterStemFilter
2323
import org.apache.lucene.analysis.es.SpanishLightStemFilter
2424
import org.apache.lucene.analysis.fr.FrenchLightStemFilter
25+
import org.apache.lucene.analysis.it.ItalianLightStemFilter
26+
import org.apache.lucene.analysis.de.GermanLightStemFilter
2527
import org.apache.lucene.analysis.LowerCaseFilter
2628
import org.apache.lucene.analysis.Analyzer
2729
import org.apache.lucene.analysis.miscellaneous.ASCIIFoldingFilter
@@ -103,6 +105,10 @@ object AnalyzerBuilder {
103105
new EnglishAnalyzerBuilder(Config.empty, false)
104106
def french: FrenchAnalyzerBuilder =
105107
new FrenchAnalyzerBuilder(Config.empty, false)
108+
def german: GermanAnalyzerBuilder =
109+
new GermanAnalyzerBuilder(Config.empty, false)
110+
def italian: ItalianAnalyzerBuilder =
111+
new ItalianAnalyzerBuilder(Config.empty, false)
106112
def spanish: SpanishAnalyzerBuilder =
107113
new SpanishAnalyzerBuilder(Config.empty, false)
108114
}
@@ -204,3 +210,55 @@ final class SpanishAnalyzerBuilder private[lucene] (
204210
def build[F[_]](implicit F: Sync[F]): Resource[F, Analyzer] =
205211
mkFromStandardTokenizer(config)(ts => if (self.stemmer) new SpanishLightStemFilter(ts) else ts)
206212
}
213+
214+
final class ItalianAnalyzerBuilder private[lucene] (
215+
config: Config,
216+
stemmer: Boolean,
217+
) extends AnalyzerBuilder(config) { self =>
218+
type Builder = ItalianAnalyzerBuilder
219+
220+
private def copy(
221+
newConfig: Config,
222+
stemmer: Boolean = self.stemmer,
223+
): ItalianAnalyzerBuilder =
224+
new ItalianAnalyzerBuilder(newConfig, stemmer)
225+
226+
def withConfig(newConfig: Config): ItalianAnalyzerBuilder =
227+
copy(newConfig = newConfig)
228+
229+
/** Adds the ItalianLight Stemmer to the end of the analyzer pipeline and enables lowercasing.
230+
* Stemming reduces words like `jumping` and `jumps` to their root word `jump`.
231+
* NOTE: Lowercasing is forced as it is required for the Lucene ItalianLightStemFilter.
232+
*/
233+
def withItalianLightStemmer: ItalianAnalyzerBuilder =
234+
copy(config.copy(lowerCase = true), stemmer = true)
235+
236+
def build[F[_]](implicit F: Sync[F]): Resource[F, Analyzer] =
237+
mkFromStandardTokenizer(config)(ts => if (self.stemmer) new ItalianLightStemFilter(ts) else ts)
238+
}
239+
240+
final class GermanAnalyzerBuilder private[lucene] (
241+
config: Config,
242+
stemmer: Boolean,
243+
) extends AnalyzerBuilder(config) { self =>
244+
type Builder = GermanAnalyzerBuilder
245+
246+
private def copy(
247+
newConfig: Config,
248+
stemmer: Boolean = self.stemmer,
249+
): GermanAnalyzerBuilder =
250+
new GermanAnalyzerBuilder(newConfig, stemmer)
251+
252+
def withConfig(newConfig: Config): GermanAnalyzerBuilder =
253+
copy(newConfig = newConfig)
254+
255+
/** Adds the GermanLight Stemmer to the end of the analyzer pipeline and enables lowercasing.
256+
* Stemming reduces words like `jumping` and `jumps` to their root word `jump`.
257+
* NOTE: Lowercasing is forced as it is required for the Lucene GermanLightStemFilter.
258+
*/
259+
def withGermanLightStemmer: GermanAnalyzerBuilder =
260+
copy(config.copy(lowerCase = true), stemmer = true)
261+
262+
def build[F[_]](implicit F: Sync[F]): Resource[F, Analyzer] =
263+
mkFromStandardTokenizer(config)(ts => if (self.stemmer) new GermanLightStemFilter(ts) else ts)
264+
}

lucene/src/test/scala/textmogrify/lucene/AnalyzerBuilderSuite.scala

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,95 @@ class SpanishAnalyzerBuilderSuite extends CatsEffectSuite {
189189
}
190190

191191
}
192+
193+
class ItalianAnalyzerBuilderSuite extends CatsEffectSuite {
194+
195+
val jalapenos = "Mi piacciono i jalapeños"
196+
val jumping = "A Neeko piace saltare sui contatori"
197+
198+
test("italian analyzer default should tokenize without any transformations") {
199+
val analyzer = AnalyzerBuilder.italian
200+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
201+
assertIO(actual, Vector("Mi", "piacciono", "i", "jalapeños"))
202+
}
203+
204+
test("italian analyzer withLowerCasing should lowercase all letters") {
205+
val analyzer = AnalyzerBuilder.italian.withLowerCasing
206+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
207+
assertIO(actual, Vector("mi", "piacciono", "i", "jalapeños"))
208+
}
209+
210+
test("italian analyzer withASCIIFolding should fold 'ñ' to 'n'") {
211+
val analyzer = AnalyzerBuilder.italian.withASCIIFolding
212+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
213+
assertIO(actual, Vector("Mi", "piacciono", "i", "jalapenos"))
214+
}
215+
216+
test("italian analyzer withStopWords should filter them out") {
217+
val analyzer = AnalyzerBuilder.italian.withStopWords(Set("i"))
218+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
219+
assertIO(actual, Vector("Mi", "piacciono", "jalapeños"))
220+
}
221+
222+
test("italian analyzer withItalianLightStemmer should lowercase and stem words") {
223+
val analyzer = AnalyzerBuilder.italian.withItalianLightStemmer
224+
val actual = analyzer.tokenizer[IO].use(f => f(jumping))
225+
assertIO(actual, Vector("a", "neeko", "piace", "saltar", "sui", "contator"))
226+
}
227+
228+
test("italian analyzer builder settings can be chained") {
229+
val analyzer = AnalyzerBuilder.italian.withItalianLightStemmer
230+
.withStopWords(Set("a", "sui"))
231+
.withASCIIFolding
232+
.withLowerCasing
233+
val actual = analyzer.tokenizer[IO].use(f => f(jumping))
234+
assertIO(actual, Vector("neeko", "piace", "saltar", "contator"))
235+
}
236+
237+
}
238+
239+
class GermanAnalyzerBuilderSuite extends CatsEffectSuite {
240+
241+
val jalapenos = "Ich mag Jalapeños"
242+
val jumping = "Neeko springt gerne auf Theken"
243+
244+
test("german analyzer default should tokenize without any transformations") {
245+
val analyzer = AnalyzerBuilder.german
246+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
247+
assertIO(actual, Vector("Ich", "mag", "Jalapeños"))
248+
}
249+
250+
test("german analyzer withLowerCasing should lowercase all letters") {
251+
val analyzer = AnalyzerBuilder.german.withLowerCasing
252+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
253+
assertIO(actual, Vector("ich", "mag", "jalapeños"))
254+
}
255+
256+
test("german analyzer withASCIIFolding should fold 'ñ' to 'n'") {
257+
val analyzer = AnalyzerBuilder.german.withASCIIFolding
258+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
259+
assertIO(actual, Vector("Ich", "mag", "Jalapenos"))
260+
}
261+
262+
test("german analyzer withStopWords should filter them out") {
263+
val analyzer = AnalyzerBuilder.german.withStopWords(Set("Ich"))
264+
val actual = analyzer.tokenizer[IO].use(f => f(jalapenos))
265+
assertIO(actual, Vector("mag", "Jalapeños"))
266+
}
267+
268+
test("german analyzer withGermanLightStemmer should lowercase and stem words") {
269+
val analyzer = AnalyzerBuilder.german.withGermanLightStemmer
270+
val actual = analyzer.tokenizer[IO].use(f => f(jumping))
271+
assertIO(actual, Vector("neeko", "springt", "gern", "auf", "thek"))
272+
}
273+
274+
test("german analyzer builder settings can be chained") {
275+
val analyzer = AnalyzerBuilder.german.withGermanLightStemmer
276+
.withStopWords(Set("auf"))
277+
.withASCIIFolding
278+
.withLowerCasing
279+
val actual = analyzer.tokenizer[IO].use(f => f(jumping))
280+
assertIO(actual, Vector("neeko", "springt", "gern", "thek"))
281+
}
282+
283+
}

0 commit comments

Comments
 (0)