Search in sources :

Example 1 with SuffixTemplateToken

use of zemberek.morphology.analysis.SurfaceTransition.SuffixTemplateToken in project zemberek-nlp by ahmetaa.

the class WordGenerator method advance.

// for all allowed matching outgoing transitions, new paths are generated.
// Transition conditions are used for checking if a search path is allowed to pass a transition.
private List<GenerationPath> advance(GenerationPath gPath) {
    List<GenerationPath> newPaths = new ArrayList<>(2);
    // for all outgoing transitions.
    for (MorphemeTransition transition : gPath.path.getCurrentState().getOutgoing()) {
        SuffixTransition suffixTransition = (SuffixTransition) transition;
        // if there are no morphemes and this transitions surface is not empty, no need to check.
        if (gPath.morphemes.isEmpty() && suffixTransition.hasSurfaceForm()) {
            if (debugMode) {
                debugData.rejectedTransitions.put(gPath.path, new RejectedTransition(suffixTransition, "Empty surface expected."));
            }
            continue;
        }
        // if transition surface is empty, here will pass.
        if (!gPath.matches(suffixTransition)) {
            if (debugMode) {
                debugData.rejectedTransitions.put(gPath.path, new RejectedTransition(suffixTransition, "Morpheme mismatch." + suffixTransition.to.morpheme));
            }
            continue;
        }
        // if transition condition fails, add it to debug data.
        if (debugMode && suffixTransition.getCondition() != null) {
            Condition condition = suffixTransition.getCondition();
            Condition failed;
            if (condition instanceof CombinedCondition) {
                failed = ((CombinedCondition) condition).getFailingCondition(gPath.path);
            } else {
                failed = condition.accept(gPath.path) ? null : condition;
            }
            if (failed != null) {
                debugData.rejectedTransitions.put(gPath.path, new RejectedTransition(suffixTransition, "Condition → " + failed.toString()));
            }
        }
        // check conditions.
        if (!suffixTransition.canPass(gPath.path)) {
            continue;
        }
        // epsilon transition. Add and continue. Use existing attributes.
        if (!suffixTransition.hasSurfaceForm()) {
            SearchPath pCopy = gPath.path.getCopyForGeneration(new SurfaceTransition("", suffixTransition), gPath.path.getPhoneticAttributes());
            newPaths.add(gPath.copy(pCopy));
            continue;
        }
        String surface = SurfaceTransition.generateSurface(suffixTransition, gPath.path.getPhoneticAttributes());
        SurfaceTransition surfaceTransition = new SurfaceTransition(surface, suffixTransition);
        // if tail is equal to surface, no need to calculate phonetic attributes.
        AttributeSet<PhoneticAttribute> attributes = AttributesHelper.getMorphemicAttributes(surface, gPath.path.getPhoneticAttributes());
        // This is required for suffixes like `cik` and `ciğ`
        // an extra attribute is added if "cik" or "ciğ" is generated and matches the tail.
        // if "cik" is generated, ExpectsConsonant attribute is added, so only a consonant starting
        // suffix can follow. Likewise, if "ciğ" is produced, a vowel starting suffix is allowed.
        attributes.remove(PhoneticAttribute.CannotTerminate);
        SuffixTemplateToken lastToken = suffixTransition.getLastTemplateToken();
        if (lastToken.getType() == TemplateTokenType.LAST_VOICED) {
            attributes.add(PhoneticAttribute.ExpectsConsonant);
        } else if (lastToken.getType() == TemplateTokenType.LAST_NOT_VOICED) {
            attributes.add(PhoneticAttribute.ExpectsVowel);
            attributes.add(PhoneticAttribute.CannotTerminate);
        }
        SearchPath p = gPath.path.getCopyForGeneration(surfaceTransition, attributes);
        newPaths.add(gPath.copy(p));
    }
    return newPaths;
}
Also used : RejectedTransition(zemberek.morphology.analysis.AnalysisDebugData.RejectedTransition) CombinedCondition(zemberek.morphology.morphotactics.CombinedCondition) Condition(zemberek.morphology.morphotactics.Condition) SuffixTransition(zemberek.morphology.morphotactics.SuffixTransition) ArrayList(java.util.ArrayList) SurfaceTransition(zemberek.morphology.analysis.SurfaceTransition) CombinedCondition(zemberek.morphology.morphotactics.CombinedCondition) MorphemeTransition(zemberek.morphology.morphotactics.MorphemeTransition) PhoneticAttribute(zemberek.core.turkish.PhoneticAttribute) SearchPath(zemberek.morphology.analysis.SearchPath) SuffixTemplateToken(zemberek.morphology.analysis.SurfaceTransition.SuffixTemplateToken)

Example 2 with SuffixTemplateToken

use of zemberek.morphology.analysis.SurfaceTransition.SuffixTemplateToken in project zemberek-nlp by ahmetaa.

the class RuleBasedAnalyzer method advance.

