Skip to content

Commit

Permalink
Re-add old deconjugator's validity test
Browse files Browse the repository at this point in the history
  • Loading branch information
wareya committed Mar 23, 2017
1 parent ff106ec commit 4db4b9c
Show file tree
Hide file tree
Showing 12 changed files with 431 additions and 224 deletions.
84 changes: 84 additions & 0 deletions src/language/deconjugator/AbstractWord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (C) 2017 Laurens Weyn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package language.deconjugator;

import language.dictionary.DefTag;
import language.dictionary.Definition;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
* Holds a possible valid conjugation if a word exists with the valid tags
* @author Laurens Weyn
*/
public abstract class AbstractWord
{
protected String word, originalWord;
protected Set<DefTag> neededTags;
protected String process;

public AbstractWord(String originalWord, String word, Set<DefTag> neededTags, String process)
{
this.originalWord = originalWord;
this.word = word;
this.neededTags = neededTags;
this.process = process.trim();
}
public AbstractWord(String word, String process)
{
this.originalWord = word;
this.word = word;
this.neededTags = new HashSet<>();
this.process = process;
}
public String getOriginalWord()
{
return originalWord;
}
public String getWord()
{
return word;
}

public Set<DefTag> getNeededTags()
{
return neededTags;
}

abstract public boolean defMatches(Definition def);

public String getProcess()
{
return process;
}

@Override
public String toString()
{
// hide non-freestanding weak forms and remove parens from freestanding ones
String temp = process;
if(temp.startsWith("("))
{
temp = temp.replaceFirst("[(]", "");
temp = temp.replaceFirst("[)]", "");
}
temp = temp.replaceAll("[(].*?[)]", "");
return word + "―" + temp;
}
}
4 changes: 3 additions & 1 deletion src/language/deconjugator/ContextRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public ContextRule(String ending, String replace, String change, DefTag neededTa
}

// Verifies and adds this rule as an inner-more conjugation in a deconjugated word
public ValidWord process(ValidWord word)
public AbstractWord process(AbstractWord abstractword)
{
ValidWord word = (ValidWord)abstractword;

if(!checker.l(this, word)) return null;

return super.process(word);
Expand Down
2 changes: 1 addition & 1 deletion src/language/deconjugator/DeconRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ public interface DeconRule
* @param word the word to attempt to deconjugate
* @return the deconjugated word, or null if it doesn't apply
*/
public abstract ValidWord process(ValidWord word);
public abstract AbstractWord process(AbstractWord word);
}
6 changes: 4 additions & 2 deletions src/language/deconjugator/StdRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.ArrayList;

/**
* Original (legacy) deconjugation rule.
* Standard (new) deconjugation rule.
* Created by Laurens on 2/18/2017.
*/
public class StdRule implements DeconRule
Expand Down Expand Up @@ -41,8 +41,10 @@ public DefTag getNeededTag()

