[irstlm] 21/78: code cleanup

Giulio Paci giuliopaci-guest at moszumanska.debian.org
Tue May 17 07:47:02 UTC 2016


This is an automated email from the git hooks/post-receive script.

giuliopaci-guest pushed a commit to tag adaptiveLM.v0.10
in repository irstlm.

commit 0e3c80693aa510585fe2acdf2cc7a216847f3dbb
Author: Nicola Bertoldi <bertoldi at fbk.eu>
Date:   Mon Nov 9 00:43:22 2015 +0100

    code cleanup
---
 src/n_gram.cpp   | 12 ++++++++-
 src/n_gram.h     |  4 ++-
 src/ngramtable.h |  5 ++++
 src/util.cpp     | 75 +++++++++++++++++++++++++++++++++++++++++---------------
 src/util.h       | 28 +++++++++++++--------
 5 files changed, 92 insertions(+), 32 deletions(-)

diff --git a/src/n_gram.cpp b/src/n_gram.cpp
index 1a9352f..866fe9a 100644
--- a/src/n_gram.cpp
+++ b/src/n_gram.cpp
@@ -150,7 +150,6 @@ ifstream& operator>> ( ifstream& fi , ngram& ng)
 	
 }
 
-
 int ngram::pushw(const char* w)
 {
 	
@@ -169,6 +168,17 @@ int ngram::pushw(const char* w)
 	
 }
 
+int ngram::pushw(std::string w){
+	return pushw(w.c_str());
+}
+
+int ngram::pushw(std::vector<std::string> w_vec){
+	for (std::vector<std::string>::iterator it=w_vec.begin(); it != w_vec.end(); ++it){
+		pushw(*it);
+	}
+	return 1;
+}
+
 int ngram::pushc(int c)
 {
 	
diff --git a/src/n_gram.h b/src/n_gram.h
index 91de1ec..f1e42d1 100644
--- a/src/n_gram.h
+++ b/src/n_gram.h
@@ -119,7 +119,9 @@ public:
   int pushc(int c);
   int pushc(int* codes, int sz);
   int pushw(const char* w);
-
+  int pushw(std::string w);
+  int pushw(std::vector<std::string> w_vec);
+	
   //~ngram();
 };
 
diff --git a/src/ngramtable.h b/src/ngramtable.h
index 1286a12..a533856 100644
--- a/src/ngramtable.h
+++ b/src/ngramtable.h
@@ -370,6 +370,11 @@ public:
     inline int bo_state(int value) {
         return backoff_state=value;
     }
+	
+	
+	inline dictionary* getDict() const {
+		return dict;
+	};
 };
 
 #endif
diff --git a/src/util.cpp b/src/util.cpp
index 24220bf..1763b7c 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -205,15 +205,14 @@ double GetUserTime()
 	return g_timer.get_elapsed_time();
 }
 