// for all allowed matching outgoing transitions, new paths are generated.
// Transition `conditions` are used for checking if a `search path`
// is allowed to pass a transition.
private List<SearchPath> advance(SearchPath path) {
    List<SearchPath> newPaths = new ArrayList<>(2);
    // for all outgoing transitions.
    for (MorphemeTransition transition : path.currentState.getOutgoing()) {
        SuffixTransition suffixTransition = (SuffixTransition) transition;
        // if tail is empty and this transitions surface is not empty, no need to check.
        if (path.tail.isEmpty() && suffixTransition.hasSurfaceForm()) {
            if (debugMode) {
                debugData.rejectedTransitions.put(path, new RejectedTransition(suffixTransition, "Empty surface expected."));
            }
            continue;
        }
        String surface = SurfaceTransition.generateSurface(suffixTransition, path.phoneticAttributes);
        // no need to go further if generated surface form is not a prefix of the paths's tail.
        boolean tailStartsWith = asciiTolerant ? TurkishAlphabet.INSTANCE.startsWithIgnoreDiacritics(path.tail, surface) : path.tail.startsWith(surface);
        if (!tailStartsWith) {
            if (debugMode) {
                debugData.rejectedTransitions.put(path, new RejectedTransition(suffixTransition, "Surface Mismatch:" + surface));
            }
            continue;
        }
        // if transition condition fails, add it to debug data.
        if (debugMode && suffixTransition.getCondition() != null) {
            Condition condition = suffixTransition.getCondition();
            Condition failed;
            if (condition instanceof CombinedCondition) {
                failed = ((CombinedCondition) condition).getFailingCondition(path);
            } else {
                failed = condition.accept(path) ? null : condition;
            }
            if (failed != null) {
                debugData.rejectedTransitions.put(path, new RejectedTransition(suffixTransition, "Condition → " + failed.toString()));
            }
        }
        // check conditions.
        if (!suffixTransition.canPass(path)) {
            continue;
        }
        // epsilon (empty) transition. Add and continue. Use existing attributes.
        if (!suffixTransition.hasSurfaceForm()) {
            newPaths.add(path.getCopy(new SurfaceTransition("", suffixTransition), path.phoneticAttributes));
            continue;
        }
        SurfaceTransition surfaceTransition = new SurfaceTransition(surface, suffixTransition);
        // if tail is equal to surface, no need to calculate phonetic attributes.
        boolean tailEqualsSurface = asciiTolerant ? TurkishAlphabet.INSTANCE.equalsIgnoreDiacritics(path.tail, surface) : path.tail.equals(surface);
        AttributeSet<PhoneticAttribute> attributes = tailEqualsSurface ? path.phoneticAttributes.copy() : AttributesHelper.getMorphemicAttributes(surface, path.phoneticAttributes);
        // This is required for suffixes like `cik` and `ciğ`
        // an extra attribute is added if "cik" or "ciğ" is generated and matches the tail.
        // if "cik" is generated, ExpectsConsonant attribute is added, so only a consonant starting
        // suffix can follow. Likewise, if "ciğ" is produced, a vowel starting suffix is allowed.
        attributes.remove(PhoneticAttribute.CannotTerminate);
        SuffixTemplateToken lastToken = suffixTransition.getLastTemplateToken();
        if (lastToken.type == TemplateTokenType.LAST_VOICED) {
            attributes.add(PhoneticAttribute.ExpectsConsonant);
        } else if (lastToken.type == TemplateTokenType.LAST_NOT_VOICED) {
            attributes.add(PhoneticAttribute.ExpectsVowel);
            attributes.add(PhoneticAttribute.CannotTerminate);
        }
        SearchPath p = path.getCopy(surfaceTransition, attributes);
        newPaths.add(p);
    }
    return newPaths;
}
Also used : RejectedTransition(zemberek.morphology.analysis.AnalysisDebugData.RejectedTransition) CombinedCondition(zemberek.morphology.morphotactics.CombinedCondition) Condition(zemberek.morphology.morphotactics.Condition) SuffixTransition(zemberek.morphology.morphotactics.SuffixTransition) ArrayList(java.util.ArrayList) CombinedCondition(zemberek.morphology.morphotactics.CombinedCondition) MorphemeTransition(zemberek.morphology.morphotactics.MorphemeTransition) PhoneticAttribute(zemberek.core.turkish.PhoneticAttribute) SuffixTemplateToken(zemberek.morphology.analysis.SurfaceTransition.SuffixTemplateToken)

Aggregations

ArrayList (java.util.ArrayList)2 PhoneticAttribute (zemberek.core.turkish.PhoneticAttribute)2 RejectedTransition (zemberek.morphology.analysis.AnalysisDebugData.RejectedTransition)2 SuffixTemplateToken (zemberek.morphology.analysis.SurfaceTransition.SuffixTemplateToken)2 CombinedCondition (zemberek.morphology.morphotactics.CombinedCondition)2 Condition (zemberek.morphology.morphotactics.Condition)2 MorphemeTransition (zemberek.morphology.morphotactics.MorphemeTransition)2 SuffixTransition (zemberek.morphology.morphotactics.SuffixTransition)2 SearchPath (zemberek.morphology.analysis.SearchPath)1 SurfaceTransition (zemberek.morphology.analysis.SurfaceTransition)1