The code might be changed and you can browse the master branch of repository here.
Created
June 18, 2017 20:14
-
-
Save erayerdin/b1ea946357b0d2984e1098aa84452ec9 to your computer and use it in GitHub Desktop.
A basic implementation of generative grammar in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erayerdin.linglib.syntax.generative; | |
import java.util.HashMap; | |
/** | |
* A Category for syntactic structure. | |
* | |
* @author Eray Erdin | |
* @see com.erayerdin.linglib.syntax.generative.Phrase | |
*/ | |
public class Category implements java.io.Serializable { | |
public String name; | |
protected Character initialChar; | |
// Category Library | |
public static Category NOUN = new Category("Noun"); | |
public static Category ADJECTIVE = new Category("Adjective"); | |
public static Category DETERMINER = new Category("Determiner"); | |
public static Category VERB = new Category("Verb"); | |
public static Category ADVERB = new Category("Adverb"); | |
public static Category INFLECTION = new Category("Inflection"); | |
public static Category COMPLEMENTIZER = new Category("Complementizer"); | |
public static HashMap<String, Category> COMMONS = new HashMap<String, Category>(){{ | |
put("noun", NOUN); | |
put("adjective", ADJECTIVE); | |
put("determiner", DETERMINER); | |
put("verb", VERB); | |
put("adverb", ADVERB); | |
put("inflection", INFLECTION); | |
put("complementizer", COMPLEMENTIZER); | |
}}; | |
public Category(String name) { | |
this.name = name; | |
this.initialChar = this.name.charAt(0); | |
} | |
public String toString() { | |
return String.format("Category: %s", this.name); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erayerdin.linglib.syntax.generative; | |
/** | |
* A Parameter for generative grammar. | |
* Parameter is passed to render function of any | |
* Phrase object to linearize a Phrase object | |
* to a string. | |
* | |
* @author Eray Erdin | |
* @see com.erayerdin.linglib.syntax.generative.Phrase | |
*/ | |
public class Parameter implements java.io.Serializable { | |
protected boolean complement; | |
protected boolean adjunct; | |
protected boolean specifier; | |
public Parameter(boolean complement, boolean adjunct, boolean specifier) { | |
this.complement = complement; | |
this.adjunct = adjunct; | |
this.specifier = specifier; | |
} | |
public Parameter(boolean complement, boolean adjunct) { | |
this.complement = complement; | |
this.adjunct = adjunct; | |
this.specifier = false; | |
} | |
public String toString() { | |
java.util.ArrayList<String> places = new java.util.ArrayList<String>(); | |
places.add("terminal"); | |
if (this.complement == false) | |
places.add(0, "[complement]"); | |
else | |
places.add("[complement]"); | |
if (this.adjunct == false) | |
places.add(0, "[adjunct...]"); | |
else | |
places.add("[adjunct...]"); | |
if (this.specifier == false) | |
places.add(0, "[specifier]"); | |
else | |
places.add("[specifier]"); | |
String placesString = Phrase.mergeStringArray(places); | |
String string = String.format("Parameter: %s", placesString); | |
return string; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.erayerdin.linglib.syntax.generative; | |
import java.util.ArrayList; | |
/** | |
* A Phrase in Generative syntax structure. | |
* | |
* @author Eray Erdin | |
* @see com.erayerdin.linglib.syntax.generative.Category | |
* @see com.erayerdin.linglib.syntax.generative.Parameter | |
*/ | |
public class Phrase implements java.io.Serializable { | |
private Category category; | |
private Phrase complement = null; | |
private ArrayList<Phrase> adjunct = new ArrayList<Phrase>(); | |
private Phrase specifier = null; | |
public String value = null; | |
public Phrase(Category category) { | |
this.category = category; | |
} | |
public Phrase(Category category, String value) { | |
this.category = category; | |
this.value = value; | |
} | |
// public Phrase(Category category, Phrase complement) { | |
// this.category = category; | |
// this.complement = complement; | |
// this.adjunct = null; | |
// this.specifier = null; | |
// } | |
public String toString() { | |
return String.format("Phrase: %s", this.value); | |
} | |
/** | |
* Gets complement object. | |
* | |
* @return Returns complement Phrase. | |
*/ | |
public Phrase getComplement() { | |
return this.complement; | |
} | |
/** | |
* Changes complement with given object. | |
* | |
* @param complement A Phrase object. | |
*/ | |
public void extendComplement(Phrase complement) { | |
this.complement = complement; | |
} | |
/** | |
* Gets specifier object. | |
* | |
* @return Returns specifier Phrase. | |
*/ | |
public Phrase getSpecifier() { | |
return this.specifier; | |
} | |
/** | |
* Changes specifier with given object. | |
* | |
* @param specifier A Phrase object. | |
*/ | |
public void extendSpecifier(Phrase specifier) { | |
this.specifier = specifier; | |
} | |
/** | |
* Gets an adjunct with given index. | |
* | |
* @param index Index of adjunct. | |
* @return A Phrase object. | |
*/ | |
public Phrase getAdjunct(int index) { | |
return this.adjunct.get(index); | |
} | |
/** | |
* Adds adjunct Phrase. | |
* | |
* @param adjunct A Phrase object. | |
*/ | |
public void addAdjunct(Phrase adjunct) { | |
this.adjunct.add(adjunct); | |
} | |
/** | |
* Adds adjunct Phrase to given index. | |
* | |
* @param index Index to be added. | |
* @param adjunct A Phrase object. | |
*/ | |
public void addAdjunct(int index, Phrase adjunct) { | |
this.adjunct.add(index,adjunct); | |
} | |
/** | |
* Removes adjunct in given index. | |
* | |
* @param index Index to be removed. | |
*/ | |
public void removeAdjunct(int index) { | |
this.adjunct.remove(index); | |
} | |
/** | |
* Size of adjuncts. | |
* | |
* @return The size of adjuncts. | |
*/ | |
public int sizeAdjunct() { | |
return this.adjunct.size(); | |
} | |
/** | |
* Gets all adjuncts in ArrayList. | |
* | |
* @return ArrayList of Phrase objects. | |
*/ | |
public ArrayList<Phrase> adjuncts() { | |
return this.adjunct; | |
} | |
/** | |
* Joins an Array of String objects with space between. | |
* | |
* @param stringArray An Array of String objects. | |
* @return Joined string of string array. | |
*/ | |
public static String mergeStringArray(String[] stringArray) { | |
ArrayList<String> newStringArrayList = new ArrayList<String>(); | |
for (String s : stringArray) { | |
if (s != null) | |
newStringArrayList.add(s); | |
} | |
String newString = String.join(" ", newStringArrayList); | |
return newString; | |
} | |
/** | |
* Joins an ArrayList of String objects with space between. | |
* | |
* @param stringArray An ArrayList of String objects. | |
* @return Joined string of string ArrayList. | |
*/ | |
public static String mergeStringArray(ArrayList<String> stringArrayList) { | |
ArrayList<String> newStringArrayList = new ArrayList<String>(); | |
for (String s : stringArrayList) { | |
if (s != null) | |
newStringArrayList.add(s); | |
} | |
String newString = String.join(" ", newStringArrayList); | |
return newString; | |
} | |
/** | |
* Renders this Phrase object with all complement, adjunct and specifier objects. | |
* | |
* @param param A Parameter object. | |
* @return A merged string. | |
*/ | |
public String render(Parameter param) { | |
ArrayList<String> renderArrayList = new ArrayList<String>(); | |
renderArrayList.add(this.value); | |
// Add complement | |
if (this.complement != null) { | |
if (param.complement == false) | |
renderArrayList.add(0, this.complement.render(param)); | |
else | |
renderArrayList.add(this.complement.render(param)); | |
} | |
// Add adjuncts | |
if (this.adjunct.size() != 0) { | |
// Render all adjuncts | |
ArrayList<String> renderAdjunctArrayList = new ArrayList<String>(); | |
for (Phrase a : this.adjunct) { | |
renderAdjunctArrayList.add( | |
a.render(param) | |
); | |
} | |
String renderAdjunctString = Phrase.mergeStringArray(renderAdjunctArrayList); | |
if (param.adjunct == false) | |
renderArrayList.add(0, renderAdjunctString); | |
else | |
renderArrayList.add(renderAdjunctString); | |
} | |
// Add specifier | |
if (this.specifier != null) { | |
if (param.specifier == false) | |
renderArrayList.add(0, this.specifier.render(param)); | |
else | |
renderArrayList.add(this.specifier.render(param)); | |
} | |
String mergeAll = Phrase.mergeStringArray(renderArrayList); | |
return mergeAll; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment