'Text'对象没有属性'lower'

问题描述 投票:0回答:1

我正在尝试通过基于网络并使用 voila 来展示我的机器学习项目

text = widgets.Text(placeholder='Input text here')

button_send = widgets.Button(
                description='Identify gender',
                tooltip='Send',
                style={'description_width': 'initial'}
            )

output = widgets.Output()

def on_button_clicked(event):
    with output:
        clear_output()
        features = find_features(top_words, text)
        print("Naive Bayes = " + NB_classifier.classify(features))
        
button_send.on_click(on_button_clicked)

vbox_result = widgets.VBox([button_send, output])

text_0 = widgets.HTML(value="<h1>Hello!</h1>")
text_1 = widgets.HTML(value="<h2>Type anything</h2>")

vbox_text = widgets.VBox([text_0, text_1, text, vbox_result])

page = widgets.HBox([vbox_headline, vbox_text])
display(page)

输入文本后我收到以下回调: my error

jupyter-notebook
1个回答
0
投票

您的

text
对象是类型为
ipywidgets.widgets.widget_string.Text
的对象。

.lower()
方法似乎用作
find_features()
中处理的一部分,用于字符串。

为了传递到

find_features
,因此您希望字符串对象包含在
text
对象中。

您的线路应该是:

features = find_features(top_words, text.value)


如果它对任何人有帮助,这就是我用来解决这个问题的方法,因为OP并没有完全包含一个完整的、工作最少的、可重现的示例”

import ipywidgets as widgets
text = widgets.Text(placeholder='Input text here')

button_send = widgets.Button(
                description='Identify something',
                tooltip='Send',
                style={'description_width': 'initial'}
            )

output = widgets.Output()

def on_button_clicked(event):
    with output:
        #clear_output()
        features = print(text.value)
        #print("Naive Bayes = " + NB_classifier.classify(features))
        
button_send.on_click(on_button_clicked)

vbox_result = widgets.VBox([button_send, output])

text_0 = widgets.HTML(value="<h1>Hello!</h1>")
text_1 = widgets.HTML(value="<h2>Type anything</h2>")

vbox_text = widgets.VBox([text_0, text_1, text, vbox_result])

page = widgets.HBox([vbox_text])
display(page)
© www.soinside.com 2019 - 2024. All rights reserved.