Skip to content
Browse files

Reordering methods

  • Loading branch information...
1 parent 5291e63 commit 199fe14e5eb337f50dfeb005557bf7db4c69ca2f @smendes smendes committed Mar 28, 2010
View
26 BayesianKit.xcodeproj/project.pbxproj
@@ -410,14 +410,36 @@
GCC_ENABLE_FIX_AND_CONTINUE = YES;
GCC_ENABLE_OBJC_GC = supported;
GCC_MODEL_TUNING = G5;
- GCC_OPTIMIZATION_LEVEL = 0;
+ GCC_OPTIMIZATION_LEVEL = 1;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = BayesianKit_Prefix.pch;
- GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
+ GCC_VERSION = "";
+ GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
+ GCC_WARN_ABOUT_MISSING_NEWLINE = YES;
+ GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
+ GCC_WARN_ALLOW_INCOMPLETE_PROTOCOL = NO;
+ GCC_WARN_CHECK_SWITCH_STATEMENTS = YES;
+ GCC_WARN_FOUR_CHARACTER_CONSTANTS = YES;
+ GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES;
+ GCC_WARN_MISSING_PARENTHESES = YES;
+ GCC_WARN_MULTIPLE_DEFINITION_TYPES_FOR_SELECTOR = NO;
+ GCC_WARN_NON_VIRTUAL_DESTRUCTOR = YES;
+ GCC_WARN_PEDANTIC = NO;
+ GCC_WARN_PROTOTYPE_CONVERSION = NO;
+ GCC_WARN_SHADOW = NO;
+ GCC_WARN_SIGN_COMPARE = YES;
+ GCC_WARN_STRICT_SELECTOR_MATCH = NO;
+ GCC_WARN_UNDECLARED_SELECTOR = NO;
+ GCC_WARN_UNINITIALIZED_AUTOS = YES;
+ GCC_WARN_UNUSED_FUNCTION = YES;
+ GCC_WARN_UNUSED_LABEL = YES;
+ GCC_WARN_UNUSED_PARAMETER = YES;
+ GCC_WARN_UNUSED_VALUE = YES;
GENERATE_PROFILING_CODE = YES;
INFOPLIST_FILE = Info.plist;
INSTALL_PATH = "$(TARGET_BUILD_DIR)";
PRODUCT_NAME = BayesianKit;
+ RUN_CLANG_STATIC_ANALYZER = YES;
SDKROOT = "";
WRAPPER_EXTENSION = framework;
ZERO_LINK = YES;
View
20 src/BKBayesianClassifier.h
@@ -52,23 +52,31 @@
@property (readonly) NSMutableDictionary *pools;
-- (float)robinsonCombinerOnProbabilities:(NSArray*)probabilities;
+
+- (id)initWithContentsOfFile:(NSString*)path;
+
+- (BKBayesianClassifier*)classifierWithContentsOfFile:(NSString*)path;
+
+- (BOOL)writeToFile:(NSString*)path;
+
+- (BKBayesianDataPool*)poolNamed:(NSString*)poolName;
+- (void)removePoolNamed:(NSString*)poolName;
+- (void)mergePoolNamed:(NSString*)sourcePoolName withPoolNamed:(NSString*)destPoolName;
- (void)updatePoolsProbabilities;
- (void)buildProbabilityCache;
+- (float)robinsonCombinerOnProbabilities:(NSArray*)probabilities;
+
- (void)trainWithFile:(NSString*)path forPoolNamed:(NSString*)poolName;
- (void)trainWithString:(NSString*)trainString forPoolNamed:(NSString*)poolName;
- (void)trainWithTokens:(NSArray*)tokens inPool:(BKBayesianDataPool*)pool;
-- (void)stripToLevel:(NSUInteger)level;
-
- (NSDictionary*)guessWithString:(NSString*)string;
- (NSDictionary*)guessWithFile:(NSString*)path;
+- (NSDictionary*)guessWithTokens:(NSArray*)tokens;
-- (id)initWithContentsOfFile:(NSString*)path;
-- (BKBayesianClassifier*)classifierWithContentsOfFile:(NSString*)path;
-- (BOOL)writeToFile:(NSString*)path;
+- (void)stripToLevel:(NSUInteger)level;
- (void)printInformations;
View
77 src/BKBayesianClassifier.m
@@ -56,6 +56,15 @@ - (id)init
return self;
}
+- (id)initWithContentsOfFile:(NSString*)path
+{
+ self = [[NSKeyedUnarchiver unarchiveObjectWithFile:path] retain];
+ if (self) {
+ }
+ return self;
+}
+
+
- (void)dealloc
{
[corpus release];
@@ -64,13 +73,7 @@ - (void)dealloc
}
#pragma mark -
-#pragma mark Serialization Methods
-- (void)encodeWithCoder:(NSCoder*)coder
-{
- [coder encodeObject:corpus forKey:@"Corpus"];
- [coder encodeObject:pools forKey:@"Pools"];
-}
-
+#pragma mark NSCoding Methods
- (id)initWithCoder:(NSCoder*)coder
{
self = [super init];
@@ -84,19 +87,21 @@ - (id)initWithCoder:(NSCoder*)coder
return self;
}
-- (id)initWithContentsOfFile:(NSString*)path
+- (void)encodeWithCoder:(NSCoder*)coder
{
- self = [[NSKeyedUnarchiver unarchiveObjectWithFile:path] retain];
- if (self) {
- }
- return self;
+ [coder encodeObject:corpus forKey:@"Corpus"];
+ [coder encodeObject:pools forKey:@"Pools"];
}
+#pragma mark -
+#pragma mark Creation Methods
- (BKBayesianClassifier*)classifierWithContentsOfFile:(NSString*)path
{
return [[[BKBayesianClassifier alloc] initWithContentsOfFile:path] autorelease];
}
+#pragma mark -
+#pragma mark Saving Methods
- (BOOL)writeToFile:(NSString*)path
{
return [NSKeyedArchiver archiveRootObject:self toFile:path];
@@ -140,20 +145,6 @@ - (void)mergePoolNamed:(NSString*)sourcePoolName withPoolNamed:(NSString*)destPo
#pragma mark -
#pragma mark Probabilities
-- (void)stripToLevel:(NSUInteger)level
-{
- for (NSString *token in [corpus allTokens]) {
- NSUInteger count = [corpus countForToken:token];
-
- if (count < level) {
- for (NSString *poolName in pools) {
- BKBayesianDataPool *pool = [pools objectForKey:poolName];
- [pool removeToken:token];
- }
- [corpus removeToken:token];
- }
- }
-}
- (void)updatePoolsProbabilities
{
if (dirty) {
@@ -168,7 +159,7 @@ - (void)buildProbabilityCache
BKBayesianDataPool *pool = [pools objectForKey:poolName];
NSUInteger poolTotalCount = [pool tokensTotalCount];
- NSUInteger deltaTotalCount = MAX([corpus tokensTotalCount] - poolTotalCount, 1);
+ NSUInteger deltaTotalCount = MAX([corpus tokensTotalCount] - poolTotalCount, 1u);
for (NSString *token in pool) {
NSUInteger corpusCount = [corpus countForToken:token];
@@ -194,7 +185,7 @@ - (void)buildProbabilityCache
- (float)robinsonCombinerOnProbabilities:(NSArray*)probabilities
{
NSUInteger length = [probabilities count];
- float nth = 1.0f / length;
+ float nth = 1.0f / (uint32_t)length;
float probs[length], inverseProbs[length];
NSUInteger idx = 0;
@@ -206,7 +197,7 @@ - (float)robinsonCombinerOnProbabilities:(NSArray*)probabilities
float inverseProbsReduced = inverseProbs[0];
float probsReduced = probs[0];
- for (int i = 1; i < length; i++) {
+ for (NSUInteger i = 1; i < length; i++) {
inverseProbsReduced = inverseProbsReduced * inverseProbs[i];
probsReduced = probsReduced * probs[i];
}
@@ -251,7 +242,7 @@ - (void)trainWithTokens:(NSArray*)tokens inPool:(BKBayesianDataPool*)pool
}
#pragma mark -
-#pragma mark Classification Methods
+#pragma mark Guessing Methods
- (NSDictionary*)guessWithFile:(NSString*)path
{
NSError *error = nil;
@@ -269,8 +260,13 @@ - (NSDictionary*)guessWithString:(NSString*)string
{
NSArray *tokens = [_tokenizer tokenizeString:string];
[self updatePoolsProbabilities];
+ return [self guessWithTokens:tokens];
+}
+
+- (NSDictionary*)guessWithTokens:(NSArray*)tokens
+{
+ NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[pools count]];
- NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:42];
for (NSString *poolName in pools) {
BKBayesianDataPool *pool = [pools objectForKey:poolName];
NSArray *tokensProbabilities = [pool probabilitiesForTokens:tokens];
@@ -285,7 +281,24 @@ - (NSDictionary*)guessWithString:(NSString*)string
}
#pragma mark -
-#pragma mark Classifier Informations
+#pragma mark Sanitizing Methods
+- (void)stripToLevel:(NSUInteger)level
+{
+ for (NSString *token in [corpus allTokens]) {
+ NSUInteger count = [corpus countForToken:token];
+
+ if (count < level) {
+ for (NSString *poolName in pools) {
+ BKBayesianDataPool *pool = [pools objectForKey:poolName];
+ [pool removeToken:token];
+ }
+ [corpus removeToken:token];
+ }
+ }
+}
+
+#pragma mark -
+#pragma mark Printing Methods
- (void)printInformations
{
[self updatePoolsProbabilities];
View
5 src/BKBayesianDataPool.h
@@ -50,7 +50,6 @@
- (id)initWithName:(NSString*)aName;
-- (void)removeToken:(NSString*)token;
- (NSUInteger)countForToken:(NSString*)token;
- (void)setCount:(NSUInteger)count forToken:(NSString*)token;
- (void)addCount:(NSUInteger)count forToken:(NSString*)token;
@@ -61,8 +60,8 @@
- (NSArray*)probabilitiesForTokens:(NSArray*)tokens;
- (NSArray*)allTokens;
-- (void)printInformations;
+- (void)removeToken:(NSString*)token;
-- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len;
+- (void)printInformations;
@end
View
35 src/BKBayesianDataPool.m
@@ -53,6 +53,15 @@ - (id)initWithName:(NSString*)aName
return self;
}
+- (void)dealloc
+{
+ [name release];
+ [_tokensCount release];
+ [super dealloc];
+}
+
+#pragma mark -
+#pragma mark NSCoding Methods
- (id)initWithCoder:(NSCoder*)coder
{
self = [super init];
@@ -64,13 +73,6 @@ - (id)initWithCoder:(NSCoder*)coder
return self;
}
-- (void)dealloc
-{
- [name release];
- [_tokensCount release];
- [super dealloc];
-}
-
- (void)encodeWithCoder:(NSCoder*)coder
{
[coder encodeObject:name forKey:@"Name"];
@@ -80,12 +82,6 @@ - (void)encodeWithCoder:(NSCoder*)coder
#pragma mark -
#pragma mark Token Counting Methods
-- (void)removeToken:(NSString*)token
-{
- _tokensTotalCount -= [self countForToken:token];
- [_tokensCount removeObjectForKey:token];
-}
-
- (NSUInteger)countForToken:(NSString*)token
{
BKBayesianTokenData *data = [_tokensCount objectForKey:token];
@@ -165,12 +161,21 @@ - (NSArray*)probabilitiesForTokens:(NSArray*)tokens
}
#pragma mark -
-#pragma mark Misc
+#pragma mark General Token Manipulation
- (NSArray*)allTokens
{
return [_tokensCount allKeys];
}
+- (void)removeToken:(NSString*)token
+{
+ _tokensTotalCount -= [self countForToken:token];
+ [_tokensCount removeObjectForKey:token];
+}
+
+
+#pragma mark -
+#pragma mark Printing Methods
- (NSString*)description
{
return [_tokensCount description];
@@ -192,7 +197,7 @@ - (void)printInformations
}
#pragma mark -
-#pragma mark Fast Enumeration Methods
+#pragma mark NSFastEnumeration Methods
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
{
return [_tokensCount countByEnumeratingWithState:state objects:stackbuf count:len];
View
1 src/BKBayesianTokenData.h
@@ -47,6 +47,7 @@
@property (readwrite, assign) float probability;
- (id)initWithCount:(NSUInteger)aCount;
+
+ (BKBayesianTokenData*)tokenDataWithCount:(NSUInteger)aCount;
- (NSComparisonResult)compareCount:(BKBayesianTokenData*)other;
View
34 src/BKBayesianTokenData.m
@@ -52,6 +52,13 @@ - (id)initWithCount:(NSUInteger)aCount
return self;
}
++ (BKBayesianTokenData*)tokenDataWithCount:(NSUInteger)aCount
+{
+ return [[[BKBayesianTokenData alloc] initWithCount:aCount] autorelease];
+}
+
+#pragma mark -
+#pragma mark NSCoding Methods
- (id)initWithCoder:(NSCoder*)coder
{
self = [super init];
@@ -62,27 +69,21 @@ - (id)initWithCoder:(NSCoder*)coder
return self;
}
-+ (BKBayesianTokenData*)tokenDataWithCount:(NSUInteger)aCount
+- (void)encodeWithCoder:(NSCoder*)coder
{
- return [[[BKBayesianTokenData alloc] initWithCount:aCount] autorelease];
+ [coder encodeInteger:count forKey:@"Count"];
+ [coder encodeFloat:probability forKey:@"Probability"];
}
+#pragma mark -
+#pragma mark Custom Setters
- (void)setProbability:(float)aProbability
{
probability = MAX(0.0001f, MIN(0.9999f, aProbability));
}
-- (NSString*)description
-{
- return [NSString stringWithFormat:@"{count: %llu, probability: %f}", count, probability];
-}
-
-- (void)encodeWithCoder:(NSCoder*)coder
-{
- [coder encodeInteger:count forKey:@"Count"];
- [coder encodeFloat:probability forKey:@"Probability"];
-}
-
+#pragma mark -
+#pragma mark Comparison Methods
- (NSComparisonResult)compareCount:(BKBayesianTokenData*)other
{
return [[NSNumber numberWithUnsignedInteger:count] compare:
@@ -95,4 +96,11 @@ - (NSComparisonResult)compareProbability:(BKBayesianTokenData*)other
[NSNumber numberWithFloat:[other probability]]];
}
+#pragma mark -
+#pragma mark Printing Methods
+- (NSString*)description
+{
+ return [NSString stringWithFormat:@"{count: %llu, probability: %f}", count, probability];
+}
+
@end

0 comments on commit 199fe14

Please sign in to comment.
Something went wrong with that request. Please try again.