SearchResult class refactor & always fetch article path in title search

This commit is contained in:
Chris Li 2016-07-02 15:20:35 -04:00
parent 79dc065793
commit 2ccee07c5d
5 changed files with 27 additions and 24 deletions

View File

@ -92,7 +92,7 @@ class SearchResultTBVC: UIViewController, UITableViewDataSource, UITableViewDele
func configureArticleCell(cell: ArticleCell, result: SearchResult) {
guard let book = Book.fetch(result.bookID, context: UIApplication.appDelegate.managedObjectContext) else {return}
cell.titleLabel.text = result.title + "(\(result.distance), \(result.percent ?? -1), \(result.score))"
cell.titleLabel.text = result.title + "(\(result.distance), \(result.probability ?? -1), \(result.score))"
cell.hasPicIndicator.backgroundColor = book.hasPic ? UIColor.havePicTintColor : UIColor.lightGrayColor()
cell.favIcon.image = book.favIcon != nil ? UIImage(data: book.favIcon!) : nil
}

View File

@ -98,9 +98,9 @@ class SearchTuneController: UIViewController, UITableViewDataSource {
}
class WeightFactor {
class func calculate(prob: Double) -> Double? {
class func calculate(prob: Double) -> Double {
let e = 2.718281828459
guard let m = Defaults[.m], let n = Defaults[.n] else {return nil}
guard let m = Defaults[.m], let n = Defaults[.n] else {return 1}
return log(n - m * prob) / log(e)
}
}

View File

@ -36,7 +36,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1372</string>
<string>1377</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -161,42 +161,45 @@ extension ZimReader {
}
class SearchResult: CustomStringConvertible {
let bookID: String
let title: String
let path: String?
let path: String
let bookID: String
let snippet: String?
let percent: Double? // range: 0-100
let probability: Double? // range: 0.0 - 1.0
let distance: Int // Levenshtein distance, non negative integer
let score: Double
init?(rawResult: [String: AnyObject]) {
let title = (rawResult["title"] as? String) ?? ""
self.bookID = (rawResult["bookID"] as? String) ?? ""
self.title = title
self.path = rawResult["path"] as? String
self.snippet = rawResult["snippet"] as? String
let path = (rawResult["path"] as? String) ?? ""
let bookID = (rawResult["bookID"] as? String) ?? ""
let snippet = rawResult["snippet"] as? String
let percent = (rawResult["percent"] as? NSNumber)?.doubleValue
let distance = (rawResult["distance"]as? NSNumber)?.integerValue ?? title.characters.count
let score: Double = {
if let percent = percent {
return (WeightFactor.calculate(percent / 100) ?? 1) * Double(distance)
let probability: Double? = {
if let probability = (rawResult["probability"] as? NSNumber)?.doubleValue {
return probability / 100.0
} else {
return Double(distance)
return nil
}
}()
self.percent = percent
let score = WeightFactor.calculate(probability ?? 1) * Double(distance)
self.title = title
self.path = path
self.bookID = bookID
self.snippet = snippet
self.probability = probability
self.distance = distance
self.score = score
if bookID == "" {return nil}
if title == "" {return nil}
if title == "" || path == "" || bookID == "" {return nil}
}
var description: String {
var parts = [bookID, title]
if let percent = percent {parts.append("\(percent)%")}
if let probability = probability {parts.append("\(probability)%")}
parts.append("dist: \(distance)")
return parts.joinWithSeparator(", ")
}

View File

@ -95,12 +95,12 @@
NSString *snippet = [NSString stringWithUTF8String:doc.get_value(1).c_str()];
NSNumber *distance = [NSNumber numberWithInteger:[self levenshteinDistance:searchTerm andString:title.lowercaseString]];
NSDictionary *result = @{@"percent": percent,
NSDictionary *result = @{@"title": title,
@"path": path,
@"title": title,
@"snippet": snippet,
@"bookID": bookID,
@"distance": distance};
@"probability": percent,
@"distance": distance,
@"snippet": snippet};
[results addObject:result];
}
return results;