~cpp
# EazyParser.py
# coding: euc-kr
import hangul
import string
u = lambda x: unicode(x, "euc-kr")
class EazyParser:
def updateDictionary(self, aDict):
self.dic = aDict
def findFundermentalForm(self, aWord):
johabWord = hangul.disjoint(u(aWord))
roots = self.dic.getRoots()
for each in roots:
if hangul.disjoint(u(each)) in johabWord:
return each + '다'
def findVerb(self, aSentence):
lastToken = aSentence.split()[-1]
return self.findFundermentalForm(
self.removeMark(lastToken) )
def removeMark(self, aPhrase):
for mark in string.punctuation:
if mark in aPhrase:
aPhrase = aPhrase.replace(mark,'')
return aPhrase
def findVP(self, aSentence):
NP = self.findNP(aSentence)
temp = aSentence.split(' ')
temp.remove(NP)
return string.join( temp, ' ')
def findNP( self, aSentence ):
return aSentence.split(' ')[0]
~cpp
# EazyParserTest.py
# coding: euc-kr
import unittest
from EazyWord import EazyWord
from EazyDic import EazyDic
from EazyParser import EazyParser
class EazyParserTestCase(unittest.TestCase):
def setUp(self):
self.parser = EazyParser()
self.dic = EazyDic()
self.muk = EazyWord()
self.muk.setRoot('먹')
self.muk.addCase('AOV')
self.dic.addWord(self.muk)
self.parser.updateDictionary( self.dic )
self.ga = EazyWord()
self.ga.setRoot('가')
self.ga.addCase('ALV')
def testFindFundermentalForm(self):
self.assertEquals( '먹다', self.parser.findFundermentalForm('먹는다') )
self.dic.addWord(self.ga)
self.parser.updateDictionary( self.dic )
self.assertEquals( '가다', self.parser.findFundermentalForm('간다') )
def testFindVerb(self):
self.assertEquals( '먹다', self.parser.findVerb('나는 밥을 먹는다.') )
self.assertEquals( None, self.parser.findVerb('나는 학교에 간다.') )
def testFindVP(self):
self.assertEquals( '밥을 먹는다.', self.parser.findVP('나는 밥을 먹는다.') )
def testFindNP(self):
self.assertEquals( '밥을', self.parser.findNP( '밥을 내가 먹는다.' ) )
if __name__ == '__main__':
unittest.main()
~cpp
# AllTest.py
from EazyParserTest import *
from EazyWordTest import *
from EazyDicTest import *
import unittest
if __name__=='__main__':
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(EazyParserTestCase, 'test'))
suite.addTest(unittest.makeSuite(EazyWordTestCase, 'test'))
suite.addTest(unittest.makeSuite(EazyDicTestCase, 'test'))
runner = unittest.TextTestRunner()
runner.run (suite)