我正在使用使用SWIG从ctcdecoder解码器生成的package (父项目)更新一个旧项目。某些方法中的某些参数类型已经更改。我被Scorer::fill_dictionary
方法困住了,该方法将const std::unordered_set<std::string>&
作为C++中的一个参数。在旧Python代码中,传递了一个bytes
列表,但是这个列表和一个集合一样不再起作用。我不知道该放哪一种。错误是
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 &'
编辑:我尝试了一个列表和一组str
和bytes
,除了上面的例外。我在Windows和WSL上使用了Python3.8。
发布于 2022-10-03 19:08:00
SWIG包括对std::unordered_set
的支持,但请注意接口奇怪地不接受Python set
对象。然而,tuple
和list
可以工作。
经过测试的例子:
test.i
%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);
演示:
>>> 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 &'
https://stackoverflow.com/questions/73900661
复制相似问题