Ejemplo n.º 1
0
	def __regTrafficUser( self, inUsername ):
		#############################
		# 応答データ
		wRes = {
			"Result"	: False,
			"Reason"	: "",
			"Update"	: False
		}
		
		#############################
		# 名前の妥当性チェック
		wResUser = CLS_UserData.sUserCheck( inUsername )
			##	"Result"	: False,
			##	"User"		: "",
			##	"Domain"	: "",
			##	"Reason"	: "",
			##	"Registed"	: False,
		if wResUser['Result']!=True or wResUser['Registed']==False :
			wRes['Reason'] = "CLS_Regist : __regTrafficUser: Username is not valid: " + inUsername
			return wRes	#不正
		
		#############################
		# 読み出し先初期化
		wTrafficUser = []
		
		#############################
		# ファイル読み込み
		wFile_path = gVal.DEF_STR_FILE['TrafficFile']
		if CLS_File.sReadFile( wFile_path, outLine=wTrafficUser )!=True :
			wRes['Reason'] = "CLS_Regist : __regTrafficUser: TrafficFile read is failed: " + gVal.DEF_STR_FILE['TrafficFile']
			return wRes	#失敗
		
		#############################
		# 既登録のドメインがあるか
		wFlg = False
		for wLine in wTrafficUser :
			wDomain = wLine.split("@")
			if wDomain[1]==wResUser['Domain'] :
				## 既登録で既にドメインがあった
				if wLine==gVal.STR_MasterConfig['MasterUser'] :
					##MasterUserの場合差し替え
					wTrafficUser.remove( gVal.STR_MasterConfig['MasterUser'] )
				else :
					##登録あり
					wFlg = True
				break
		
		if wFlg==True :
			wRes['Result'] = True
			return wRes	#更新しないで終了
		
		#############################
		# DBに登録
		
		#############################
		# DB接続
		wOBJ_DB = CLS_PostgreSQL_Use( gVal.DEF_STR_FILE['DBinfo_File'] )
		wDBRes = wOBJ_DB.GetIniStatus()
		if wDBRes['Result']!=True :
			##失敗
			wRes['Reason'] = "CLS_Regist : __regTrafficUser: DB Connect test is failed: " + wDBRes['Reason']
##			wOBJ_DB.Close()
			return wRes
		
		#############################
		# ドメイン存在チェック
		wQuery = "domain = '" + wResUser['Domain'] + "'"
		wDBRes = wOBJ_DB.RunExist( inObjTable="TBL_TRAFFIC_DATA", inWhere=wQuery )
		wDBRes = wOBJ_DB.GetQueryStat()
		if wDBRes['Result']!=True :
			##失敗
##			wRes['Reason'] = "CLS_Regist : __regTrafficUser: Run Query is failed: " + wDBRes['Reason']
			wRes['Reason'] = "CLS_Regist : __regTrafficUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query']
			wOBJ_DB.Close()
			return wRes
		
		#############################
		# 存在しなければ追加
		if wDBRes['Responce']==False :
			wQuery = "insert into TBL_TRAFFIC_DATA values (" + \
						"'" + wResUser['Domain'] + "'," + \
						"0," + \
						"-1," + \
						"-1" + \
						") ;"
			wDBRes = wOBJ_DB.RunQuery( wQuery )
			wDBRes = wOBJ_DB.GetQueryStat()
			if wDBRes['Result']!=True :
				##失敗
				wRes['Reason'] = "CLS_Regist : __regTrafficUser: DB insert is failed: " + wDBRes['Reason']
				wOBJ_DB.Close()
				return wRes
		
		#############################
		# DB切断
		wOBJ_DB.Close()
		
		#############################
		# トラヒックに登録
		wTrafficUser.append( inUsername )
		if self.__setTrafficUser( wTrafficUser )!=True :
			wRes['Reason'] = "CLS_Regist : __regTrafficUser: TrafficFile write is failed: " + gVal.DEF_STR_FILE['TrafficFile']
			return wRes
		
		#############################
		# 正常
		wRes['Update'] = True
		wRes['Result'] = True
		return wRes
Ejemplo n.º 2
0
	def __delTrafficUser( self, inUsername ):
		#############################
		# 応答データ
		wRes = {
			"Result"	: False,
			"Reason"	: "",
			"Update"	: False
		}
		
		#############################
		# 名前の妥当性チェック
		wResUser = CLS_UserData.sUserCheck( inUsername )
			##	"Result"	: False,
			##	"User"		: "",
			##	"Domain"	: "",
			##	"Reason"	: "",
			##	"Registed"	: False,
