|
@@ -8,7 +8,7 @@
|
|
import Foundation
|
|
import Foundation
|
|
import Cocoa
|
|
import Cocoa
|
|
|
|
|
|
-class CharacterAutoTest : AutoTest {
|
|
|
|
|
|
+class StringAutoTest : AutoTest {
|
|
|
|
|
|
// override func type() -> String {
|
|
// override func type() -> String {
|
|
// return "PDFConvert_China_Auto_Test"
|
|
// return "PDFConvert_China_Auto_Test"
|
|
@@ -19,23 +19,30 @@ class CharacterAutoTest : AutoTest {
|
|
}
|
|
}
|
|
|
|
|
|
override func keys() -> NSArray {
|
|
override func keys() -> NSArray {
|
|
- return ["字符", "快照"]
|
|
|
|
|
|
+ return ["字符"]
|
|
}
|
|
}
|
|
|
|
|
|
- static var cSharedInstance = CharacterAutoTest()
|
|
|
|
|
|
+
|
|
|
|
+ override func needTest() -> Bool {
|
|
|
|
+ return self.selectedKeys().count > 0
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
override class func shared() -> AutoTest? {
|
|
override class func shared() -> AutoTest? {
|
|
- return cSharedInstance
|
|
|
|
|
|
+ return StringAutoTest()
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// Auto Test refrence Check File
|
|
// Auto Test refrence Check File
|
|
override func autoTest(_ complention:@escaping (_ object:AutoTest, _ report:NSAttributedString?) -> ()) {
|
|
override func autoTest(_ complention:@escaping (_ object:AutoTest, _ report:NSAttributedString?) -> ()) {
|
|
|
|
+ self.compareFinishedFiles.removeAllObjects();
|
|
|
|
+ self.convertFiles.removeAllObjects()
|
|
|
|
+
|
|
clearCacheFiles()
|
|
clearCacheFiles()
|
|
|
|
|
|
- let checkString = self.selectedKeys().contains("字符")
|
|
|
|
- let needCompare = self.selectedKeys().contains("快照")
|
|
|
|
|
|
+ let needCheckString = self.selectedKeys().contains("字符")
|
|
|
|
|
|
- if !needCompare && !checkString {
|
|
|
|
|
|
+ if !needCheckString {
|
|
_status = .Finished
|
|
_status = .Finished
|
|
complention(self, self.reportString)
|
|
complention(self, self.reportString)
|
|
return
|
|
return
|
|
@@ -45,6 +52,7 @@ class CharacterAutoTest : AutoTest {
|
|
reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】字符比对开始!\n",
|
|
reportString = NSMutableAttributedString.init(string: "\n【\(String(self.fileType())) - \(self.name())】字符比对开始!\n",
|
|
attributes:[.foregroundColor : NSColor.blue])
|
|
attributes:[.foregroundColor : NSColor.blue])
|
|
let files = DataModel.shared.originFilesFor(_fileType, type: _type)
|
|
let files = DataModel.shared.originFilesFor(_fileType, type: _type)
|
|
|
|
+ self.testFiles = NSArray(array: files);
|
|
|
|
|
|
let checkDirectory = self.checkFileDirectory()
|
|
let checkDirectory = self.checkFileDirectory()
|
|
let originDirectory = self.originFileDirectory()
|
|
let originDirectory = self.originFileDirectory()
|
|
@@ -86,211 +94,81 @@ class CharacterAutoTest : AutoTest {
|
|
attributes:[.foregroundColor : NSColor.black]))
|
|
attributes:[.foregroundColor : NSColor.black]))
|
|
// ...
|
|
// ...
|
|
// 执行转换过程
|
|
// 执行转换过程
|
|
|
|
+ let index = self.testFiles.index(of: fileName);
|
|
|
|
+ if (index != NSNotFound) {
|
|
|
|
+ self.convertProgress = Double(index) / Double(self.testFiles.count)
|
|
|
|
+ }
|
|
|
|
+ self.convertFiles.add(fileName);
|
|
|
|
+ self.testlog("开始转换:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
|
|
self.process(originPath, resultPath: resultPath) { status in
|
|
self.process(originPath, resultPath: resultPath) { status in
|
|
if FileManager.default.fileExists(atPath: resultPath) && status == 1 {
|
|
if FileManager.default.fileExists(atPath: resultPath) && status == 1 {
|
|
- if checkString && FileManager.default.fileExists(atPath: checkPath) {
|
|
|
|
- // Load check file
|
|
|
|
- let checkData = NSData.init(contentsOfFile: checkPath) as! Data
|
|
|
|
- var documentAttributes:NSDictionary!
|
|
|
|
- let checkAttString = NSAttributedString.init(rtf: checkData, documentAttributes: &documentAttributes)
|
|
|
|
- var checkString = NSString(string: checkAttString!.string) as NSString
|
|
|
|
-
|
|
|
|
- let resultString = try? NSString.init(contentsOfFile: resultPath, encoding: NSUTF8StringEncoding)
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-#if false
|
|
|
|
- //识别字符串 \shptxt\shptxt ... }
|
|
|
|
- let pageInfoStrings = resultString!.components(separatedBy: "\\shptxt\\shptxt") as NSArray
|
|
|
|
- var finalString = ""
|
|
|
|
- if pageInfoStrings.count > 0 {
|
|
|
|
- let subStrings = pageInfoStrings.subarray(with: NSMakeRange(1, Int(pageInfoStrings.count - 1))) as! [String]
|
|
|
|
- for pageInfoString in subStrings {
|
|
|
|
- let endRange = NSString(string: pageInfoString).range(of: "}")
|
|
|
|
- finalString = finalString.appending(NSString(string: pageInfoString).substring(to: endRange.location))
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- //识别所有 【空格 ~ \】 之间的值,并进行缝合
|
|
|
|
- // Detect all strings between Spaces and \ and stitch
|
|
|
|
- let strings = finalString.components(separatedBy: " ")
|
|
|
|
- var resultStr = "" as NSString
|
|
|
|
- for str in strings {
|
|
|
|
- let markStr = str as NSString
|
|
|
|
|
|
+ if needCheckString && FileManager.default.fileExists(atPath: checkPath) {
|
|
|
|
+ DispatchQueue.global().async {
|
|
|
|
+ let checkString = self.readTextFile(checkPath as NSString)
|
|
|
|
+ let resultStr = self.readTextFile(resultPath as NSString)
|
|
|
|
|
|
- if (markStr.contains("\\f")) {
|
|
|
|
- let fRange = markStr.range(of: "\\f")
|
|
|
|
- let cRange = markStr.range(of: "\\c")
|
|
|
|
- let bRange = markStr.range(of: "\\b")
|
|
|
|
- let iRange = markStr.range(of: "\\i")
|
|
|
|
- let eRange = markStr.range(of: "\\e")
|
|
|
|
- let pRange = markStr.range(of: "\\p")
|
|
|
|
- let minPos = min(Int(fRange.location),
|
|
|
|
- Int(cRange.location),
|
|
|
|
- Int(bRange.location),
|
|
|
|
- Int(iRange.location),
|
|
|
|
- Int(eRange.location),
|
|
|
|
- Int(pRange.location))
|
|
|
|
- resultStr = resultStr.appending(markStr.substring(to: minPos)) as NSString
|
|
|
|
- }else {
|
|
|
|
- resultStr = resultStr.appending(markStr as String) as NSString
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- resultStr = self.replaceUnicodeString(resultStr)
|
|
|
|
-#else
|
|
|
|
- let resultData = NSData.init(contentsOfFile: resultPath) as! Data
|
|
|
|
- var rDocumentAttributes:NSDictionary!
|
|
|
|
- let resultAttString = NSAttributedString.init(rtf: resultData, documentAttributes: &rDocumentAttributes)
|
|
|
|
- var resultStr = NSString(string: resultAttString!.string)
|
|
|
|
-#endif
|
|
|
|
- resultStr = resultStr.replacingOccurrences(of: "\n", with: "") as NSString
|
|
|
|
- checkString = checkString.replacingOccurrences(of: "\n", with: "") as NSString
|
|
|
|
- resultStr = resultStr.replacingOccurrences(of: " ", with: "") as NSString
|
|
|
|
- checkString = checkString.replacingOccurrences(of: " ", with: "") as NSString
|
|
|
|
- resultStr = resultStr.replacingOccurrences(of: "\\pard", with: "") as NSString
|
|
|
|
- resultStr = resultStr.replacingOccurrences(of: "\\par", with: "") as NSString
|
|
|
|
-
|
|
|
|
- // do { // save cache file for test
|
|
|
|
- // try? NSString(string: resultStr).write(toFile: NSString(string: DataModel.shared.resultPath()).appending("/\(self.name())-result-cache.txt"),
|
|
|
|
- // atomically: true, encoding: NSUTF8StringEncoding)
|
|
|
|
- //
|
|
|
|
- // try? NSString(string: checkString).write(toFile: NSString(string: DataModel.shared.resultPath()).appending("/\(self.name())-check-cache.txt"),
|
|
|
|
- // atomically: true, encoding: NSUTF8StringEncoding)
|
|
|
|
- // }
|
|
|
|
-
|
|
|
|
- let maxSize = checkString.length
|
|
|
|
- var successCount = 0;
|
|
|
|
- /**
|
|
|
|
- (A0 = B0)
|
|
|
|
- - A-1 & B-1
|
|
|
|
- (A0 != B0) & (A0 in B) & (B0 in A)
|
|
|
|
- - 取 A0,B0最小 Range 值
|
|
|
|
- - 字符串裁剪对齐
|
|
|
|
- (A0 != B0) & (A0 in B)
|
|
|
|
- - 存储B0到识别错误缓存
|
|
|
|
- (A0 != B0) & (B0 in A)
|
|
|
|
- - 存储 A0到识别遗漏字符串
|
|
|
|
- (A0 != B0)
|
|
|
|
- - 分别存储 A0、B0到遗漏及错误字串
|
|
|
|
- */
|
|
|
|
- var skipString = NSString()
|
|
|
|
- var failString = NSString()
|
|
|
|
- while (checkString.length > 0 && resultStr.length > 0) {
|
|
|
|
- let subc = checkString.substring(to: 1) as NSString
|
|
|
|
- let subr = resultStr.substring(to: 1) as NSString
|
|
|
|
-
|
|
|
|
- if subc.isEqual(to: subr) { // (A0 = B0)
|
|
|
|
- // Check Success
|
|
|
|
- self.appendErrorInfo(skipString, failString: failString)
|
|
|
|
- skipString = NSString()
|
|
|
|
- failString = NSString()
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- checkString = checkString.substring(from:1) as NSString
|
|
|
|
- resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
- successCount = successCount + 1
|
|
|
|
- }else if (checkString.contains(subr as String) && resultStr.contains(subc as String)) {
|
|
|
|
- self.appendErrorInfo(skipString, failString: failString)
|
|
|
|
- skipString = NSString()
|
|
|
|
- failString = NSString()
|
|
|
|
-
|
|
|
|
- let cRange = checkString.range(of: subr as String)
|
|
|
|
- let rRange = resultStr.range(of: subc as String)
|
|
|
|
-
|
|
|
|
- if (cRange.location < rRange.location) {
|
|
|
|
- let cacheString = checkString.substring(to:cRange.location + cRange.length)
|
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "对照字符串【\(cacheString)】未识别到\n",
|
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
|
- checkString = checkString.substring(from:cRange.location) as NSString
|
|
|
|
- }else {
|
|
|
|
- let cacheString = resultStr.substring(to:rRange.location)
|
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "字符串【\(cacheString)】识别出错\n",
|
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
|
- resultStr = resultStr.substring(from:rRange.location + rRange.length) as NSString
|
|
|
|
|
|
+ if (checkString != nil && resultStr != nil) {
|
|
|
|
+ let maxSize = checkString!.count
|
|
|
|
+ let report = NSMutableAttributedString(string: "")
|
|
|
|
+ let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { appAttr in
|
|
|
|
+ report.append(appAttr)
|
|
}
|
|
}
|
|
|
|
+// let degree = self.compareString(checkString as! NSString, result: resultStr as! NSString) { skipString, failString in
|
|
|
|
+// self.appendErrorInfo(skipString, failString: failString)
|
|
|
|
+// }
|
|
|
|
|
|
- }else if (checkString.contains(subr as String)) {
|
|
|
|
- skipString = skipString.appending(subc as String) as NSString
|
|
|
|
- checkString = checkString.substring(from:1) as NSString
|
|
|
|
- }else if (resultStr.contains(subc as String)) {
|
|
|
|
- failString = failString.appending(subr as String) as NSString
|
|
|
|
- resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
- }else {
|
|
|
|
- skipString = skipString.appending(subc as String) as NSString
|
|
|
|
- failString = failString.appending(subr as String) as NSString
|
|
|
|
|
|
+ var color = NSColor.black
|
|
|
|
+ if fabs(degree-100.0) >= 0.01 {
|
|
|
|
+ color = NSColor.red
|
|
|
|
+ }
|
|
|
|
+ tDegree += degree;
|
|
|
|
+ tCount += 1
|
|
|
|
|
|
- checkString = checkString.substring(from:1) as NSString
|
|
|
|
- resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- skipString = skipString.appending(checkString as String) as NSString
|
|
|
|
- failString = failString.appending(resultStr as String) as NSString
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- self.appendErrorInfo(skipString, failString: failString)
|
|
|
|
-
|
|
|
|
- let degree = Float(successCount)/Float(maxSize) * 100
|
|
|
|
- var color = NSColor.black
|
|
|
|
- if fabs(degree-100.0) >= 0.01 {
|
|
|
|
- color = NSColor.red
|
|
|
|
- }
|
|
|
|
- self.reportString?.append(NSAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"比对完成,准确率\(degree)%(\(successCount)/\(maxSize))\n",
|
|
|
|
- attributes:[.foregroundColor : color]))
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- // compare screenshoot between result file with check file
|
|
|
|
- if needCompare {
|
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照生成中\n",
|
|
|
|
- attributes:[.foregroundColor : NSColor.black]))
|
|
|
|
- let rComparePath = NSString(string: resultDirectory).appendingPathComponent(fName+".jpg")
|
|
|
|
- let cComparePath = NSString(string: checkDirectory).appendingPathComponent(fName+".jpg")
|
|
|
|
-
|
|
|
|
- let processThumbSemaphore = DispatchSemaphore(value: 0)
|
|
|
|
- var processSuccess = false
|
|
|
|
- let thumbnailQueue = DispatchQueue.global()
|
|
|
|
- thumbnailQueue.async {
|
|
|
|
- autoreleasepool {
|
|
|
|
- processSuccess = ProcessThumbnal.process(resultPath, desPath: rComparePath, outputSize: CGSize.init(width: 2048, height: 2048))
|
|
|
|
|
|
+ TestDegreeManager.shared().set(degree,
|
|
|
|
+ fileType: self.fileType(),
|
|
|
|
+ type: self.type(),
|
|
|
|
+ fileName: fileName)
|
|
|
|
|
|
- if (processSuccess &&
|
|
|
|
- FileManager.default.fileExists(atPath: rComparePath)) {
|
|
|
|
-
|
|
|
|
- let degree = ImageProcess.compareJPEG(rComparePath, checkPath: cComparePath, processCover: true)
|
|
|
|
-
|
|
|
|
- if degree == -1 {
|
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比失败,生成快照失败或无比对文件\n",
|
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
|
- }else {
|
|
|
|
- var color = NSColor.black
|
|
|
|
- if fabs(degree-100.0) >= 0.01 {
|
|
|
|
- color = NSColor.red
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- TestDegreeManager.shared().set(degree,
|
|
|
|
- fileType: self.fileType(),
|
|
|
|
- type: self.type(),
|
|
|
|
- fileName: fileName);
|
|
|
|
- TestDegreeManager.shared().set(degree,
|
|
|
|
- fileType: self.fileType(),
|
|
|
|
- type: self.type(),
|
|
|
|
- fileName: fileName,
|
|
|
|
- refFilePath: fName+".jpg");
|
|
|
|
-
|
|
|
|
- tDegree += degree
|
|
|
|
- tCount += 1
|
|
|
|
|
|
+ let successCount = Int(maxSize * Int(degree)/100)
|
|
|
|
+ report.append(NSAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"比对完成,准确率\(degree)%(\(successCount)/\(maxSize))\n",
|
|
|
|
+ attributes:[.foregroundColor : color]))
|
|
|
|
+ if (report != nil) {
|
|
|
|
+ do {
|
|
|
|
+ let rtfData = try? report.data(from: .init(location: 0, length: report.length),
|
|
|
|
+ documentAttributes: [.documentType: NSAttributedString.DocumentType.rtf])
|
|
|
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照对比完成,图像相似度 \(degree)%\n",
|
|
|
|
- attributes:[.foregroundColor : color]))
|
|
|
|
|
|
+ let path = NSString(string: resultPath).appendingPathExtension("rtf")
|
|
|
|
+ try? FileManager.default.removeItem(atPath: path!);
|
|
|
|
+ try? rtfData?.write(to: NSURL.fileURL(withPath: path!))
|
|
|
|
+ } catch {
|
|
}
|
|
}
|
|
- }else {
|
|
|
|
- self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"快照生成失败\n",
|
|
|
|
- attributes:[.foregroundColor : NSColor.red]))
|
|
|
|
|
|
+ self.reportString?.append(report)
|
|
}
|
|
}
|
|
- processThumbSemaphore.signal()
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ let index = self.testFiles.index(of: fileName);
|
|
|
|
+ if (index != NSNotFound) {
|
|
|
|
+ self.compareProgress = Double(index) / Double(self.testFiles.count)
|
|
|
|
+ }
|
|
|
|
+ self.compareFinishedFiles.add(fileName);
|
|
|
|
+ self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
|
|
|
|
+
|
|
|
|
+ fileIndex += 1
|
|
|
|
+ convertFileBlock(files);
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】对照文件 \"\(fName)\"不存在!\n",
|
|
|
|
+ attributes:[.foregroundColor : NSColor.red]))
|
|
|
|
+
|
|
|
|
+ let index = self.testFiles.index(of: fileName);
|
|
|
|
+ if (index != NSNotFound) {
|
|
|
|
+ self.compareProgress = Double(index) / Double(self.testFiles.count)
|
|
}
|
|
}
|
|
- processThumbSemaphore.wait()
|
|
|
|
|
|
+ self.compareFinishedFiles.add(fileName);
|
|
|
|
+ self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
|
|
|
|
+
|
|
|
|
+ fileIndex += 1
|
|
|
|
+ convertFileBlock(files);
|
|
}
|
|
}
|
|
}else {
|
|
}else {
|
|
if (status == 0) {
|
|
if (status == 0) {
|
|
@@ -303,10 +181,16 @@ class CharacterAutoTest : AutoTest {
|
|
self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
|
|
self.reportString?.append(NSMutableAttributedString.init(string: "【\(String(self.fileType())) - \(self.name())】文件 \"\(fName)\"转档中 Crash!\n",
|
|
attributes:[.foregroundColor : NSColor.red]))
|
|
attributes:[.foregroundColor : NSColor.red]))
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ let index = self.testFiles.index(of: fileName);
|
|
|
|
+ if (index != NSNotFound) {
|
|
|
|
+ self.compareProgress = Double(index) / Double(self.testFiles.count)
|
|
|
|
+ }
|
|
|
|
+ self.compareFinishedFiles.add(fileName);
|
|
|
|
+ self.testlog("对比完成:"+fileName, (self.compareProgress + self.convertProgress)/2.0)
|
|
|
|
+ fileIndex += 1
|
|
|
|
+ convertFileBlock(files);
|
|
}
|
|
}
|
|
-
|
|
|
|
- fileIndex += 1
|
|
|
|
- convertFileBlock(files);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
convertFileBlock(files);
|
|
convertFileBlock(files);
|
|
@@ -328,6 +212,35 @@ class CharacterAutoTest : AutoTest {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ func attributeStringWith(_ skipString:NSString, failString: NSString) -> NSAttributedString {
|
|
|
|
+ if skipString.length > 0 && failString.length > 0 {
|
|
|
|
+ let strikethroughStyle = NSParagraphStyle.init()
|
|
|
|
+ let attString = NSMutableAttributedString.init(string: skipString as String,
|
|
|
|
+ attributes:[.foregroundColor : NSColor.red,
|
|
|
|
+ .strikethroughStyle:NSNumber(integerLiteral: NSUnderlineStyle.single.rawValue),
|
|
|
|
+ ])
|
|
|
|
+ attString.append(NSMutableAttributedString.init(string: failString as String,
|
|
|
|
+ attributes:[.foregroundColor : NSColor.blue,
|
|
|
|
+ ]))
|
|
|
|
+ return attString
|
|
|
|
+ }else if (skipString.length > 0) {
|
|
|
|
+ let strikethroughStyle = NSParagraphStyle.init()
|
|
|
|
+ let attString = NSMutableAttributedString.init(string: skipString as String,
|
|
|
|
+ attributes:[.foregroundColor : NSColor.red,
|
|
|
|
+ .strikethroughStyle:NSNumber(integerLiteral: NSUnderlineStyle.single.rawValue),
|
|
|
|
+ ])
|
|
|
|
+ return attString
|
|
|
|
+ }else if failString.length > 0 {
|
|
|
|
+ let strikethroughStyle = NSParagraphStyle.init()
|
|
|
|
+ let attString = NSMutableAttributedString.init(string: failString as String,
|
|
|
|
+ attributes:[.foregroundColor : NSColor.blue,
|
|
|
|
+ ])
|
|
|
|
+ return attString
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return NSAttributedString()
|
|
|
|
+ }
|
|
|
|
+
|
|
func replaceUnicodeString(_ string:NSString) -> NSString {//中
|
|
func replaceUnicodeString(_ string:NSString) -> NSString {//中
|
|
let items = string.components(separatedBy: "\\u") as [NSString]
|
|
let items = string.components(separatedBy: "\\u") as [NSString]
|
|
|
|
|
|
@@ -350,4 +263,253 @@ class CharacterAutoTest : AutoTest {
|
|
return resultString
|
|
return resultString
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ /// Compare
|
|
|
|
+ func compareString(_ check:NSString, result:NSString, callback:@escaping (_ appAttr:NSAttributedString)->()) -> Double {
|
|
|
|
+// func compareString(_ check:NSString, result:NSString, failure:@escaping (_ skipString:NSString, _ failString:NSString)->()) -> Double {
|
|
|
|
+ return autoreleasepool {
|
|
|
|
+ var checkString = check
|
|
|
|
+ var resultStr = result
|
|
|
|
+ let maxSize = checkString.length
|
|
|
|
+ var successCount = 0;
|
|
|
|
+ /**
|
|
|
|
+ (A0 = B0)
|
|
|
|
+ - A-1 & B-1
|
|
|
|
+ (A0 != B0) & (A0 in B) & (B0 in A)
|
|
|
|
+ - 取 A0,B0最小 Range 值
|
|
|
|
+ - 字符串裁剪对齐
|
|
|
|
+ (A0 != B0) & (A0 in B)
|
|
|
|
+ - 存储B0到识别错误缓存
|
|
|
|
+ (A0 != B0) & (B0 in A)
|
|
|
|
+ - 存储 A0到识别遗漏字符串
|
|
|
|
+ (A0 != B0)
|
|
|
|
+ - 分别存储 A0、B0到遗漏及错误字串
|
|
|
|
+ */
|
|
|
|
+ var skipString = NSString()
|
|
|
|
+ var failString = NSString()
|
|
|
|
+ while (checkString.length > 0 && resultStr.length > 0) {
|
|
|
|
+ let subc = checkString.substring(to: 1) as NSString
|
|
|
|
+ let subr = resultStr.substring(to: 1) as NSString
|
|
|
|
+
|
|
|
|
+ let cRange = checkString.range(of: subr as String)
|
|
|
|
+ let rRange = resultStr.range(of: subc as String)
|
|
|
|
+
|
|
|
|
+ if subc.isEqual(to: subr) { // (A0 = B0)
|
|
|
|
+ // Check Success
|
|
|
|
+ callback(attributeStringWith(skipString, failString: failString))
|
|
|
|
+// failure(skipString, failString)
|
|
|
|
+// self.appendErrorInfo(skipString, failString: failString)
|
|
|
|
+ skipString = NSString()
|
|
|
|
+ failString = NSString()
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ checkString = checkString.substring(from:1) as NSString
|
|
|
|
+ resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
+ successCount = successCount + 1
|
|
|
|
+ callback(NSAttributedString(string: subc as String))
|
|
|
|
+ } else if (cRange.location != NSNotFound &&
|
|
|
|
+ rRange.location != NSNotFound) {
|
|
|
|
+ if (min(cRange.location, rRange.location) >= 2) {
|
|
|
|
+ let nextc = checkString.substring(with: NSRange(location: 1, length: 1)) as NSString
|
|
|
|
+ let nextr = resultStr.substring(with: NSRange(location: 1, length: 1)) as NSString
|
|
|
|
+ let ncRange = checkString.range(of: nextr as String)
|
|
|
|
+ let nrRange = resultStr.range(of: nextc as String)
|
|
|
|
+
|
|
|
|
+ if (min(nrRange.location, ncRange.location) < min(cRange.location, rRange.location)) {
|
|
|
|
+ if (ncRange.location < nrRange.location) {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ skipString = skipString.appending(subc as String) as NSString
|
|
|
|
+ checkString = checkString.substring(from:1) as NSString
|
|
|
|
+ }else {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ failString = failString.appending(subr as String) as NSString
|
|
|
|
+ resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
+ }
|
|
|
|
+ }else if (cRange.location < rRange.location) {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ skipString = skipString.appending(subc as String) as NSString
|
|
|
|
+ checkString = checkString.substring(from:1) as NSString
|
|
|
|
+ }else {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ failString = failString.appending(subr as String) as NSString
|
|
|
|
+ resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
+ }
|
|
|
|
+ }else {
|
|
|
|
+ if (cRange.location < rRange.location) {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ skipString = skipString.appending(subc as String) as NSString
|
|
|
|
+ checkString = checkString.substring(from:1) as NSString
|
|
|
|
+ }else {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ failString = failString.appending(subr as String) as NSString
|
|
|
|
+ resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
+ }
|
|
|
|
+// // self.appendErrorInfo(skipString, failString: failString)
|
|
|
|
+// callback(attributeStringWith(skipString, failString: failString))
|
|
|
|
+// // failure(skipString, failString)
|
|
|
|
+// skipString = NSString()
|
|
|
|
+// failString = NSString()
|
|
|
|
+//
|
|
|
|
+// // C:a[bcde]fghij
|
|
|
|
+// // R:f[klmnbvcx]a
|
|
|
|
+// if (cRange.location < rRange.location) {
|
|
|
|
+// let cacheString = checkString.substring(to:cRange.location + cRange.length)
|
|
|
|
+// callback(attributeStringWith(cacheString as NSString, failString: ""))
|
|
|
|
+// // failure(cacheString as NSString, "")
|
|
|
|
+// // self.reportString?.append(NSMutableAttributedString.init(string: "对照字符串【\(cacheString)】未识别到\n",
|
|
|
|
+// // attributes:[.foregroundColor : NSColor.red]))
|
|
|
|
+// checkString = checkString.substring(from:cRange.location) as NSString
|
|
|
|
+// }else {
|
|
|
|
+// let cacheString = resultStr.substring(to:rRange.location)
|
|
|
|
+// callback(attributeStringWith("", failString: cacheString as NSString))
|
|
|
|
+// // failure("", cacheString as NSString)
|
|
|
|
+// // self.reportString?.append(NSMutableAttributedString.init(string: "字符串【\(cacheString)】识别出错\n",
|
|
|
|
+// // attributes:[.foregroundColor : NSColor.red]))
|
|
|
|
+// resultStr = resultStr.substring(from:rRange.location + rRange.length) as NSString
|
|
|
|
+// }
|
|
|
|
+ }
|
|
|
|
+ }else if (cRange.location != NSNotFound) {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ skipString = skipString.appending(subc as String) as NSString
|
|
|
|
+ checkString = checkString.substring(from:1) as NSString
|
|
|
|
+ }else if (rRange.location != NSNotFound) {
|
|
|
|
+ //‘subc' 字符串有在‘resultStr’中,但'subr'不在’checkString‘中,resultStr 往后推一
|
|
|
|
+ failString = failString.appending(subr as String) as NSString
|
|
|
|
+ resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
+ }else {
|
|
|
|
+ // 两个子字串均未找到
|
|
|
|
+ skipString = skipString.appending(subc as String) as NSString
|
|
|
|
+ failString = failString.appending(subr as String) as NSString
|
|
|
|
+
|
|
|
|
+ checkString = checkString.substring(from:1) as NSString
|
|
|
|
+ resultStr = resultStr.substring(from:1) as NSString
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ skipString = skipString.appending(checkString as String) as NSString
|
|
|
|
+ failString = failString.appending(resultStr as String) as NSString
|
|
|
|
+// failure(skipString, failString)
|
|
|
|
+ callback(attributeStringWith(skipString, failString: failString))
|
|
|
|
+ callback(NSAttributedString(string: "\n"))
|
|
|
|
+
|
|
|
|
+ let degree = (maxSize>1) ? Double(Float(successCount)/Float(maxSize) * 100) : 0
|
|
|
|
+
|
|
|
|
+ return degree
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Read File
|
|
|
|
+ func readTextFile(_ filePath:NSString) -> String? {
|
|
|
|
+ if NSArray(array: ["TXT", "txt"]).contains(filePath.pathExtension) {
|
|
|
|
+ var checkString = try? NSString.init(contentsOfFile: filePath as String, encoding: NSUTF8StringEncoding)
|
|
|
|
+ if (checkString != nil) {
|
|
|
|
+ checkString = checkString!.replacingOccurrences(of: "\n", with: "") as NSString
|
|
|
|
+ checkString = checkString!.replacingOccurrences(of: " ", with: "") as NSString
|
|
|
|
+ return checkString! as String
|
|
|
|
+ }
|
|
|
|
+ return nil
|
|
|
|
+ }else if NSArray(array: ["rtf", "RTF"]).contains(filePath.pathExtension) {
|
|
|
|
+ // Load check file
|
|
|
|
+ let checkData = NSData.init(contentsOfFile: filePath as String) as! Data
|
|
|
|
+ var documentAttributes:NSDictionary!
|
|
|
|
+ let checkAttString = NSAttributedString.init(rtf: checkData, documentAttributes: &documentAttributes)
|
|
|
|
+ var checkString = NSString(string: checkAttString!.string) as NSString
|
|
|
|
+ checkString = checkString.replacingOccurrences(of: "\n", with: "") as NSString
|
|
|
|
+ checkString = checkString.replacingOccurrences(of: " ", with: "") as NSString
|
|
|
|
+ // 常规 rtf 读取失败
|
|
|
|
+
|
|
|
|
+ //使用框排进行读取
|
|
|
|
+ if (checkString.length > 0) {
|
|
|
|
+ return checkString as String?
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var resultString = try? NSString.init(contentsOfFile: filePath as String, encoding: NSUTF8StringEncoding)
|
|
|
|
+
|
|
|
|
+ if (nil != resultString && !resultString!.contains("\\shptxt\\shptxt")) {
|
|
|
|
+ resultString = resultString!.replacingOccurrences(of: "\n", with: "") as NSString
|
|
|
|
+ resultString = resultString!.replacingOccurrences(of: " ", with: "") as NSString
|
|
|
|
+ return resultString! as String
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //识别字符串 \shptxt\shptxt ... }
|
|
|
|
+ let pageInfoStrings = resultString!.components(separatedBy: "\\shptxt\\shptxt") as NSArray
|
|
|
|
+ var finalString = ""
|
|
|
|
+ if pageInfoStrings.count > 0 {
|
|
|
|
+ let subStrings = pageInfoStrings.subarray(with: NSMakeRange(1, Int(pageInfoStrings.count - 1))) as! [String]
|
|
|
|
+ for pageInfoString in subStrings {
|
|
|
|
+ let endRange = NSString(string: pageInfoString).range(of: "}")
|
|
|
|
+ finalString = finalString.appending(NSString(string: pageInfoString).substring(to: endRange.location))
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ //识别所有 【空格 ~ \】 之间的值,并进行缝合
|
|
|
|
+ // Detect all strings between Spaces and \ and stitch
|
|
|
|
+ let strings = finalString.components(separatedBy: " ")
|
|
|
|
+ var resultStr = "" as NSString
|
|
|
|
+ for str in strings {
|
|
|
|
+ let markStr = str as NSString
|
|
|
|
+
|
|
|
|
+ if (markStr.contains("\\f")) {
|
|
|
|
+ let fRange = markStr.range(of: "\\f")
|
|
|
|
+ let cRange = markStr.range(of: "\\c")
|
|
|
|
+ let bRange = markStr.range(of: "\\b")
|
|
|
|
+ let iRange = markStr.range(of: "\\i")
|
|
|
|
+ let eRange = markStr.range(of: "\\e")
|
|
|
|
+ let pRange = markStr.range(of: "\\p")
|
|
|
|
+ let minPos = min(Int(fRange.location),
|
|
|
|
+ Int(cRange.location),
|
|
|
|
+ Int(bRange.location),
|
|
|
|
+ Int(iRange.location),
|
|
|
|
+ Int(eRange.location),
|
|
|
|
+ Int(pRange.location))
|
|
|
|
+ resultStr = resultStr.appending(markStr.substring(to: minPos)) as NSString
|
|
|
|
+ }else {
|
|
|
|
+ resultStr = resultStr.appending(markStr as String) as NSString
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resultStr = self.replaceUnicodeString(resultStr)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ resultStr = resultStr.replacingOccurrences(of: "\n", with: "") as NSString
|
|
|
|
+ resultStr = resultStr.replacingOccurrences(of: " ", with: "") as NSString
|
|
|
|
+ resultStr = resultStr.replacingOccurrences(of: "\\pard", with: "") as NSString
|
|
|
|
+ resultStr = resultStr.replacingOccurrences(of: "\\par", with: "") as NSString
|
|
|
|
+
|
|
|
|
+ return resultStr as String?
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ override func compareFiles() -> NSArray? {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override func compareFiles(_ fileName: String) -> NSArray? {
|
|
|
|
+ return nil
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ Replace the refrence image for next image check test
|
|
|
|
+ */
|
|
|
|
+ override func canUpdateRefImage() -> Bool {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override func updateRefImage() {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override func canUpdateRefImage(_ fileName:String) -> Bool {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override func updateRefImage(_ fileName:String) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|