def __init__(self, parse=None): """ Constructor of MorphologicalParse class which takes a String parse as an input. First it creates a list as iGs for inflectional groups, and while given String contains derivational boundary (^DB+), it adds the substring to the iGs list and continue to use given String from 4th index. If it does not contain ^DB+, it directly adds the given String to the iGs list. Then, it creates a new list as inflectionalGroups and checks for some cases. If the first item of iGs list is ++Punc, it creates a new root as +, and by calling InflectionalGroup method with Punc it initializes the IG list by parsing given input String IG by + and calling the getMorphologicalTag method with these substrings. If getMorphologicalTag method returns a tag, it adds this tag to the IG list and also to the inflectionalGroups list. If the first item of iGs list has +, it creates a new word of first item's substring from index 0 to +, and assigns it to root. Then, by calling InflectionalGroup method with substring from index 0 to +, it initializes the IG list by parsing given input String IG by + and calling the getMorphologicalTag method with these substrings. If getMorphologicalTag method returns a tag, it adds this tag to the IG list and also to the inflectionalGroups list. If the first item of iGs list does not contain +, it creates a new word with first item and assigns it as root. At the end, it loops through the items of iGs and by calling InflectionalGroup method with these items it initializes the IG list by parsing given input String IG by + and calling the getMorphologicalTag method with these substrings. If getMorphologicalTag method returns a tag, it adds this tag to the IG list and also to the inflectionalGroups list. PARAMETERS ---------- parse : str String input. """ if parse is not None: if isinstance(parse, str): iGs = [] st = parse while "^DB+" in st: iGs.append(st[:st.index("^DB+")]) st = st[st.index("^DB+") + 4:] iGs.append(st) self.inflectionalGroups = [] if iGs[0] == "++Punc": self.root = Word("+") self.inflectionalGroups.append(InflectionalGroup("Punc")) else: if iGs[0].index("+") != -1: self.root = Word(iGs[0][:iGs[0].index("+")]) self.inflectionalGroups.append( InflectionalGroup(iGs[0][iGs[0].index("+") + 1:])) else: self.root = Word(iGs[0]) for i in range(1, len(iGs)): self.inflectionalGroups.append( InflectionalGroup(iGs[i])) elif isinstance(parse, list): self.inflectionalGroups = [] if parse[0].index("+") != -1: self.root = Word(parse[0][:parse[0].index("+")]) self.inflectionalGroups.append( InflectionalGroup(parse[0][parse[0].index("+") + 1:])) for i in range(1, len(parse)): self.inflectionalGroups.append(InflectionalGroup(parse[i]))
def __init__(self, parse:None): if parse is not None: if isinstance(parse, str): iGs = [] st = parse while "^DB+" in st: iGs.append(st[:st.index("^DB+")]) st = st[st.index("^DB+") + 4:] iGs.append(st) self.inflectionalGroups = [] if iGs[0] == "++Punc": self.root = Word("+") self.inflectionalGroups.append(InflectionalGroup("Punc")) else: if iGs[0].index("+") != -1: self.root = Word(iGs[0][:iGs[0].index("+")]) self.inflectionalGroups.append(InflectionalGroup(iGs[0][iGs[0].index("+") + 1:])) else: self.root = Word(iGs[0]) for i in range(1, len(iGs)): self.inflectionalGroups.append(InflectionalGroup(iGs[i])) elif isinstance(parse, list): self.inflectionalGroups = [] if parse[0].index("+") != -1: self.root = Word(parse[0][:parse[0].index("+")]) self.inflectionalGroups.append(InflectionalGroup(parse[0][parse[0].index("+") + 1:])) for i in range(1, len(parse)): self.inflectionalGroups.append(InflectionalGroup(parse[i]))
def test_getMorphologicalTag(self): self.assertEqual(InflectionalGroup.getMorphologicalTag("noun"), MorphologicalTag.NOUN) self.assertEqual(InflectionalGroup.getMorphologicalTag("without"), MorphologicalTag.WITHOUT) self.assertEqual(InflectionalGroup.getMorphologicalTag("interj"), MorphologicalTag.INTERJECTION) self.assertEqual(InflectionalGroup.getMorphologicalTag("inf2"), MorphologicalTag.INFINITIVE2)
def constructInflectionalGroups(self): parse = self.transitionList() iGs = [] while "^DB+" in parse: iGs.append(parse[:parse.index("^DB+")]) parse = parse[parse.index("^DB+") + 4:] iGs.append(parse) self.inflectionalGroups = [] self.inflectionalGroups.append( InflectionalGroup(iGs[0][iGs[0].index("+") + 1:])) for i in range(1, len(iGs)): self.inflectionalGroups.append(InflectionalGroup(iGs[i]))
def getTag(self, index: int) -> str: """ The getTag method takes an integer index as an input and and if the given index is 0, it directly return the root. Then, it loops through the inflectionalGroups list it returns the MorphologicalTag of the corresponding inflectional group. PARAMETERS ---------- index : int Integer input. RETURNS ------- str The MorphologicalTag of the corresponding inflectional group, or null of invalid index inputs. """ size = 1 if index == 0: return self.root.getName() for group in self.inflectionalGroups: if index < size + group.size(): return InflectionalGroup.getTagString( group.getTag(index - size)) size += group.size() return None
def getTag(self, index: int) -> str: size = 1 if index == 0: return self.root.getName() for group in self.inflectionalGroups: if index < size + group.size(): return InflectionalGroup.getTagString(group.getTag(index - size)) size += group.size() return None
def getWordWithPos(self) -> Word: """ The getWordWithPos method returns root with the MorphologicalTag of the first inflectional as a new word. RETURNS ------- Word Root with the MorphologicalTag of the first inflectional as a new word. """ return Word(self.root.getName() + "+" + InflectionalGroup.getTagString(self.firstInflectionalGroup().getTag(0)))
def getRootPos(self) -> str: """ The getRootPos method returns the MorphologicalTag of the first inflectional group. RETURNS ------- str The MorphologicalTag of the first inflectional group. """ return InflectionalGroup.getTagString(self.firstInflectionalGroup().getTag(0))
def constructInflectionalGroups(self): """ The constructInflectionalGroups method initially calls the transitionList method and assigns the resulting str to the parse variable and creates a new list as iGs. If parse str contains a derivational boundary it adds the substring starting from the 0 to the index of derivational boundary to the iGs. If it does not contain a DB, it directly adds parse to the iGs. Then, creates and initializes new list as inflectionalGroups and fills with the items of iGs. """ parse = self.transitionList() iGs = [] while "^DB+" in parse: iGs.append(parse[:parse.index("^DB+")]) parse = parse[parse.index("^DB+") + 4:] iGs.append(parse) self.inflectionalGroups = [] self.inflectionalGroups.append( InflectionalGroup(iGs[0][iGs[0].index("+") + 1:])) for i in range(1, len(iGs)): self.inflectionalGroups.append(InflectionalGroup(iGs[i]))
def lastIGContainsCase(self) -> str: """ The lastIGContainsCase method returns the MorphologicalTag of last inflectional group if it is one of the NOMINATIVE, ACCUSATIVE, DATIVE, LOCATIVE or ABLATIVE cases, null otherwise. RETURNS ------- str The MorphologicalTag of last inflectional group if it is one of the NOMINATIVE, ACCUSATIVE, DATIVE, LOCATIVE or ABLATIVE cases, null otherwise. """ caseTag = self.lastInflectionalGroup().containsCase() if caseTag is not None: return InflectionalGroup.getTagString(caseTag) else: return "NULL"
def getRootPos(self) -> str: return InflectionalGroup.getTagString(self.firstInflectionalGroup().getTag(0))
def test_containsPlural(self): inflectionalGroup1 = InflectionalGroup("VERB+NEG+NECES+A1PL") self.assertTrue(inflectionalGroup1.containsPlural()) inflectionalGroup2 = InflectionalGroup("PRON+PERS+A2PL+PNON+NOM") self.assertTrue(inflectionalGroup2.containsPlural()) inflectionalGroup3 = InflectionalGroup("NOUN+DIM+A3PL+P2SG+GEN") self.assertTrue(inflectionalGroup3.containsPlural()) inflectionalGroup4 = InflectionalGroup("NOUN+A3PL+P1PL+GEN") self.assertTrue(inflectionalGroup4.containsPlural()) inflectionalGroup5 = InflectionalGroup("NOUN+ZERO+A3SG+P2PL+INS") self.assertTrue(inflectionalGroup5.containsPlural()) inflectionalGroup6 = InflectionalGroup("PRON+QUANTP+A3PL+P3PL+LOC") self.assertTrue(inflectionalGroup6.containsPlural())
def test_containsCase(self): inflectionalGroup1 = InflectionalGroup("NOUN+ACTOF+A3PL+P1PL+NOM") self.assertIsNotNone(inflectionalGroup1.containsCase()) inflectionalGroup2 = InflectionalGroup("NOUN+A3PL+P1PL+ACC") self.assertIsNotNone(inflectionalGroup2.containsCase()) inflectionalGroup3 = InflectionalGroup("NOUN+ZERO+A3SG+P3PL+DAT") self.assertIsNotNone(inflectionalGroup3.containsCase()) inflectionalGroup4 = InflectionalGroup("PRON+QUANTP+A1PL+P1PL+LOC") self.assertIsNotNone(inflectionalGroup4.containsCase()) inflectionalGroup5 = InflectionalGroup("NOUN+AGT+A3SG+P2SG+ABL") self.assertIsNotNone(inflectionalGroup5.containsCase())
def test_containsTag(self): inflectionalGroup1 = InflectionalGroup("NOUN+ZERO+A3SG+P1SG+NOM") self.assertTrue(inflectionalGroup1.containsTag(MorphologicalTag.NOUN)) inflectionalGroup2 = InflectionalGroup("NOUN+AGT+A3PL+P2SG+ABL") self.assertTrue(inflectionalGroup2.containsTag(MorphologicalTag.AGENT)) inflectionalGroup3 = InflectionalGroup("NOUN+INF2+A3PL+P3SG+NOM") self.assertTrue( inflectionalGroup3.containsTag(MorphologicalTag.NOMINATIVE)) inflectionalGroup4 = InflectionalGroup("NOUN+ZERO+A3SG+P1PL+ACC") self.assertTrue(inflectionalGroup4.containsTag(MorphologicalTag.ZERO)) inflectionalGroup5 = InflectionalGroup("NOUN+ZERO+A3SG+P2PL+INS") self.assertTrue(inflectionalGroup5.containsTag(MorphologicalTag.P2PL)) inflectionalGroup6 = InflectionalGroup("PRON+QUANTP+A3PL+P3PL+LOC") self.assertTrue( inflectionalGroup6.containsTag( MorphologicalTag.QUANTITATIVEPRONOUN))
def test_containsPossessive(self): inflectionalGroup1 = InflectionalGroup("NOUN+ZERO+A3SG+P1SG+NOM") self.assertTrue(inflectionalGroup1.containsPossessive()) inflectionalGroup2 = InflectionalGroup("NOUN+AGT+A3PL+P2SG+ABL") self.assertTrue(inflectionalGroup2.containsPossessive()) inflectionalGroup3 = InflectionalGroup("NOUN+INF2+A3PL+P3SG+NOM") self.assertTrue(inflectionalGroup3.containsPossessive()) inflectionalGroup4 = InflectionalGroup("NOUN+ZERO+A3SG+P1PL+ACC") self.assertTrue(inflectionalGroup4.containsPossessive()) inflectionalGroup5 = InflectionalGroup("NOUN+ZERO+A3SG+P2PL+INS") self.assertTrue(inflectionalGroup5.containsPossessive()) inflectionalGroup6 = InflectionalGroup("PRON+QUANTP+A3PL+P3PL+LOC") self.assertTrue(inflectionalGroup6.containsPossessive())
def getWordWithPos(self) -> Word: return Word(self.root.getName() + "+" + InflectionalGroup.getTagString(self.firstInflectionalGroup().getTag(0)))
def test_size(self): inflectionalGroup1 = InflectionalGroup("ADJ") self.assertEqual(1, inflectionalGroup1.size()) inflectionalGroup2 = InflectionalGroup("ADJ+JUSTLIKE") self.assertEqual(2, inflectionalGroup2.size()) inflectionalGroup3 = InflectionalGroup("ADJ+FUTPART+P1PL") self.assertEqual(3, inflectionalGroup3.size()) inflectionalGroup4 = InflectionalGroup("NOUN+A3PL+P1PL+ABL") self.assertEqual(4, inflectionalGroup4.size()) inflectionalGroup5 = InflectionalGroup("ADJ+WITH+A3SG+P3SG+ABL") self.assertEqual(5, inflectionalGroup5.size()) inflectionalGroup6 = InflectionalGroup("VERB+ABLE+NEG+FUT+A3PL+COP") self.assertEqual(6, inflectionalGroup6.size()) inflectionalGroup7 = InflectionalGroup( "VERB+ABLE+NEG+AOR+A3SG+COND+A1SG") self.assertEqual(7, inflectionalGroup7.size())
def lastIGContainsCase(self) -> str: caseTag = self.lastInflectionalGroup().containsCase() if caseTag is not None: return InflectionalGroup.getTagString(caseTag) else: return "NULL"