##		if wResUser['Result']!=True or wResUser['Registed']==False :
		if wResUser['Result']!=True :
			wRes['Reason'] = "CLS_Regist : __delTrafficUser: Username is not valid: " + inUsername
			return wRes	#不正
		
		#############################
		# 読み出し先初期化
		wTrafficUser = []
		
		#############################
		# ファイル読み込み
		wFile_path = gVal.DEF_STR_FILE['TrafficFile']
		if CLS_File.sReadFile( wFile_path, outLine=wTrafficUser )!=True :
			wRes['Reason'] = "CLS_Regist : __delTrafficUser: TrafficFile read is failed: " + gVal.DEF_STR_FILE['TrafficFile']
			return wRes	#失敗
		
		#############################
		# トラヒック対象ではなら削除しない
		if inUsername not in wTrafficUser :
			wRes['Result'] = True
			return wRes	#削除しないで終了
		
		###対象なので削除する
		wTrafficUser.remove( inUsername )
		
		#############################
		# ユーザ一覧取得
		wUserList = CLS_UserData.sGetUserList()
		
		#############################
		# 削除ユーザと同一の既登録ドメインがあるか
		wUsername = None
		for wLine in wUserList :
			wDomain = wLine.split("@")
			if wDomain[1]==wResUser['Domain'] :
				## 既登録で既にドメインがあった
				wUsername = wLine
				break
		
		if wUsername!=None :
			##同一ドメインの別ユーザに切り替える
			wTrafficUser.append( wUsername )
		else :
			##同一ドメインの別ユーザがなければDBから削除する
			#############################
			# DB接続
			wOBJ_DB = CLS_PostgreSQL_Use( gVal.DEF_STR_FILE['DBinfo_File'] )
			wDBRes = wOBJ_DB.GetIniStatus()
			if wDBRes['Result']!=True :
				##失敗
				wRes['Reason'] = "CLS_Regist : __delTrafficUser: DB Connect test is failed: " + wDBRes['Reason']
##				wOBJ_DB.Close()
				return wRes
			
			#############################
			# ドメイン存在チェック
			wQuery = "domain = '" + wResUser['Domain'] + "'"
			wDBRes = wOBJ_DB.RunExist( inObjTable="TBL_TRAFFIC_DATA", inWhere=wQuery )
			wDBRes = wOBJ_DB.GetQueryStat()
			if wDBRes['Result']!=True :
				##失敗
##				wRes['Reason'] = "CLS_Regist : __delTrafficUser: Run Query is failed: " + wDBRes['Reason']
				wRes['Reason'] = "CLS_Regist : __delTrafficUser: Run Query is failed: " + wDBRes['Reason'] + " query=" + wDBRes['Query']
				wOBJ_DB.Close()
				return wRes
			
			#############################
			# 存在していれば削除
			if wDBRes['Responce']==True :
				wQuery = "delete from TBL_TRAFFIC_DATA where domain = '" + wResUser['Domain'] + \
							"' ;"
				wDBRes = wOBJ_DB.RunQuery( wQuery )
				wDBRes = wOBJ_DB.GetQueryStat()
				if wDBRes['Result']!=True :
					##失敗
					wRes['Reason'] = "CLS_Regist : __delTrafficUser: DB insert is failed: " + wDBRes['Reason']
					wOBJ_DB.Close()
					return wRes
			
			#############################
			# DB切断
			wRes['Update'] = True	#DBからは削除
			wOBJ_DB.Close()
		
		#############################
		# トラヒックを更新
		if self.__setTrafficUser( wTrafficUser )!=True :
			wRes['Reason'] = "CLS_Regist : __delTrafficUser: TrafficFile write is failed: " + gVal.DEF_STR_FILE['TrafficFile']
			return wRes
		
		#############################
		# 正常
		wRes['Result'] = True
		return wRes
Ejemplo n.º 3
0
    def WordStudy(self, inCont):
        #############################
        # 単語なし
        if inCont == "":
            return False

        #############################
        # ' 文字が含まれていたら除外する