-
-void ShowProgress(long long current, long long target){
-    
-    int frac=(current * 1000)/target;
-    if (!(frac % 10)) fprintf(stderr,"%02d\b\b",frac/10);
-
+void ShowProgress(long long current, long long target)
+{
+	
+	int frac=(current * 1000)/target;
+	if (!(frac % 10)) fprintf(stderr,"%02d\b\b",frac/10);
+	
 }
 
-
 int parseWords(char *sentence, const char **words, int max)
 {
 	char *word;
@@ -279,7 +278,8 @@ int parseline(istream& inp, int Order,ngram& ng,float& prob,float& bow)
 	return 1;
 }
 
-void exit_error(int err, const std::string &msg){
+void exit_error(int err, const std::string &msg)
+{
 	if (msg != "") {
 		VERBOSE(0,msg+"\n";);
 	}
@@ -312,17 +312,17 @@ void exit_error(int err, const std::string &msg){
 };
 
 /*
-#ifdef MY_ASSERT_FLAG
-#if MY_ASSERT_FLAG>0
-#undef MY_ASSERT(x)
-#define MY_ASSERT(x) do { assert(x); } while (0)
-#else
-#define MY_ASSERT(x) { UNUSED(x); }
-#endif
-#else
-#define MY_ASSERT(x) { UNUSED(x); }
-#endif
-*/
+ #ifdef MY_ASSERT_FLAG
+ #if MY_ASSERT_FLAG>0
+ #undef MY_ASSERT(x)
+ #define MY_ASSERT(x) do { assert(x); } while (0)
+ #else
+ #define MY_ASSERT(x) { UNUSED(x); }
+ #endif
+ #else
+ #define MY_ASSERT(x) { UNUSED(x); }
+ #endif
+ */
 
 /** assert macros e functions**/
 #ifdef MY_ASSERT_FLAG
@@ -364,7 +364,7 @@ namespace irstlm {
 			return NULL;
 		}
 	}
-
+	
 	string_vec_t &split(const std::string &s, const char delim, string_vec_t &elems) {
 		std::stringstream ss(s);
 		std::string item;
@@ -374,6 +374,41 @@ namespace irstlm {
 		return elems;
 	}
 	
+	float logsum(float a,float b){
+		if (b<a) return a + logf(1 + expf(b-a));
+		else return b + logf(1+ expf(a-b));
+	}
+	
+	float log10sum(float a,float b){
+		if (b<a) return a + log10(1 + pow(10.0,(b-a)));
+		else return b + log10(1 + pow(10.0,(a-b)));
+	}
+	
+	double logsum(double a,double b){
+		if (b<a) return a + log(1 + exp(b-a));
+		else return b + log(1 + exp(a-b));
+	}
+	
+	double log10sum(double a,double b){
+		if (b<a) return a + log10(1 + pow(10.0,(b-a)));
+		else return b + log10(1 + pow(10.0,(a-b)));
+	}
+	
+	
+	double logistic_function(double x, double max, double steep)
+	{
+		//domain: (-inf, +inf)
+		//image: [0,max)
+		//logistic_function(0.0,max,steep) = max/2
+		//lim_{x->+inf} logistic_function(x,max,steep) = max
+		//lim_{x->-inf} logistic_function(x,max,steep) = 0
+		//if steep1>steep2, then
+		// logistic_function(x,max,steep1) > logistic_function(x,max,steep2)   if x>0
+		// logistic_function(x,max,steep1) < logistic_function(x,max,steep2)   if x<0
+		
+		return max/1+exp(-steep*x);
+	}
+	
 	
 }
 
diff --git a/src/util.h b/src/util.h
index 23609d8..000ab73 100644
--- a/src/util.h
+++ b/src/util.h
@@ -9,6 +9,7 @@
 #include <vector>
 #include <map>
 #include <assert.h>
+#include <math.h>
 
 using namespace std;
 
@@ -84,10 +85,6 @@ int parseline(istream& inp, int Order,ngram& ng,float& prob,float& bow);
 
 void exit_error(int err, const std::string &msg="");
 
-namespace irstlm{
-  string_vec_t &split(const std::string &s, const char delim, string_vec_t &elems);
-	void* reallocf(void *ptr, size_t size);
-}
 
 //extern int tracelevel;
 extern const int tracelevel;
@@ -97,14 +94,25 @@ extern const int tracelevel;
 #define IFVERBOSE(level) if (tracelevel > level)
 
 /*
-#define _DEBUG_LEVEL TRACE_LEVEL
-
-#define TRACE_ERR(str) { std::cerr << str; }
-#define VERBOSE(level,str) { if (_DEBUG_LEVEL > level) { TRACE_ERR("DEBUG_LEVEL:" <<_DEBUG_LEVEL << " "); TRACE_ERR(str); } }
-#define IFVERBOSE(level) if (_DEBUG_LEVEL > level)
-*/
+ #define _DEBUG_LEVEL TRACE_LEVEL
+ 
+ #define TRACE_ERR(str) { std::cerr << str; }
+ #define VERBOSE(level,str) { if (_DEBUG_LEVEL > level) { TRACE_ERR("DEBUG_LEVEL:" <<_DEBUG_LEVEL << " "); TRACE_ERR(str); } }
+ #define IFVERBOSE(level) if (_DEBUG_LEVEL > level)
+ */
 
 void MY_ASSERT(bool x);
 
+namespace irstlm{
+  string_vec_t &split(const std::string &s, const char delim, string_vec_t &elems);
+	void* reallocf(void *ptr, size_t size);
+	
+	float logsum(float a,float b);
+	float log10sum(float a,float b);
+	double logsum(double a,double b);
+	double log10sum(double a,double b);
+	
+	double logistic_function(double x, double max=1.0, double steep=1.0);
+}
 #endif
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/irstlm.git



More information about the debian-science-commits mailing list