@Override
// Verifies and adds this rule as an inner-more conjugation in a deconjugated word
public ValidWord process(ValidWord word)
public AbstractWord process(AbstractWord abstractword)
{
ValidWord word = (ValidWord)abstractword;

if(word.getWord().equals("")) return null; // can't deconjugate emptiness
if(!word.getWord().endsWith(ending)) return null; // can't possibly be valid

Expand Down
54 changes: 54 additions & 0 deletions src/language/deconjugator/StdRuleOld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package language.deconjugator;

import language.dictionary.DefTag;
import java.util.HashSet;

/**
* Original (legacy) deconjugation rule.
* Created by Laurens on 2/18/2017.
*/
public class StdRuleOld implements DeconRule
{
private String ending, replace, change;
private DefTag neededTag, impliedTag;
public StdRuleOld(String ending, String replace, String change, DefTag neededTag)
{
this(ending, replace, change, neededTag, null);
}
public StdRuleOld(String ending, String replace, String change, DefTag neededTag, DefTag impliedTag)
{
this.ending = ending;
this.replace = replace;
this.change = change;
this.neededTag = neededTag;
this.impliedTag = impliedTag;
}
public StdRuleOld(String ending, String replace, String change)
{
this(ending, replace, change, null);
}

@Override
public AbstractWord process(AbstractWord abstractword)
{
ValidWordOld word = (ValidWordOld)abstractword;

if(word.getProcess().contains(change))
{
return null;//don't stack the same conjugation onto itself
}
//ending matches:
if(word.getWord().endsWith(ending))
{
//add tag and return
HashSet<DefTag> tags = new HashSet<DefTag>(word.getNeededTags());
HashSet<DefTag> impliedTags = new HashSet<DefTag>(word.getImpliedTags());
if(neededTag != null)tags.add(neededTag);
if(impliedTag != null)impliedTags.add(impliedTag);
String newProcess = word.getProcess() + " " + change;
return new ValidWordOld(word.getWord().substring(0, word.getWord().length() - ending.length()) + replace, tags, impliedTags, newProcess);
}
//doesn't match, don't add new word
return null;
}
}
45 changes: 12 additions & 33 deletions src/language/deconjugator/ValidWord.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,57 +22,38 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;

/**
* Holds a possible valid conjugation if a word exists with the valid tags
* @author Laurens Weyn
*/
public class ValidWord
public class ValidWord extends AbstractWord
{
private String word, originalWord;
private Set<DefTag> neededTags;
private HashSet<String> seenForms;
private ArrayList<DefTag> conjugationTags;
private String process;
private Integer conjugations;
protected HashSet<String> seenForms;
protected ArrayList<DefTag> conjugationTags;
protected Integer conjugations;

public ValidWord(Integer conjugations, String originalWord, String word, HashSet<String> seenForms, Set<DefTag> neededTags, ArrayList<DefTag> conjugationTags, String process)
{
super(originalWord, word, neededTags, process);
this.conjugations = conjugations;
this.originalWord = originalWord;
this.seenForms = seenForms;
this.conjugationTags = conjugationTags;
this.word = word;
this.neededTags = neededTags;
this.process = process.trim();
}
public ValidWord(String word, String process)
{
super(word, process);
this.conjugations = 0;
this.originalWord = word;
this.word = word;
this.neededTags = new HashSet<>();
this.seenForms = new HashSet<>();
this.conjugationTags = new ArrayList<>();
this.process = process;
}
public Integer getNumConjugations()
{
return conjugations;
}
public String getOriginalWord()
{
return originalWord;
}
public String getWord()
{
return word;
}

public Set<DefTag> getNeededTags()
{
return neededTags;
}
public HashSet<String> getSeenForms()
{
return seenForms;
Expand All @@ -81,7 +62,6 @@ public ArrayList<DefTag> getConjugationTags()
{
return conjugationTags;
}

boolean hasSeenForm(String test)
{
return seenForms.contains(test);
Expand All @@ -95,16 +75,15 @@ boolean hasSeenForm(String test)
public boolean defMatches(Definition def)
{

if(def.getTags() == null && getNeededTags().isEmpty())return true;//still accept if no tags needed
else if (def.getTags() == null)return false;//does not have needed tags
if(def.getTags() == null && getNeededTags().isEmpty()) return true;//still accept if no tags needed
else if (def.getTags() == null) return false;//does not have needed tags

for(DefTag needed:getNeededTags())
{
if(needed.toString().equals("")) return false;
if(needed.toString().equals(""))
return false;
if(!def.getTags().contains(needed))
{
return false;
}
}
return true;
}
Expand Down
64 changes: 64 additions & 0 deletions src/language/deconjugator/ValidWordOld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2017 Laurens Weyn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package language.deconjugator;

import language.dictionary.DefTag;
import language.dictionary.Definition;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
* Holds a possible valid conjugation if a word exists with the valid tags
* @author Laurens Weyn
*/
public class ValidWordOld extends AbstractWord
{
private Set<DefTag> impliedTags;

public ValidWordOld(String word, Set<DefTag> neededTags, Set<DefTag> impliedTags, String process)
{
super(word, word, neededTags, process);
this.impliedTags = impliedTags;
}
public ValidWordOld(String word, String process)
{
super(word, process);
this.impliedTags = new HashSet<>();
}

public Set<DefTag> getImpliedTags()
{
return impliedTags;
}

public boolean defMatches(Definition def)
{
if(def.getTags() == null && getNeededTags().isEmpty())return true;//still accept if no tags needed
else if (def.getTags() == null)return false;//does not have needed tags

for(DefTag needed:getNeededTags())
{
if(!def.getTags().contains(needed) &&!getImpliedTags().contains(needed))
{
return false;
}
}
return true;
}
}

0 comments on commit 4db4b9c

Please sign in to comment.