ChineseStringAutoTest.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // ChineseStringAutoTest.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/22.
  6. //
  7. import Foundation
  8. import Cocoa
  9. class ChineseStringAutoTest : AutoTest {
  10. override func type() -> String {
  11. return "PDFConvert_China_Auto_Test"
  12. }
  13. override func name() -> String {
  14. return "中文字符转换准确率测试"
  15. }
  16. override func keys() -> NSArray {
  17. return ["字符对比", "前一版对比"]
  18. }
  19. static var cSharedInstance = ChineseStringAutoTest()
  20. override class func shared() -> AutoTest? {
  21. return cSharedInstance
  22. }
  23. // Auto Test refrence Check File
  24. override func autoTest() {
  25. _status = .Process
  26. let checkString = self.selectedKeys().contains("字符对比")
  27. let needCompare = self.selectedKeys().contains("前一版对比")
  28. if !needCompare && !checkString {
  29. return
  30. }
  31. // 暂时先用转RTF做为范例
  32. let checkPath = self.checkFilePath()
  33. let originPath = self.originFilePath()
  34. // ...
  35. // 执行转换过程
  36. if self.isResultFileExist() {
  37. if checkString {
  38. reportString = NSMutableAttributedString.init(string: "\n【\(self.name())】字符比对开始!\n",
  39. attributes:[.foregroundColor : NSColor.blue])
  40. // Load check file
  41. let checkData = NSData.init(contentsOfFile: checkPath) as! Data
  42. var documentAttributes:NSDictionary!
  43. let checkAttString = NSAttributedString.init(rtf: checkData, documentAttributes: &documentAttributes)
  44. var checkString = NSString(string: checkAttString!.string) as NSString
  45. let resultPath = self.resultFilePath()
  46. let resultString = try? NSString.init(contentsOfFile: resultPath, encoding: NSUTF8StringEncoding)
  47. #if true
  48. //识别字符串 \shptxt\shptxt ... }
  49. let pageInfoStrings = resultString!.components(separatedBy: "\\shptxt\\shptxt") as NSArray
  50. var finalString = ""
  51. if pageInfoStrings.count > 0 {
  52. let subStrings = pageInfoStrings.subarray(with: NSMakeRange(1, Int(pageInfoStrings.count - 1))) as! [String]
  53. for pageInfoString in subStrings {
  54. let endRange = NSString(string: pageInfoString).range(of: "}")
  55. finalString = finalString.appending(NSString(string: pageInfoString).substring(to: endRange.location))
  56. }
  57. }
  58. //识别所有 【空格 ~ \】 之间的值,并进行缝合
  59. // Detect all strings between Spaces and \ and stitch
  60. let strings = finalString.components(separatedBy: " ")
  61. var resultStr = "" as NSString
  62. for str in strings {
  63. let markStr = str as NSString
  64. if (markStr.contains("\\f")) {
  65. let fRange = markStr.range(of: "\\f")
  66. let cRange = markStr.range(of: "\\c")
  67. let bRange = markStr.range(of: "\\b")
  68. let iRange = markStr.range(of: "\\i")
  69. let eRange = markStr.range(of: "\\e")
  70. let pRange = markStr.range(of: "\\p")
  71. let minPos = min(Int(fRange.location),
  72. Int(cRange.location),
  73. Int(bRange.location),
  74. Int(iRange.location),
  75. Int(eRange.location),
  76. Int(pRange.location))
  77. resultStr = resultStr.appending(markStr.substring(to: minPos)) as NSString
  78. }else {
  79. resultStr = resultStr.appending(markStr as String) as NSString
  80. }
  81. }
  82. resultStr = self.replaceUnicodeString(resultStr)
  83. #else
  84. let resultData = NSData.init(contentsOfFile: resultPath) as! Data
  85. var rDocumentAttributes:NSDictionary!
  86. let resultAttString = NSAttributedString.init(rtf: resultData, documentAttributes: &rDocumentAttributes)
  87. var resultStr = NSString(string: resultAttString!.string)
  88. #endif
  89. resultStr = resultStr.replacingOccurrences(of: "\n", with: "") as NSString
  90. checkString = checkString.replacingOccurrences(of: "\n", with: "") as NSString
  91. resultStr = resultStr.replacingOccurrences(of: " ", with: "") as NSString
  92. checkString = checkString.replacingOccurrences(of: " ", with: "") as NSString
  93. resultStr = resultStr.replacingOccurrences(of: "\\pard", with: "") as NSString
  94. resultStr = resultStr.replacingOccurrences(of: "\\par", with: "") as NSString
  95. do { // save cache file for test
  96. try? NSString(string: resultStr).write(toFile: NSString(string: DataModel.shared.resultPath()).appending("/\(self.name())-result-cache.txt"),
  97. atomically: true, encoding: NSUTF8StringEncoding)
  98. try? NSString(string: checkString).write(toFile: NSString(string: DataModel.shared.resultPath()).appending("/\(self.name())-check-cache.txt"),
  99. atomically: true, encoding: NSUTF8StringEncoding)
  100. }
  101. let maxSize = checkString.length
  102. var successCount = 0;
  103. /**
  104. (A0 = B0)
  105. - A-1 & B-1
  106. (A0 != B0) & (A0 in B) & (B0 in A)
  107. - 取 A0,B0最小 Range 值
  108. - 字符串裁剪对齐
  109. (A0 != B0) & (A0 in B)
  110. - 存储B0到识别错误缓存
  111. (A0 != B0) & (B0 in A)
  112. - 存储 A0到识别遗漏字符串
  113. (A0 != B0)
  114. - 分别存储 A0、B0到遗漏及错误字串
  115. */
  116. var skipString = NSString()
  117. var failString = NSString()
  118. while (checkString.length > 0 && resultStr.length > 0) {
  119. let subc = checkString.substring(to: 1) as NSString
  120. let subr = resultStr.substring(to: 1) as NSString
  121. if subc.isEqual(to: subr) { // (A0 = B0)
  122. // Check Success
  123. appendErrorInfo(skipString, failString: failString)
  124. skipString = NSString()
  125. failString = NSString()
  126. checkString = checkString.substring(from:1) as NSString
  127. resultStr = resultStr.substring(from:1) as NSString
  128. successCount = successCount + 1
  129. }else if (checkString.contains(subr as String) && resultStr.contains(subc as String)) {
  130. appendErrorInfo(skipString, failString: failString)
  131. skipString = NSString()
  132. failString = NSString()
  133. let cRange = checkString.range(of: subr as String)
  134. let rRange = resultStr.range(of: subc as String)
  135. if (cRange.location < rRange.location) {
  136. let cacheString = checkString.substring(to:cRange.location + cRange.length)
  137. reportString?.append(NSMutableAttributedString.init(string: "对照字符串【\(cacheString)】未识别到\n",
  138. attributes:[.foregroundColor : NSColor.red]))
  139. checkString = checkString.substring(from:cRange.location) as NSString
  140. }else {
  141. let cacheString = resultStr.substring(to:rRange.location)
  142. reportString?.append(NSMutableAttributedString.init(string: "字符串【\(cacheString)】识别出错\n",
  143. attributes:[.foregroundColor : NSColor.red]))
  144. resultStr = resultStr.substring(from:rRange.location + rRange.length) as NSString
  145. }
  146. }else if (checkString.contains(subr as String)) {
  147. skipString = skipString.appending(subc as String) as NSString
  148. checkString = checkString.substring(from:1) as NSString
  149. }else if (resultStr.contains(subc as String)) {
  150. failString = failString.appending(subr as String) as NSString
  151. resultStr = resultStr.substring(from:1) as NSString
  152. }else {
  153. skipString = skipString.appending(subc as String) as NSString
  154. failString = failString.appending(subr as String) as NSString
  155. checkString = checkString.substring(from:1) as NSString
  156. resultStr = resultStr.substring(from:1) as NSString
  157. }
  158. }
  159. skipString = skipString.appending(checkString as String) as NSString
  160. failString = failString.appending(resultStr as String) as NSString
  161. appendErrorInfo(skipString, failString: failString)
  162. reportString?.append(NSAttributedString.init(string: "【\(self.name())】字符串比对完成,准确率\(Float(successCount)/Float(maxSize) * 100)%(\(successCount)/\(maxSize))\n",
  163. attributes:[.foregroundColor : NSColor.blue]))
  164. }
  165. // compare screenshoot between result file with check file
  166. if needCompare {
  167. }
  168. }else {
  169. reportString = NSMutableAttributedString.init(string: "\n【\(self.name())】转档失败!\n",
  170. attributes:[.foregroundColor : NSColor.red])
  171. }
  172. NSLog("\(reportString)")
  173. _status = .Finished
  174. }
  175. func appendErrorInfo(_ skipString:NSString, failString: NSString) {
  176. if skipString.length > 0 && failString.length > 0 {
  177. reportString?.append(NSMutableAttributedString.init(string: "对比字符串【\(skipString)】错识别为【\(failString)】\n",
  178. attributes:[.foregroundColor : NSColor.red]))
  179. }else if (skipString.length > 0) {
  180. reportString?.append(NSMutableAttributedString.init(string: "对比字符串【\(skipString)】未识别到\n",
  181. attributes:[.foregroundColor : NSColor.red]))
  182. }else if failString.length > 0 {
  183. reportString?.append(NSMutableAttributedString.init(string: "字符串【\(failString)】识别出错\n",
  184. attributes:[.foregroundColor : NSColor.red]))
  185. }
  186. }
  187. func replaceUnicodeString(_ string:NSString) -> NSString {
  188. let items = string.components(separatedBy: "\\u") as [NSString]
  189. var resultString = NSString()
  190. for item in items {
  191. if (item.contains("?")) {
  192. let unicodeValue = item.intValue
  193. let skipRange = item.range(of: "?")
  194. let nextString = item.substring(from: Int(skipRange.location + skipRange.length)) as NSString
  195. let bytes : [UInt8] = [UInt8(unicodeValue/256),UInt8(unicodeValue%256)]
  196. let data = NSData.init(bytes: bytes, length: 2)
  197. let unicodeString = NSString.init(data: data as Data, encoding: NSUnicodeStringEncoding)! as NSString
  198. resultString = resultString.appending(String("\(unicodeString)\(nextString)")) as NSString
  199. }else {
  200. resultString = resultString.appending(String(item)) as NSString
  201. }
  202. }
  203. return resultString
  204. }
  205. // Auto Test refrence Latest Status
  206. override func autoCheck() {
  207. _status = .Process
  208. _status = .Finished
  209. }
  210. }