class TestIoread(unittest.TestCase): logger = logging.getLogger("Asrt.TestIoread") testFile = scriptsDir + "/resources/ioread_utf8.txt" testFileCSV = scriptsDir + "/resources/ioread_utf8.csv" testsString = [ """Utf-8 test\nLatin characters é à ä\nNon latin characters 镕\n""", """Non latin characters 镕""" ] testList = [[ 'Utf-8 test', 'Latin characters é à ä', 'Non latin characters 镕' ]] def setUp(self): self.ioread = Ioread() ############ # Tests # def testOpenFile(self): try: fd = self.ioread.openFile(self.testFile) self.ioread.closeFile(fd) except Exception: self.fail("testOpenFile raised ExceptionType unexpectedled") def testReadFileContent(self): strContent = self.ioread.readFileContent(self.testFile) self.assertEquals(self.testsString[0], strContent) def testReadFileContentList(self): strContentList = self.ioread.readFileContentList(self.testFile) self.assertEquals(3, len(strContentList)) self.assertEquals(self.testsString[1], strContentList[2]) def testReadCSV(self): strContentList = self.ioread.readCSV(self.testFileCSV) self.assertEquals(1, len(strContentList)) self.assertEquals(strContentList, self.testList) def testWriteFileContent(self): strContent = self.testsString[0] self.ioread.writeFileContent(TEMPDIRUNITTEST + "/test.txt", strContent) readStrContent = self.ioread.readFileContent(self.testFile) self.assertEquals(strContent, readStrContent)
def text2text(sourcePath, destinationPath, logDir): """Make a copy of 'destinationPath'. """ TextRepresentation.logger.info("Copying txt file: " + sourcePath + " into text.") io = Ioread() strContent = io.readFileContent(sourcePath) #Write utf8 io.writeFileContent(destinationPath, strContent)
def countPresentFile(self, input_file): """Count the presence of all the punctuation in the punctuation model present in the file named [input_file] input_file Input file to be used as source return dictionary containing key and count """ try: io = Ioread() file_content = io.readFileContent(input_file) return self.countPresenceText(file_content) except Exception as e: print(("exception<" + input_file + "> : " + str(e))) return {}
def countPresentFile(self, input_file): """Count the presence of all the punctuation in the punctuation model present in the file named [input_file] input_file Input file to be used as source return dictionary containing key and count """ try: io = Ioread() file_content = io.readFileContent(input_file) return self.countPresenceText(file_content) except Exception, e : print("exception<" + input_file + "> : " + str(e), file=sys.stderr) return {}
def replaceFile(self, input_file, output_file=None): """Replace the punctuation in the file named [input_file] and return the result in the file named [output_file] input_file Input file to be used as source output_file Output file (if None return Text return True if success False is failure in case output_file is present output_text if not present """ try: io = Ioread() input_text = io.readFileContent(input_file) if output_file: f = open(output_file, "w") f.write(self.replaceText(input_text)) return True else: return self.replaceText(input_text) except Exception as e: print(("exception<" + input_file + "> : " + str(e))) if output_file: return False else: return None
def replaceFile(self, input_file, output_file = None): """Replace the punctuation in the file named [input_file] and return the result in the file named [output_file] input_file Input file to be used as source output_file Output file (if None return Text return True if success False is failure in case output_file is present output_text if not present """ try: io = Ioread() input_text = io.readFileContent(input_file) if output_file : f = open(output_file, "w") f.write(self.replaceText(input_text)) return True else: return self.replaceText(input_text) except Exception, e : print("exception<" + input_file + "> : " + str(e), file=sys.stderr) if output_file : return False else : return None