StringAutoBackTest.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // CharacterAutoTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/22.
  6. //
  7. import Foundation
  8. import Cocoa
  9. class StringAutoBackTest : StringAutoTest {
  10. override func needTest() -> Bool {
  11. return self.selectedKeys().count > 0
  12. }
  13. override class func shared() -> AutoTest? {
  14. return StringAutoBackTest()
  15. }
  16. // // Auto Test refrence Check File
  17. // override func autoTest(_ complention:@escaping (_ object:AutoTest, _ report:NSAttributedString?) -> ()) {
  18. // self.compareFinishedFiles.removeAllObjects();
  19. // self.convertFiles.removeAllObjects()
  20. //
  21. // clearCacheFiles()
  22. //
  23. // let needCheckString = self.selectedKeys().contains("字符")
  24. //
  25. // if !needCheckString {
  26. // _status = .Finished
  27. // complention(self, self.reportString)
  28. // return
  29. // }
  30. //
  31. // _status = .Process
  32. // reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】字符比对开始!\n",
  33. // attributes:[.foregroundColor : NSColor.blue])
  34. // let files = DataModel.shared.originFilesFor(_fileType, type: _type)
  35. // self.testFiles = NSArray(array: files);
  36. //
  37. // let checkDirectory = self.checkFileDirectory()
  38. // let originDirectory = self.originFileDirectory()
  39. // let resultDirectory = self.resultFileDirectory()
  40. //
  41. // if (files.count > 0) {
  42. // try? FileManager.default.createDirectory(atPath: checkDirectory, withIntermediateDirectories: true);
  43. // try? FileManager.default.createDirectory(atPath: resultDirectory, withIntermediateDirectories: true);
  44. // }
  45. //
  46. // var tDegree = Double(0);
  47. // var tCount = Int(0)
  48. // var fileIndex = 0;
  49. //
  50. // var convertFileBlock = { (files:[String]) in }
  51. // convertFileBlock = { (files:[String]) in
  52. // if (fileIndex >= files.count) {
  53. // TestDegreeManager.shared().set(((tCount != 0) ? tDegree/Double(tCount) : 0.0),
  54. // fileType: self.fileType(),
  55. // type: self.type())
  56. //
  57. // self._status = .Finished
  58. // DispatchQueue.main.async {
  59. // autoreleasepool {
  60. // complention(self, self.reportString);
  61. // }
  62. // }
  63. // return
  64. // }
  65. //
  66. // let fileName = files[fileIndex]
  67. // let fName = NSString(string: fileName).deletingPathExtension
  68. // let originPath = NSString(string: originDirectory).appendingPathComponent(fName+".pdf")
  69. // let resultPath = NSString(string: resultDirectory).appendingPathComponent(fName+"."+self.extention())
  70. // let checkPath = NSString(string: checkDirectory).appendingPathComponent(fName+"."+self.extention())
  71. //
  72. //
  73. // self.reportString?.append(NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】开始转换文件 \"\(fName)\"\n",
  74. // attributes:[.foregroundColor : NSColor.black]))
  75. // // ...
  76. // // 执行转换过程
  77. // let index = self.testFiles.index(of: fileName);
  78. // if (index != NSNotFound) {
  79. // self.convertProgress = Double(index) / Double(self.testFiles.count)
  80. // }
  81. // self.convertFiles.add(fileName);
  82. // self.testlog("开始转换:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  83. // self.process(originPath, resultPath: resultPath) { status in
  84. // if FileManager.default.fileExists(atPath: resultPath) && status == 1 {
  85. // if needCheckString && FileManager.default.fileExists(atPath: checkPath) {
  86. // DispatchQueue.global().async {
  87. // let checkString = self.readTextFile(checkPath as NSString)
  88. // let resultStr = self.readTextFile(resultPath as NSString)
  89. //
  90. // if (checkString != nil && resultStr != nil) {
  91. // let maxSize = checkString!.count
  92. // let report = NSMutableAttributedString(string: "")
  93. // let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { appAttr in
  94. // report.append(appAttr)
  95. // }
  96. // // let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { skipString, failString in
  97. // // self.appendErrorInfo(skipString, failString: failString)
  98. // // }
  99. //
  100. // var color = NSColor.black
  101. // if fabs(degree-100.0) >= 0.01 {
  102. // color = NSColor.red
  103. // }
  104. // tDegree += degree;
  105. // tCount += 1
  106. //
  107. // TestDegreeManager.shared().set(degree,
  108. // fileType: self.fileType(),
  109. // type: self.type(),
  110. // fileName: fileName)
  111. //
  112. // let successCount = Int(maxSize * Int(degree)/100)
  113. // report.append(NSAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"比对完成,准确率\(degree)%(\(successCount)/\(maxSize))\n",
  114. // attributes:[.foregroundColor : color]))
  115. // if (report != nil) {
  116. // do {
  117. // let rtfData = try? report.data(from: .init(location: 0, length: report.length),
  118. // documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
  119. //
  120. // let path = NSString(string: resultPath).appendingPathExtension("rtf")
  121. // try? FileManager.default.removeItem(atPath: path!);
  122. // try? rtfData?.write(to: NSURL.fileURL(withPath: path!))
  123. // } catch {
  124. // }
  125. // self.reportString?.append(report)
  126. // }
  127. // }
  128. //
  129. // let index = self.testFiles.index(of: fileName);
  130. // if (index != NSNotFound) {
  131. // self.compareProgress = Double(index) / Double(self.testFiles.count)
  132. // }
  133. // self.compareFinishedFiles.add(fileName);
  134. // self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  135. //
  136. // fileIndex += 1
  137. // convertFileBlock(files);
  138. // }
  139. // }else {
  140. // self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】对照文件 \"\(fName)\"不存在!\n",
  141. // attributes:[.foregroundColor : NSColor.red]))
  142. //
  143. // let index = self.testFiles.index(of: fileName);
  144. // if (index != NSNotFound) {
  145. // self.compareProgress = Double(index) / Double(self.testFiles.count)
  146. // }
  147. // self.compareFinishedFiles.add(fileName);
  148. // self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  149. //
  150. // fileIndex += 1
  151. // convertFileBlock(files);
  152. // }
  153. // }else {
  154. // if (status == 0) {
  155. // self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档失败!\n",
  156. // attributes:[.foregroundColor : NSColor.red]))
  157. // }else if (status == -1 || status == -2) {
  158. // self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"文档无法打开!\n",
  159. // attributes:[.foregroundColor : NSColor.red]))
  160. // }else if (status == -3) {
  161. // self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
  162. // attributes:[.foregroundColor : NSColor.red]))
  163. // }
  164. //
  165. // let index = self.testFiles.index(of: fileName);
  166. // if (index != NSNotFound) {
  167. // self.compareProgress = Double(index) / Double(self.testFiles.count)
  168. // }
  169. // self.compareFinishedFiles.add(fileName);
  170. // self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
  171. // fileIndex += 1
  172. // convertFileBlock(files);
  173. // }
  174. // }
  175. // }
  176. // convertFileBlock(files);
  177. // }
  178. //
  179. //
  180. /// Compare
  181. override func compareString(_ check:NSString, result:NSString, callback:@escaping (_ appAttr:NSAttributedString)->()) -> Double {
  182. // func compareString(_ check:NSString, result:NSString, failure:@escaping (_ skipString:NSString, _ failString:NSString)->()) -> Double {
  183. return autoreleasepool {
  184. var checkString = check
  185. var resultStr = result
  186. var maxSize = checkString.length
  187. var successCount = 0;
  188. resultStr = resultStr.replacingOccurrences(of: "\n", with: "") as NSString
  189. resultStr = resultStr.replacingOccurrences(of: " ", with: "") as NSString
  190. /**
  191. (A0 = B0)
  192. - A-1 & B-1
  193. (A0 != B0) & (A0 in B) & (B0 in A)
  194. - 取 A0,B0最小 Range 值
  195. - 字符串裁剪对齐
  196. (A0 != B0) & (A0 in B)
  197. - 存储B0到识别错误缓存
  198. (A0 != B0) & (B0 in A)
  199. - 存储 A0到识别遗漏字符串
  200. (A0 != B0)
  201. - 分别存储 A0、B0到遗漏及错误字串
  202. */
  203. var skipString = NSString()
  204. var failString = NSString()
  205. while (checkString.length > 0 && resultStr.length > 0) {
  206. let subc = checkString.substring(to: 1) as NSString
  207. let subr = resultStr.substring(to: 1) as NSString
  208. let cRange = checkString.range(of: subr as String)
  209. let rRange = resultStr.range(of: subc as String)
  210. if subc.isEqual(to: subr) { // (A0 = B0)
  211. // Check Success
  212. callback(attributeStringWith(skipString, failString: failString))
  213. // failure(skipString, failString)
  214. // self.appendErrorInfo(skipString, failString: failString)
  215. skipString = NSString()
  216. failString = NSString()
  217. checkString = checkString.substring(from:1) as NSString
  218. resultStr = resultStr.substring(from:1) as NSString
  219. successCount = successCount + 1
  220. callback(NSAttributedString(string: subc as String))
  221. }else if (["\n", "\r", " "].contains(subc)) {
  222. callback(NSAttributedString(string: subc as String))
  223. maxSize -= 1;
  224. checkString = checkString.substring(from:1) as NSString
  225. } else if (cRange.location != NSNotFound &&
  226. rRange.location != NSNotFound) {
  227. if (min(cRange.location, rRange.location) >= 2) {
  228. let nextc = checkString.substring(with: NSRange(location: 1, length: 1)) as NSString
  229. let nextr = resultStr.substring(with: NSRange(location: 1, length: 1)) as NSString
  230. let ncRange = checkString.range(of: nextr as String)
  231. let nrRange = resultStr.range(of: nextc as String)
  232. if (min(nrRange.location, ncRange.location) < min(cRange.location, rRange.location)) {
  233. if (ncRange.location < nrRange.location) {
  234. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  235. skipString = skipString.appending(subc as String) as NSString
  236. checkString = checkString.substring(from:1) as NSString
  237. }else {
  238. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  239. failString = failString.appending(subr as String) as NSString
  240. resultStr = resultStr.substring(from:1) as NSString
  241. }
  242. }else if (cRange.location < rRange.location) {
  243. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  244. skipString = skipString.appending(subc as String) as NSString
  245. checkString = checkString.substring(from:1) as NSString
  246. }else {
  247. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  248. failString = failString.appending(subr as String) as NSString
  249. resultStr = resultStr.substring(from:1) as NSString
  250. }
  251. }else {
  252. // var scale = (skipString.length > 0) ? (Float(checkString.length) / Float(skipString.length)) : Float(1.0)
  253. // if (checkString.length > skipString.length && cRange.location <= Int(Float(rRange.location)*scale)) {
  254. // //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  255. // skipString = skipString.appending(subc as String) as NSString
  256. // checkString = checkString.substring(from:1) as NSString
  257. // }else if (checkString.length <= skipString.length && cRange.location > Int(Float(rRange.location)*scale)) {
  258. // failString = failString.appending(subr as String) as NSString
  259. // resultStr = resultStr.substring(from:1) as NSString
  260. // }else
  261. if (cRange.location < rRange.location) {
  262. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  263. skipString = skipString.appending(subc as String) as NSString
  264. checkString = checkString.substring(from:1) as NSString
  265. }else {
  266. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  267. failString = failString.appending(subr as String) as NSString
  268. resultStr = resultStr.substring(from:1) as NSString
  269. }
  270. }
  271. }else if (cRange.location != NSNotFound) {
  272. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  273. skipString = skipString.appending(subc as String) as NSString
  274. checkString = checkString.substring(from:1) as NSString
  275. }else if (rRange.location != NSNotFound) {
  276. //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
  277. failString = failString.appending(subr as String) as NSString
  278. resultStr = resultStr.substring(from:1) as NSString
  279. }else {
  280. // 两个子字串均未找到
  281. skipString = skipString.appending(subc as String) as NSString
  282. failString = failString.appending(subr as String) as NSString
  283. checkString = checkString.substring(from:1) as NSString
  284. resultStr = resultStr.substring(from:1) as NSString
  285. }
  286. }
  287. skipString = skipString.appending(checkString as String) as NSString
  288. failString = failString.appending(resultStr as String) as NSString
  289. // failure(skipString, failString)
  290. callback(attributeStringWith(skipString, failString: failString))
  291. callback(NSAttributedString(string: "\n"))
  292. let degree = (maxSize>1) ? Double(Float(successCount)/Float(maxSize) * 100) : 0
  293. return degree
  294. }
  295. }
  296. }