首页 > 解决方案 > 由字符而不是单词分隔的 IPython 小部件

问题描述

如下图所示,表格是由字符而不是完整的单词分隔的。

def apply(f):
    text = f
    text = re.sub(r'\W+', ' ', text)
    res = LM().check_probabilities(text, topk=20)
    l = str(res)
    paraphrase_widget = widgets.SelectMultiple(
    options=l,
    description='Paraphrases',
    disabled=False,
    layout= widgets.Layout(width='100%')
   )
    display(paraphrase_widget)
    return {"result": res}


apply("In order to")

在此处输入图像描述

标签: pythonpytorchipythongoogle-colaboratory

解决方案


这里的问题在于解包 pytorch 的预测并将这些结果以正确的格式(元组列表)传递给小部件。您可以这样做:

# Modify your widget to the following
paraphrase_widget = widgets.SelectMultiple(
      options=res['pred_topk'][2],
      description='Paraphrases',
      disabled=False,
      layout= widgets.Layout(width='100%', height="300px")
   )

这对我来说是这样的:

在此处输入图像描述


推荐阅读