use of zemberek.morphology._morphotactics.AttributeSet in project zemberek-nlp by ahmetaa.
the class AttributesHelper method getMorphemicAttributes.
public static AttributeSet<PhoneticAttribute> getMorphemicAttributes(CharSequence seq, AttributeSet<PhoneticAttribute> predecessorAttrs) {
if (seq.length() == 0) {
return predecessorAttrs.copy();
}
AttributeSet<PhoneticAttribute> attrs = new AttributeSet<>();
if (alphabet.containsVowel(seq)) {
TurkicLetter last = alphabet.getLastLetter(seq);
if (last.isVowel()) {
attrs.add(LastLetterVowel);
} else {
attrs.add(LastLetterConsonant);
}
TurkicLetter lastVowel = last.isVowel() ? last : alphabet.getLastVowel(seq);
if (lastVowel.isFrontal()) {
attrs.add(LastVowelFrontal);
} else {
attrs.add(LastVowelBack);
}
if (lastVowel.isRounded()) {
attrs.add(LastVowelRounded);
} else {
attrs.add(LastVowelUnrounded);
}
if (alphabet.getFirstLetter(seq).isVowel()) {
attrs.add(FirstLetterVowel);
} else {
attrs.add(FirstLetterConsonant);
}
} else {
// we transfer vowel attributes from the predecessor attributes.
attrs.copyFrom(predecessorAttrs);
attrs.addAll(NO_VOWEL_ATTRIBUTES);
attrs.remove(LastLetterVowel);
attrs.remove(ExpectsConsonant);
}
TurkicLetter last = alphabet.getLastLetter(seq);
if (last.isVoiceless()) {
attrs.add(LastLetterVoiceless);
if (last.isStopConsonant()) {
// kitap
attrs.add(LastLetterVoicelessStop);
}
} else {
attrs.add(LastLetterVoiced);
}
return attrs;
}
use of zemberek.morphology._morphotactics.AttributeSet in project zemberek-nlp by ahmetaa.
the class InterpretingAnalyzer 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, AnalysisDebugData debugData) {
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 (debugData != null) {
debugData.rejectedTransitions.put(path, new RejectedTransition(suffixTransition, "Empty surface expected."));
}
continue;
}
String surface = SurfaceTransition.generate(suffixTransition, path.phoneticAttributes);
// no need to go further if generated surface form is not a prefix of the paths's tail.
if (!path.tail.startsWith(surface)) {
if (debugData != null) {
debugData.rejectedTransitions.put(path, new RejectedTransition(suffixTransition, "Surface Mismatch:" + surface));
}
continue;
}
// if transition condition fails, add it to debug data.
if (debugData != null && 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 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.
AttributeSet<PhoneticAttribute> attributes = path.tail.equals(surface) ? 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;
}
use of zemberek.morphology._morphotactics.AttributeSet in project zemberek-nlp by ahmetaa.
the class StemTransitionGenerator method handleSpecialRoots.
private List<StemTransition> handleSpecialRoots(DictionaryItem item) {
String id = item.getId();
AttributeSet<PhoneticAttribute> originalAttrs = calculateAttributes(item.pronunciation);
StemTransition original, modified;
MorphemeState unmodifiedRootState = morphotactics.getRootState(item, originalAttrs);
switch(id) {
case "içeri_Noun":
case "içeri_Adj":
case "dışarı_Adj":
case "dışarı_Noun":
case "dışarı_Postp":
case "yukarı_Noun":
case "yukarı_Adj":
case "şura_Noun":
case "bura_Noun":
case "ora_Noun":
original = new StemTransition(item.root, item, originalAttrs, unmodifiedRootState);
MorphemeState rootForModified;
switch(item.primaryPos) {
case Noun:
rootForModified = morphotactics.nounLastVowelDropRoot_S;
break;
case Adjective:
rootForModified = morphotactics.adjLastVowelDropRoot_S;
break;
// TODO: check postpositive case. Maybe it is not required.
case PostPositive:
rootForModified = morphotactics.adjLastVowelDropRoot_S;
break;
default:
throw new IllegalStateException("No root morpheme state found for " + item);
}
String m = item.root.substring(0, item.root.length() - 1);
modified = new StemTransition(m, item, calculateAttributes(m), rootForModified);
modified.getPhoneticAttributes().add(PhoneticAttribute.ExpectsConsonant);
modified.getPhoneticAttributes().add(PhoneticAttribute.CannotTerminate);
return Lists.newArrayList(original, modified);
case "ben_Pron_Pers":
case "sen_Pron_Pers":
original = new StemTransition(item.root, item, originalAttrs, unmodifiedRootState);
if (item.lemma.equals("ben")) {
modified = new StemTransition("ban", item, calculateAttributes("ban"), morphotactics.pronPers_Mod_S);
} else {
modified = new StemTransition("san", item, calculateAttributes("san"), morphotactics.pronPers_Mod_S);
}
original.getPhoneticAttributes().add(PhoneticAttribute.UnModifiedPronoun);
modified.getPhoneticAttributes().add(PhoneticAttribute.ModifiedPronoun);
return Lists.newArrayList(original, modified);
case "demek_Verb":
case "yemek_Verb":
original = new StemTransition(item.root, item, originalAttrs, morphotactics.vDeYeRoot_S);
switch(item.lemma) {
case "demek":
modified = new StemTransition("di", item, calculateAttributes("di"), morphotactics.vDeYeRoot_S);
break;
default:
modified = new StemTransition("yi", item, calculateAttributes("yi"), morphotactics.vDeYeRoot_S);
}
return Lists.newArrayList(original, modified);
case "birbiri_Pron_Quant":
case "çoğu_Pron_Quant":
case "öbürü_Pron_Quant":
case "birçoğu_Pron_Quant":
original = new StemTransition(item.root, item, originalAttrs, morphotactics.pronQuant_S);
switch(item.lemma) {
case "birbiri":
modified = new StemTransition("birbir", item, calculateAttributes("birbir"), morphotactics.pronQuantModified_S);
break;
case "çoğu":
modified = new StemTransition("çok", item, calculateAttributes("çok"), morphotactics.pronQuantModified_S);
break;
case "öbürü":
modified = new StemTransition("öbür", item, calculateAttributes("öbür"), morphotactics.pronQuantModified_S);
break;
default:
modified = new StemTransition("birçok", item, calculateAttributes("birçok"), morphotactics.pronQuantModified_S);
break;
}
original.getPhoneticAttributes().add(PhoneticAttribute.UnModifiedPronoun);
modified.getPhoneticAttributes().add(PhoneticAttribute.ModifiedPronoun);
return Lists.newArrayList(original, modified);
default:
throw new IllegalArgumentException("Lexicon Item with special stem change cannot be handled:" + item);
}
}
use of zemberek.morphology._morphotactics.AttributeSet in project zemberek-nlp by ahmetaa.
the class StemTransitionGenerator method generateModifiedRootNodes.
private List<StemTransition> generateModifiedRootNodes(DictionaryItem dicItem) {
StringBuilder modifiedSeq = new StringBuilder(dicItem.pronunciation);
AttributeSet<PhoneticAttribute> originalAttrs = calculateAttributes(dicItem.pronunciation);
AttributeSet<PhoneticAttribute> modifiedAttrs = originalAttrs.copy();
MorphemeState modifiedRootState = null;
MorphemeState unmodifiedRootState = null;
for (RootAttribute attribute : dicItem.attributes) {
// generate other boundary attributes and modified root state.
switch(attribute) {
case Voicing:
char last = alphabet.getLastChar(modifiedSeq);
char voiced = alphabet.voice(last);
if (last == voiced) {
throw new LexiconException("Voicing letter is not proper in:" + dicItem);
}
if (dicItem.lemma.endsWith("nk")) {
voiced = 'g';
}
modifiedSeq.setCharAt(modifiedSeq.length() - 1, voiced);
modifiedAttrs.remove(PhoneticAttribute.LastLetterVoicelessStop);
originalAttrs.add(PhoneticAttribute.ExpectsConsonant);
modifiedAttrs.add(PhoneticAttribute.ExpectsVowel);
// TODO: find a better way for this.
modifiedAttrs.add(PhoneticAttribute.CannotTerminate);
break;
case Doubling:
modifiedSeq.append(alphabet.getLastChar(modifiedSeq));
originalAttrs.add(PhoneticAttribute.ExpectsConsonant);
modifiedAttrs.add(PhoneticAttribute.ExpectsVowel);
modifiedAttrs.add(PhoneticAttribute.CannotTerminate);
break;
case LastVowelDrop:
TurkicLetter lastLetter = alphabet.getLastLetter(modifiedSeq);
if (lastLetter.isVowel()) {
modifiedSeq.deleteCharAt(modifiedSeq.length() - 1);
modifiedAttrs.add(PhoneticAttribute.ExpectsConsonant);
modifiedAttrs.add(PhoneticAttribute.CannotTerminate);
} else {
modifiedSeq.deleteCharAt(modifiedSeq.length() - 2);
if (!dicItem.primaryPos.equals(PrimaryPos.Verb)) {
originalAttrs.add(PhoneticAttribute.ExpectsConsonant);
} else {
unmodifiedRootState = morphotactics.verbLastVowelDropUnmodRoot_S;
modifiedRootState = morphotactics.verbLastVowelDropModRoot_S;
}
modifiedAttrs.add(PhoneticAttribute.ExpectsVowel);
modifiedAttrs.add(PhoneticAttribute.CannotTerminate);
}
break;
case InverseHarmony:
originalAttrs.add(PhoneticAttribute.LastVowelFrontal);
originalAttrs.remove(PhoneticAttribute.LastVowelBack);
modifiedAttrs.add(PhoneticAttribute.LastVowelFrontal);
modifiedAttrs.remove(PhoneticAttribute.LastVowelBack);
break;
case ProgressiveVowelDrop:
modifiedSeq.deleteCharAt(modifiedSeq.length() - 1);
if (alphabet.containsVowel(modifiedSeq)) {
modifiedAttrs = calculateAttributes(modifiedSeq);
}
modifiedAttrs.add(PhoneticAttribute.LastLetterDropped);
break;
default:
break;
}
}
if (unmodifiedRootState == null) {
unmodifiedRootState = morphotactics.getRootState(dicItem, originalAttrs);
}
StemTransition original = new StemTransition(dicItem.root, dicItem, originalAttrs, unmodifiedRootState);
// if modified root state is not defined in the switch block, get it from morphotactics.
if (modifiedRootState == null) {
modifiedRootState = morphotactics.getRootState(dicItem, modifiedAttrs);
}
StemTransition modified = new StemTransition(modifiedSeq.toString(), dicItem, modifiedAttrs, modifiedRootState);
if (original.equals(modified)) {
return Collections.singletonList(original);
}
return Lists.newArrayList(original, modified);
}
use of zemberek.morphology._morphotactics.AttributeSet in project zemberek-nlp by ahmetaa.
the class StemTransitionGenerator method generate.
/**
* Generates StemTransition objects from the dictionary item. <p>Most of the time a single
* StemNode is generated.
*
* @param item DictionaryItem
* @return one or more StemTransition objects.
*/
public List<StemTransition> generate(DictionaryItem item) {
if (specialRoots.contains(item.id)) {
return handleSpecialRoots(item);
}
if (hasModifierAttribute(item)) {
return generateModifiedRootNodes(item);
} else {
AttributeSet<PhoneticAttribute> phoneticAttributes = calculateAttributes(item.pronunciation);
StemTransition transition = new StemTransition(item.root, item, phoneticAttributes, morphotactics.getRootState(item, phoneticAttributes));
return Lists.newArrayList(transition);
}
}
Aggregations