StringAutoCompareTest.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // CharacterAutoTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/22.
  6. //
  7. import Foundation
  8. import Cocoa
  9. class StringAutoCompareTest : StringAutoTest {
  10. override class func shared() -> AutoTest? {
  11. return StringAutoCompareTest()
  12. }
  13. // Auto Test refrence Check File
  14. override func autoTest(_ complention:@escaping (_ object:AutoTest, _ report:NSAttributedString?) -> ()) {
  15. self.compareFinishedFiles.removeAllObjects();
  16. self.convertFiles.removeAllObjects()
  17. // clearCacheFiles()
  18. let needCheckString = self.selectedKeys().contains("字符")
  19. if !needCheckString {
  20. _status = .Finished
  21. complention(self, self.reportString)
  22. return
  23. }
  24. _status = .Process
  25. reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】字符比对开始!\n",
  26. attributes:[.foregroundColor : NSColor.blue])
  27. let files = DataModel.shared.originFilesFor(_fileType, type: _type)
  28. self.testFiles = NSArray(array: files);
  29. let checkDirectory = self.checkFileDirectory()
  30. let originDirectory = self.originFileDirectory()
  31. let resultDirectory = self.resultFileDirectory()
  32. if (files.count > 0) {
  33. try? FileManager.default.createDirectory(atPath: checkDirectory, withIntermediateDirectories: true);
  34. try? FileManager.default.createDirectory(atPath: resultDirectory, withIntermediateDirectories: true);
  35. }
  36. var tDegree = Double(0);
  37. var tCount = Int(0)
  38. var fileIndex = 0;
  39. var convertFileBlock = { (files:[String]) in }
  40. convertFileBlock = { (files:[String]) in
  41. if (fileIndex >= files.count) {
  42. TestDegreeManager.shared().set(((tCount != 0) ? tDegree/Double(tCount) : 0.0),
  43. fileType: self.fileType(),
  44. type: self.type())
  45. self._status = .Finished
  46. DispatchQueue.main.async {
  47. autoreleasepool {
  48. complention(self, self.reportString);
  49. }
  50. }
  51. return
  52. }
  53. let fileName = files[fileIndex]
  54. let fName = NSString(string: fileName).deletingPathExtension
  55. let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  56. let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+self.extention())
  57. let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+self.extention())
  58. self.reportString?.append(NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】开始对照文件 \"\(fName)\"\n",
  59. attributes:[.foregroundColor : NSColor.black]))
  60. // ...
  61. // 执行转换过程
  62. let index = self.testFiles.index(of: fileName);
  63. if (index != NSNotFound) {
  64. self.convertProgress = Double(index) / Double(self.testFiles.count)
  65. }
  66. self.convertFiles.add(fileName);
  67. self.testlog("开始对照:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  68. // self.process(originPath, resultPath: resultPath) { status in
  69. let status = 1
  70. if FileManager.default.fileExists(atPath: resultPath) && status == 1 {
  71. if needCheckString && FileManager.default.fileExists(atPath: checkPath) {
  72. DispatchQueue.global().async {
  73. let checkString = self.readTextFile(checkPath as NSString)
  74. let resultStr = self.readTextFile(resultPath as NSString)
  75. if (checkString != nil && resultStr != nil) {
  76. let maxSize = checkString!.count
  77. let report = NSMutableAttributedString(string: "")
  78. let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { appAttr in
  79. report.append(appAttr)
  80. }
  81. // let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { skipString, failString in
  82. // self.appendErrorInfo(skipString, failString: failString)
  83. // }
  84. var color = NSColor.black
  85. if fabs(degree-100.0) >= 0.01 {
  86. color = NSColor.red
  87. }
  88. tDegree += degree;
  89. tCount += 1
  90. TestDegreeManager.shared().set(degree,
  91. fileType: self.fileType(),
  92. type: self.type(),
  93. fileName: fileName)
  94. let successCount = Int(maxSize * Int(degree)/100)
  95. report.append(NSAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"比对完成,准确率\(degree)%(\(successCount)/\(maxSize))\n",
  96. attributes:[.foregroundColor : color]))
  97. if (report != nil) {
  98. do {
  99. let rtfData = try? report.data(from: .init(location: 0, length: report.length),
  100. documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
  101. let path = NSString(string: resultPath).appendingPathExtension("rtf")
  102. try? FileManager.default.removeItem(atPath: path!);
  103. try? rtfData?.write(to: NSURL.fileURL(withPath: path!))
  104. } catch {
  105. }
  106. self.reportString?.append(report)
  107. }
  108. }
  109. let index = self.testFiles.index(of: fileName);
  110. if (index != NSNotFound) {
  111. self.compareProgress = Double(index) / Double(self.testFiles.count)
  112. }
  113. self.compareFinishedFiles.add(fileName);
  114. self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  115. fileIndex += 1
  116. convertFileBlock(files);
  117. }
  118. }else {
  119. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】对照文件 \"\(fName)\"不存在!\n",
  120. attributes:[.foregroundColor : NSColor.red]))
  121. let index = self.testFiles.index(of: fileName);
  122. if (index != NSNotFound) {
  123. self.compareProgress = Double(index) / Double(self.testFiles.count)
  124. }
  125. self.compareFinishedFiles.add(fileName);
  126. self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  127. fileIndex += 1
  128. convertFileBlock(files);
  129. }
  130. }else {
  131. if (status == 0) {
  132. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n",
  133. attributes:[.foregroundColor : NSColor.red]))
  134. }else if (status == -1 || status == -2) {
  135. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"文档无法打开!\n",
  136. attributes:[.foregroundColor : NSColor.red]))
  137. }else if (status == -3) {
  138. self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
  139. attributes:[.foregroundColor : NSColor.red]))
  140. }
  141. let index = self.testFiles.index(of: fileName);
  142. if (index != NSNotFound) {
  143. self.compareProgress = Double(index) / Double(self.testFiles.count)
  144. }
  145. self.compareFinishedFiles.add(fileName);
  146. self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  147. fileIndex += 1
  148. convertFileBlock(files);
  149. }
  150. // }
  151. }
  152. convertFileBlock(files);
  153. }
  154. }
  155. // Dispared
  156. class StringAutoCompare : StringAutoCompareTest {
  157. override class func shared() -> AutoTest? {
  158. return StringAutoCompareTest()
  159. }
  160. }