[irstlm] 121/126: improved code; code cleanuo

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


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

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

commit 16053cc251db5e0816f31a89b3be7e7318d0d670
Author: Nicola Bertoldi <bertoldi at fbk.eu>
Date:   Fri Oct 23 08:07:01 2015 +0200

    improved code; code cleanuo
---
 src/lmContainer.cpp     |  2 +-
 src/lmInterpolation.cpp | 39 +++++++++++++++++++++++++--------------
 src/lmInterpolation.h   | 12 +++++++-----
 src/util.h              |  2 ++
 4 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/src/lmContainer.cpp b/src/lmContainer.cpp
index 56e7187..34895a5 100644
--- a/src/lmContainer.cpp
+++ b/src/lmContainer.cpp
@@ -169,6 +169,6 @@ namespace irstlm {
 			return res;
 		}
 		return false;
-	};
+	}
 	
 }//namespace irstlm
diff --git a/src/lmInterpolation.cpp b/src/lmInterpolation.cpp
index 18cf612..06db624 100644
--- a/src/lmInterpolation.cpp
+++ b/src/lmInterpolation.cpp
@@ -23,6 +23,7 @@
 #include <cstdlib>
 #include <stdlib.h>
 #include <iostream>
+#include <sstream>
 #include <stdexcept>
 #include <string>
 #include "lmContainer.h"
@@ -41,7 +42,7 @@ namespace irstlm {
 		order=0;
 		memmap=0;
 		isInverted=false;
-		m_name_flag=false;
+		m_map_flag=false;
 	}
 	
 	void lmInterpolation::load(const std::string &filename,int mmap)
@@ -81,13 +82,13 @@ namespace irstlm {
 		
 		size_t idx_weight, idx_file, idx_name, idx_inverted, idx_size;
 		if (tokenN==2){
-			m_name_flag=false;
+			m_map_flag=false;
 			idx_weight=0;
 			idx_file=1;
 			idx_inverted=2;
 			idx_size=3;
 		}else{
-			m_name_flag=true;
+			m_map_flag=true;
 			idx_weight=0;
 			idx_name=1;
 			idx_file=2;
@@ -122,9 +123,18 @@ namespace irstlm {
 			VERBOSE(2,"i:" << i << " m_isinverted[i]:" << m_isinverted[i] << endl);
 			
 			m_weight[i] = atof(words[idx_weight]);
-			m_name[words[idx_name]] = i;
+			if (m_map_flag){
+				m_idx[words[idx_name]] = i;
+				m_name[i] = words[idx_name];
+			}else{
+				std::stringstream name;
+				name << i;
+				m_idx[name.str()] = i;
+				m_name[i] = name.str();
+			}
 			m_file[i] = words[idx_file];
-			VERBOSE(2,"lmInterpolation::load(const std::string &filename,int mmap) m_file:"<< words[1] << std::endl;);
+			
+			VERBOSE(2,"lmInterpolation::load(const std::string &filename,int mmap) i:" << i << " m_name:|"<< m_name[i] << "|" " m_file:|"<< m_file[i] << "|" << std::endl);
 			
 			m_lm[i] = load_lm(i,memmap,ngramcache_load_factor,dictionary_load_factor);
 			//set the actual value for inverted flag, which is known only after loading the lM
@@ -169,13 +179,6 @@ namespace irstlm {
 		return lmt;
 	}
 	
-	
-	void lmInterpolation::set_weight(const lm_map_t& map, std::vector<double>& weight){
-		for (lm_map_t::const_iterator it=map.begin(); it!=map.end();++it){
-			weight[m_name[it->first]] = it->second;
-		}
-	}
-	
 	//return log10 prob of an ngram
 	double lmInterpolation::clprob(ngram ng, lm_map_t& lm_weights, double* bow,int* bol,char** maxsuffptr,unsigned int* statesize,bool* extendible)
 	{
@@ -191,11 +194,10 @@ namespace irstlm {
 		bool _extendible=false;
 		bool actualextendible=false;
 
-		std::vector<double> weight(m_number_lm);
+		double_vec_t weight(m_number_lm);
 		set_weight(lm_weights,weight);
 		
 		for (size_t i=0; i<m_lm.size(); i++) {
-			
 			if (weight[i]>0.0){
 				ngram _ng(m_lm[i]->getDict());
 				_ng.trans(ng);
@@ -362,4 +364,13 @@ namespace irstlm {
 		logOOVpenalty=log10(OOVpenalty);
 		return logOOVpenalty;
 	}
+	
+	void lmInterpolation::set_weight(const lm_map_t& map, double_vec_t& weight){
+		VERBOSE(4,"void lmInterpolation::set_weight" << std::endl);
+		VERBOSE(4,"map.size:" << map.size() << std::endl);
+		for (lm_map_t::const_iterator it=map.begin(); it!=map.end();++it){
+			weight[m_idx[it->first]] = it->second;
+		  VERBOSE(4,"it->first:|" << it->first << "| it->second:|" << it->second << "| m_idx[it->first]:|" << m_idx[it->first] << "| weight[m_idx[it->first]]:|" <<weight[m_idx[it->first]] << "|" << std::endl);
+		}
+	}
 }//namespace irstlm
diff --git a/src/lmInterpolation.h b/src/lmInterpolation.h
index 686ef9e..23d70f4 100644
--- a/src/lmInterpolation.h
+++ b/src/lmInterpolation.h
@@ -50,15 +50,17 @@ class lmInterpolation: public lmContainer
   int dictionary_upperbound; //set by user
   double  logOOVpenalty; //penalty for OOV words (default 0)
   bool      isInverted;
-	bool m_name_flag; //flag for the presence of a map between name and lm
+	bool m_map_flag; //flag for the presence of a map between name and lm
   int memmap;  //level from which n-grams are accessed via mmap
 
   std::vector<double> m_weight;
   std::vector<std::string> m_file;
   std::vector<bool> m_isinverted;
   std::vector<lmContainer*> m_lm;
-	lm_map_t m_name;
-
+	
+	std::map< std::string, size_t > m_idx;
+	std::map< size_t, std::string > m_name;
+	
   int               maxlev; //maximun order of sub LMs;
 
   float ngramcache_load_factor;
@@ -66,13 +68,13 @@ class lmInterpolation: public lmContainer
 
   dictionary *dict; // dictionary for all interpolated LMs
 
+	void set_weight(const lm_map_t& map, std::vector<double>& weight);
+	
 public:
 
   lmInterpolation(float nlf=0.0, float dlfi=0.0);
   virtual ~lmInterpolation() {};
 
-	void set_weight(const lm_map_t& map, std::vector<double>& weight);
-	
   void load(const std::string &filename,int mmap=0);
   lmContainer* load_lm(int i, int memmap, float nlf, float dlf);
 
diff --git a/src/util.h b/src/util.h
index 90ec07e..67ee339 100644
--- a/src/util.h
+++ b/src/util.h
@@ -50,6 +50,8 @@ using namespace std;
 #define SSEED 50
 
 typedef std::vector< std::string > string_vec_t;
+typedef std::vector< double > double_vec_t;
+typedef std::vector< float > float_vec_t;
 
 class ngram;
 class mfstream;

-- 
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