发布
社区首页 >问答首页 >使用swig Python包装器:类型为'std::unordered_set< std::string > const &‘的参数2

使用swig Python包装器:类型为'std::unordered_set< std::string > const &‘的参数2
EN

Stack Overflow用户
提问于 2022-09-29 19:21:48
回答 1查看 181关注 0票数 0

我正在使用使用SWIG从ctcdecoder解码器生成的package (父项目)更新一个旧项目。某些方法中的某些参数类型已经更改。我被Scorer::fill_dictionary方法困住了,该方法将const std::unordered_set<std::string>&作为C++中的一个参数。在旧Python代码中,传递了一个bytes列表,但是这个列表和一个集合一样不再起作用。我不知道该放哪一种。错误是

代码语言:javascript
代码运行次数:0
复制
Traceback (most recent call last):
  File "/mnt/d/shared/speech/dsalign/STT-align/align/align.py", line 693, in <module>
    main()
  File "/mnt/d/shared/speech/dsalign/STT-align/align/align.py", line 451, in main
    create_bundle(alphabet_path, scorer_path + '.' + 'lm.binary', scorer_path + '.' + 'vocab-500000.txt', scorer_path, False, 0.931289039105002, 1.1834137581510284)
  File "/mnt/d/shared/speech/dsalign/STT-align/align/generate_package.py", line 75, in create_bundle
    scorer.fill_dictionary(words)
  File "/mnt/d/shared/speech/dsalign/STT-align/venv/lib/python3.10/site-packages/coqui_stt_ctcdecoder/swigwrapper.py", line 1269, in fill_dictionary
    return _swigwrapper.Scorer_fill_dictionary(self, vocabulary)
TypeError: in method 'Scorer_fill_dictionary', argument 2 of type 'std::unordered_set< std::string > const &'

编辑:我尝试了一个列表和一组strbytes,除了上面的例外。我在Windows和WSL上使用了Python3.8。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-03 19:08:00

SWIG包括对std::unordered_set的支持,但请注意接口奇怪地不接受Python set对象。然而,tuplelist可以工作。

经过测试的例子:

test.i

代码语言:javascript
代码运行次数:0
复制
%module test

// Code injected into wrapper
%{
#include <iostream>
#include <string>
#include <unordered_set>

// Function using the parameter type from OP error message
void func(std::unordered_set<std::string> const &string_set) {
    for(auto& s : string_set)
        std::cout << s << std::endl;
}
%}

// SWIG support for templates
%include <std_unordered_set.i>
%include <std_string.i>
// Must instantiate the specific template used
%template(string_set) std::unordered_set<std::string>;

// Tell SWIG to wrap the function
void func(std::unordered_set<std::string> const &string_set);

演示:

代码语言:javascript
代码运行次数:0
复制
>>> import test
>>> test.func(['abc','def','abc','ghi'])  # list works
abc
def
ghi
>>> s = test.string_set(['aaa','bbb','ccc','aaa','bbb'])
>>> test.func(s)
aaa
bbb
ccc
>>> s = test.string_set(('aaa','bbb','ccc','aaa','bbb')) # tuple works
>>> list(s)
['aaa', 'bbb', 'ccc']
>>> test.func({'abc','def'})  # set doesn't work
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\test.py", line 194, in func
    return _test.func(string_set)
TypeError: in method 'func', argument 1 of type 'std::unordered_set< std::string,std::hash< std::string >,std::equal_to< std::string >,std::allocator< std::string > > const &'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73900661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档