Autopsy  4.14.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
LanguageDetector.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.keywordsearch;
20 
21 import com.optimaize.langdetect.LanguageDetectorBuilder;
22 import com.optimaize.langdetect.i18n.LdLocale;
23 import com.optimaize.langdetect.ngram.NgramExtractors;
24 import com.optimaize.langdetect.profiles.LanguageProfileReader;
25 import com.optimaize.langdetect.text.CommonTextObjectFactories;
26 import com.optimaize.langdetect.text.TextObject;
27 import com.optimaize.langdetect.text.TextObjectFactory;
28 
29 import java.io.IOException;
30 import java.io.UncheckedIOException;
31 import java.util.Optional;
32 
37 class LanguageDetector {
38 
39  private com.optimaize.langdetect.LanguageDetector impl;
40  private TextObjectFactory textObjectFactory;
41 
42  LanguageDetector() {
43  try {
44  impl = LanguageDetectorBuilder.create(NgramExtractors.standard())
45  .withProfiles(new LanguageProfileReader().readAllBuiltIn())
46  .build();
47  textObjectFactory = CommonTextObjectFactories.forDetectingOnLargeText();
48  } catch (IOException e) {
49  // The IOException here could occur when failing to read the language profiles from the classpath.
50  // That can be considered to be a severe IO problem. Nothing can be done here.
51  throw new UncheckedIOException(e);
52  }
53  }
54 
55  Optional<Language> detect(String text) {
56  TextObject textObject = textObjectFactory.forText(text);
57  Optional<LdLocale> localeOpt = impl.detect(textObject).transform(Optional::of).or(Optional.empty());
58  return localeOpt.map(LdLocale::getLanguage).flatMap(Language::fromValue);
59  }
60 }
static Optional< Language > fromValue(String value)
Definition: Language.java:39

Copyright © 2012-2020 Basis Technology. Generated on: Wed Apr 8 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.