com.cobber.fta.plugins.person.BirthYear Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fta Show documentation
Show all versions of fta Show documentation
Analyze Text data to determine simple type and Semantic type information as well as other key metrics associated with a text stream.
/*
* Copyright 2017-2024 Tim Segall
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cobber.fta.plugins.person;
import java.time.LocalDate;
import com.cobber.fta.AnalysisConfig;
import com.cobber.fta.AnalyzerContext;
import com.cobber.fta.Facts;
import com.cobber.fta.FiniteMap;
import com.cobber.fta.LogicalTypeInfinite;
import com.cobber.fta.PluginAnalysis;
import com.cobber.fta.PluginDefinition;
import com.cobber.fta.core.FTAPluginException;
import com.cobber.fta.core.Utils;
import com.cobber.fta.dates.DateTimeParser.DateResolutionMode;
import com.cobber.fta.dates.DateTimeParserConfig;
import com.cobber.fta.dates.DateTimeParserResult;
import com.cobber.fta.token.TokenStreams;
/**
* Plugin to detect Birth Year (Person).
*/
public class BirthYear extends LogicalTypeInfinite {
private final static String dateFormat = "yyyy";
private String regExp;
/**
* Construct a plugin based on the Plugin Definition.
* @param plugin The definition of this plugin.
*/
public BirthYear(final PluginDefinition plugin) {
super(plugin);
}
@Override
public boolean initialize(final AnalysisConfig analysisConfig) throws FTAPluginException {
super.initialize(analysisConfig);
regExp = DateTimeParserResult.asResult(dateFormat, DateResolutionMode.None, new DateTimeParserConfig()).getRegExp();
return true;
}
@Override
public String nextRandom() {
return String.valueOf(LocalDate.now().getYear() - getRandom().nextInt(99) + 1);
}
@Override
public boolean isRegExpComplete() {
return false;
}
@Override
public boolean isValid(final String input, final boolean detectMode, final long count) {
return validate(input.trim());
}
private boolean validate(final String trimmed) {
return trimmed.length() == 4 && Utils.isNumeric(trimmed) && Integer.parseInt(trimmed) > 1600;
}
@Override
public boolean isCandidate(final String trimmed, final StringBuilder compressed, final int[] charCounts, final int[] lastIndex) {
return validate(trimmed);
}
@Override
public String getRegExp() {
return regExp;
}
@Override
public PluginAnalysis analyzeSet(final AnalyzerContext context, final long matchCount, final long realSamples, final String currentRegExp,
final Facts facts, final FiniteMap cardinality, final FiniteMap outliers, final TokenStreams tokenStreams,
final AnalysisConfig analysisConfig) {
return getConfidence(matchCount, realSamples, context) >= getThreshold() / 100.0 ? PluginAnalysis.OK : PluginAnalysis.SIMPLE_NOT_OK;
}
}