C++ 通过 python 调用 TensorFlow

C++ 程序

/* g++ -o main main.cpp -I/usr/include/python2.7 -L/usr/lib64/python2.7/config -lpython2.7 #include <iostream>
#include <Python.h>
using namespace std;

extern "C"
{
#include "Python.h"
}

int test() {
    Py_Initialize();

    PyRun_SimpleString( "import sys");
    PyRun_SimpleString("sys.path.append('/home/test/tensorflow')");
    PyRun_SimpleString("print sys.path");

    PyObject * pModule = NULL;
    pModule = PyImport_ImportModule("mytest");
    if (!pModule) {
        if (PyErr_Occurred())
            PyErr_Print();
        fprintf(stderr, "Cannot find module test\n");
    }

    PyObject * pClass = NULL;
    pClass = PyObject_GetAttrString(pModule, "TFHandler");
    if (!pClass )
    {
        if (PyErr_Occurred())
            PyErr_Print();
        fprintf(stderr, "Load class TFHandler failed\n");
    }
    
    PyObject* pInstance = PyInstance_New(pClass, NULL, NULL);
    if (!pInstance) {
        fprintf(stderr, "Cant create instance./n");
        return -1;
    }
    
    PyObject *res = PyObject_CallMethod(pInstanceSecond, "getResult", "(s)", "ÄãºÃ°¡");
    if (res == NULL) {
        fprintf(stderr, "PyObject_CallMethod failed./n");
        if (PyErr_Occurred())
            PyErr_Print();
    } else {
        printf("function return value:%s\n", PyString_AsString(res));
        Py_DECREF(res);
    }

    Py_Finalize();
}
int main(int argc, char** argv)
{
    test();
    return 0;
}

python 程序代码

# -*- coding: utf-8 -*-
from __future__ import print_function
import jieba
import sys
import io
import jieba.posseg as pseg
from gensim import corpora, models, similarities
import numpy as np
import random
import warnings
warnings.filterwarnings(action='ignore',category=UserWarning,module='gensim')
warnings.filterwarnings(action='ignore',category=FutureWarning,module='gensim')

class TFHandler:
    def __init__(self):
        stop_words_path = 'stopword.txt'
        question_path = 'question.txt'
        answer_path = 'answer.txt'
        nomal_answer_path = 'nomal_answer.txt'

        self.stop_words = self.stopWordsList(stop_words_path)

        self.texts = [self.segSentence(seg, self.stop_words) for seg in io.open(question_path, 'r', encoding='utf8').readlines()]
        self.orig_txt = [seg for seg in io.open(question_path, 'r', encoding='utf8').readlines()]
        self.Answers = [seg for seg in io.open(answer_path, 'r', encoding='utf8').readlines()]
        self.NomalA = [seg for seg in io.open(nomal_answer_path, 'r', encoding='utf8').readlines()]
        self.dictionary = corpora.Dictionary(self.texts)
        self.feature_cnt = len(self.dictionary.token2id.keys())
        self.corpus = [self.dictionary.doc2bow(text) for text in self.texts]
        self.tfidf = models.TfidfModel(self.corpus)

    def stopWordsList(self, filepath):
        wlst = [w.strip() for w in io.open(filepath, 'r', encoding='utf8').readlines()]
        return wlst

    def segSentence(self, sentence, stop_words):
        stop_flag = ['x', 'c', 'u', 'd', 'p', 't', 'uj', 'f', 'r']
        sentence_seged = pseg.cut(sentence)
        outstr = []
        for word,flag in sentence_seged:
            if word not in stop_words and flag not in stop_flag:
                outstr.append(word)
        return outstr

    def getResult(self, src):
        keyword_vec = self.dictionary.doc2bow(self.segSentence(src, self.stop_words))
        index = similarities.SparseMatrixSimilarity(self.tfidf[self.corpus], num_features=self.feature_cnt)
        sim = index[self.tfidf[keyword_vec]]

        result_dic = {}
        result_dics = {}
        for i in range(len(self.orig_txt) - 1):
            result_dic[self.orig_txt[i]] = sim[i]
            result_dics[self.Answers[i]] = sim[i]

        result_dict = sorted(result_dic.items(), key=lambda d: d[1], reverse=True)
        result_dicts = sorted(result_dics.items(), key=lambda d: d[1], reverse=True)

        if result_dict[0][1] != 0.0:
            print(type(result_dicts[0][0]))
            return result_dicts[0][0].encode("utf-8")
        else:
            printf(type(self.NomalA[random.randint(0, len(self.NomalA) - 1)]))
            return self.NomalA[random.randint(0, len(self.NomalA) - 1)].encode("utf-8")

    def test(self):
        sys.stdout.write("Input question:")
        sys.stdout.flush()
        keyword = sys.stdin.readline().strip('\n')

        while keyword:
            kw_vector = self.dictionary.doc2bow(self.segSentence(keyword, self.stop_words))
            index = similarities.SparseMatrixSimilarity(self.tfidf[self.corpus], num_features=self.feature_cnt)
            sim = index[self.tfidf[kw_vector]]

            result_dic = {}
            result_dics = {}

            for i in range(len(self.orig_txt) - 1):
                result_dic[self.orig_txt[i]] = sim[i]
                result_dics[self.Answers[i]] = sim[i]
            result_dict = sorted(result_dic.items(), key=lambda d: d[1], reverse=True)
            result_dicts = sorted(result_dics.items(), key=lambda d: d[1], reverse=True)
            if result_dict[0][1] != 0.0:
                print('Result:' + result_dicts[0][0])
            else:
                print('Result:' + self.NomalA[random.randint(0, len(self.NomalA) - 1)])
            sys.stdout.write("Input question:")
            sys.stdout.flush()
            keyword = sys.stdin.readline().strip('\n')

if __name__ == "__main__":
    lc_nlp = TFHandler()

    lc_nlp.test()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值