##		wRes = CLS_OSIF.sRe_Search( "'", inCont )
##		if wRes :
        if inCont.find("'") >= 0:
            return False

        #############################
        # ワード収集が有効か
        if gVal.STR_MasterConfig['WordStudy'] != "on":
            return False  #無効

        #############################
        # 今回の学習数が上限か
        if self.STR_Stat['StudyNum'] >= gVal.DEF_STR_TLNUM['studyNum']:
            return False  #今回は学習しない

        #############################
        # DB接続
        wOBJ_DB = CLS_PostgreSQL_Use(gVal.DEF_STR_FILE['DBinfo_File'])
        wRes = wOBJ_DB.GetIniStatus()
        if wRes['Result'] != True:
            ##失敗
            self.Obj_Parent.OBJ_Mylog.Log(
                'a', "CLS_WordCorr: WordStudy: DB Connect test is failed: " +
                wRes['Reason'])
            wOBJ_DB.Close()
            return False

        wVAL_Rate_WordNum = -1
        #############################
        # 1日1回、古いレコードを削除する
        if gVal.STR_TimeInfo['OneDay'] == True:
            ###現在のレコード数
            wDBRes = wOBJ_DB.RunCount("TBL_WORD_CORRECT")
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: Run Query is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False
            wVAL_Rate_WordNum = wDBRes['Responce']

            ###指定日付の抽出
            wLag = gVal.DEF_STR_TLNUM['studyDay'] * 24 * 60 * 60
            ##			wLagTime = CLS_OSIF.sTimeLag( gVal.STR_TimeInfo['TimeDate'], inThreshold=wLag, inTimezone=-1 )
            wLagTime = CLS_OSIF.sTimeLag(inThreshold=wLag, inTimezone=-1)
            if wLagTime['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: sTimeLag is failed")
                wOBJ_DB.Close()
                return False

            ###単語テーブル
            wQuery = "delete from TBL_WORD_CORRECT where lupdate < " + \
              "timestamp '" + str(wLagTime['RateTime']) + "' " + \
              ";"
            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Old TBL_WORD_CORRECT delete): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            ###品詞パターン テーブル
            wQuery = "delete from TBL_CLAZ_LIST where lupdate < " + \
              "timestamp '" + str(wLagTime['RateTime']) + "' " + \
              ";"
            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Old TBL_CLAZ_LIST delete): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            ###削除後レコード数
            wDBRes = wOBJ_DB.RunCount("TBL_WORD_CORRECT")
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: Run Query is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False
            wVAL_WordNum = wDBRes['Responce']

            ###削除数
            wVAL_Delete_WordNum = wVAL_Rate_WordNum - wVAL_WordNum
            self.STR_Stat["Delete"] += wVAL_Delete_WordNum
            self.Obj_Parent.OBJ_Mylog.Log(
                'b', "古い単語・品詞パターン削除: 対象=" + str(wLagTime['RateTime']) + " 以前")

        #############################
        # レコード数の抽出
        if wVAL_Rate_WordNum == -1:
            ###削除で結果出してない場合に処理する
            wDBRes = wOBJ_DB.RunCount("TBL_WORD_CORRECT")
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a', "CLS_WordCorr: WordStudy: Run Query is failed: " +
                    wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            wVAL_WordNum = wDBRes['Responce']

        #############################
        # 学習数が上限か
        if gVal.DEF_STR_TLNUM['studyMax'] <= wVAL_WordNum:
            if self.STR_Stat['WordLimit'] == False:
                self.Obj_Parent.OBJ_Mylog.Log(
                    'b', "学習不能(単語登録数上限: " +
                    str(gVal.DEF_STR_TLNUM['studyMax']) + "件)")
                self.STR_Stat['WordLimit'] = True
            wOBJ_DB.Close()
            return False  #上限

        #############################
        # デコーダで解読 (出力は辞書型)
        wGetWords = self.__analizeMeCab(inCont)
        if len(wGetWords) == 0:
            self.Obj_Parent.OBJ_Mylog.Log(
                'a',
                "CLS_WordCorr: WordStudy: MeCab analize result is zero, or failed"
            )
            wOBJ_DB.Close()
            return False  #失敗

        # ここまでで登録処理確定
        #############################
        self.STR_Stat['Cope'] += len(wGetWords)  #単語数を記録

        ##		#############################
        ##		# トゥートから時間を取得
        ##		wTime = CLS_OSIF.sGetTimeformat( inCreateAt )
        ##		if wTime['Result']==True :
        ##			wTime = wTime['TimeDate']
        ##		else:
        ##			wTime = None
        ##
        #############################
        # 解読した結果(単語)を判定しながら詰めていく
        wKeylist = wGetWords.keys()  #キーはIndex整数
        wClazList = ""  #文書パターン
        for wKey in wKeylist:
            #############################
            # 登録済みの単語か
            wQuery = "word = '" + wGetWords[wKey]['word'] + "'"
            wDBRes = wOBJ_DB.RunExist("TBL_WORD_CORRECT", wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Word check): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            if wDBRes['Responce'] == True:
                ###登録済み
                ###  触れたので時刻を更新
                wQuery = "update TBL_WORD_CORRECT set " + \
                  "lupdate = '"   + str(gVal.STR_TimeInfo['TimeDate']) + "' " + \
                  "where word = '" + wGetWords[wKey]['word'] + "' ;"

                wDBRes = wOBJ_DB.RunQuery(wQuery)
                wDBRes = wOBJ_DB.GetQueryStat()
                if wDBRes['Result'] != True:
                    ##失敗
                    self.Obj_Parent.OBJ_Mylog.Log(
                        'a',
                        "CLS_WordCorr: WordStudy: Run Query is failed (Word check, time update): "
                        + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                    wOBJ_DB.Close()
                    return False

                ###品詞パターンは記録する
                wClazList = wClazList + wGetWords[wKey]['claz'] + ","
                continue  #登録済なのでスキップ

            #############################
            # 除外する品詞か
            #### BOS/EOS
            if wGetWords[wKey]['claz'] == "BOS/EOS":
                continue

            #### 意識不明な単語、パターンとして使えない単語
            if (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['yomi'] == "*" and wGetWords[wKey]['cla1'] == "サ変接続" ) or \
               (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['cla1'] == "数" ) or \
               wGetWords[wKey]['claz'] == "記号" :
                continue

            #### 名詞かつ 3文字以内の半角英字
            if (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['cla1'] == "一般" ) or \
               (wGetWords[wKey]['claz'] == "名詞" and wGetWords[wKey]['cla1'] == "固有名詞" and wGetWords[wKey]['cla2'] == "組織" ) :
                wRes = CLS_OSIF.sRe_Search(r'^[a-zA-Z]+$',
                                           wGetWords[wKey]['word'])
                if wRes:
                    if len(wGetWords[wKey]['word']) <= 3:
                        continue

            #### 禁止ワードを含むか
##			if self.CheckWordREM( wGetWords[wKey]['word'] )==False :
            if wGetWords[wKey]['word'] in gVal.STR_WordREM:
                continue  #禁止あり

            ###**ループ中に辞書の登録上限を超えても何もしない仕様(DBだしええかと)

            #############################
            # 単語の登録
            wWord = str(wGetWords[wKey]['word']).replace("'", "''")
            wClaz = str(wGetWords[wKey]['claz']).replace("'", "''")
            wYomi = str(wGetWords[wKey]['yomi']).replace("'", "''")
            wQuery = "insert into TBL_WORD_CORRECT values (" + \
               "'" + wWord + "'," + \
               "'" + wClaz + "'," + \
               "'" + wYomi + "'," + \
               "'" + wGetWords[wKey]['cla1'] + "'," + \
               "'" + wGetWords[wKey]['cla2'] + "'," + \
               "'" + wGetWords[wKey]['cla3'] + "'," + \
               "'" + wGetWords[wKey]['ktyp'] + "'," + \
               "'" + wGetWords[wKey]['kkat'] + "'," + \
               "'" + str(gVal.STR_TimeInfo['TimeDate']) + "'," + \
               "False " + \
               ") ;"

            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Word regist): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            self.STR_Stat['Regist'] += 1

            ###品詞パターンは記録する
            wClazList = wClazList + wGetWords[wKey]['claz'] + ","

        #############################
        # 今回の学習回数更新
        self.STR_Stat['StudyNum'] += 1

        #############################
        # 品詞パターン学習
        if wClazList != "":
            wClazList = wClazList[0:len(wClazList) - 1]  #末尾の','を抜く

            ###同じ品詞パターンがあるか
            wQuery = "claz = '" + wClazList + "'"
            wDBRes = wOBJ_DB.RunExist("TBL_CLAZ_LIST", wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Claz check): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            if wDBRes['Responce'] == True:
                ###登録済みならここで終わる(正常)
                wOBJ_DB.Close()
                return True

            ###登録なしなら、登録する
            ###  **ここで品詞パターンの登録上限を超えても何もしない仕様(DBだしええかと)
            wQuery = "insert into TBL_CLAZ_LIST values (" + \
               "'" + wClazList + "'," + \
               "'" + str(gVal.STR_TimeInfo['TimeDate']) + "'," + \
               "False " + \
               ") ;"

            wDBRes = wOBJ_DB.RunQuery(wQuery)
            wDBRes = wOBJ_DB.GetQueryStat()
            if wDBRes['Result'] != True:
                ##失敗
                self.Obj_Parent.OBJ_Mylog.Log(
                    'a',
                    "CLS_WordCorr: WordStudy: Run Query is failed (Claz regist): "
                    + wDBRes['Reason'] + " query=" + wDBRes['Query'])
                wOBJ_DB.Close()
                return False

            self.STR_Stat['ClazList'] += 1

        #############################
        # 正常終了
        wOBJ_DB.Close()
        return True