【发布时间】:2022-01-03 17:48:50
【问题描述】:
由于我刚刚开始学习编码,请与我交谈,Python 是我学习的第一门语言。我是一个自学成才的人,目前设法在一家初创公司找到了实习机会。他们让我为他们的网站制作一个聊天机器人,我已经设法使用了一个开源聊天机器人,使用 NLTK 和 Tensorflow,使其适应我们的需求,并为其添加了一些新东西,仅此而已。也设法部署了它,现在已经成功运行了一个多月。依此类推,我正在从事第二个项目,我决定从头开始构建,因为它并不复杂,但我在 OOP 方面遇到了很多困难,无法真正理解这些功能是如何工作的,以我可以的方式制作一个单独的函数,它会做它应该做的事情,但是我返回的变量,当我在另一个函数中需要它时,我无法设法调用或使用它。有人可以帮我理解它的深度吗? 这是我从聊天机器人项目中提取的一些代码,不是我构建的代码,也绝对不是我完全知道如何构建自己的代码,而是我理解并能够根据我的需要进行编辑的代码。我现在尝试从我正在从事的新项目中构建函数结构时得到启发。
def clean_up_sentence(sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
return sentence_words
def chatbot_response(msg):
print("Message: %s" % msg)
ints = predict_class(msg, model)
if len(ints) == 0:
return "Undskyld, kan ikke forstå dig. Prøv at stille et andet spørgsmål."
res = getResponse(ints[0])
if len(res) == 0:
return "Undskyld, kan ikke forstå dig. Prøv at stille et andet spørgsmål."
return res
def getResponse(intent):
tag = intent["intent"]
for i in list_of_intents:
if i["tag"] == tag:
return random.choice(i["responses"])
def bow(sentence, words, show_details=True):
sentence_words = clean_up_sentence(sentence)
bag = [0] * len(words)
for s in sentence_words:
for i, w in enumerate(words):
if w == s:
bag[i] = 1
if show_details:
print("found in bag: %s" % w)
return np.array(bag)
def predict_class(sentence, model):
p = bow(sentence, words, show_details=False)
res = model.predict(np.array([p]))[0]
ERROR_THRESHOLD = 0.9
before_filtering = [[i, r] for i, r in enumerate(res) if r > 0.0]
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
# sort by strength of probability
before_filtering.sort(key=lambda x: x[1], reverse=True)
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
before_filtering_list = []
for r in before_filtering:
before_filtering_list.append(
{"intent": classes[r[0]], "probability": str(r[1])}
)
print("Before filtering: %s" % before_filtering_list[:3])
print("After filtering: %s" % return_list)
return return_list
这是我在我正在从事的新项目中尝试的,与聊天机器人完全无关。我的代码不起作用,我无法理解如何连接这些方法以及为什么要以这种方式连接。
man_coded_bag = []
woman_coded_bag = []
feminine_coded_words = [
"agree",
"affectionate",
"child",
"cheer",
"collab",
"commit",
"communal",
"compassion",
"connect",
"considerate",
"cooperat",
"co-operat",
"depend",
"emotiona",
"empath",
"feel",
"flatterable",
"gentle",
"honest",
"interpersonal",
"interdependen",
"interpersona",
"inter-personal",
"inter-dependen",
"inter-persona",
"kind",
"kinship",
"loyal",
"modesty",
"nag",
"nurtur",
"pleasant",
"polite",
"quiet",
"respon",
"sensitiv",
"submissive",
"support",
"sympath",
"tender",
"together",
"trust",
"understand",
"warm",
"whin",
"enthusias",
"inclusive",
"yield",
"share",
"sharin"
]
masculine_coded_words = [
"active",
"adventurous",
"aggress",
"ambitio",
"analy",
"assert",
"athlet",
"autonom",
"battle",
"boast",
"challeng",
"champion",
"compet",
"confident",
"courag",
"decid",
"decision",
"decisive",
"defend",
"determin",
"domina",
"dominant",
"driven",
"fearless",
"fight",
"force",
"greedy",
"head-strong",
"headstrong",
"hierarch",
"hostil",
"impulsive",
"independen",
"individual",
"intellect",
"lead",
"logic",
"objective",
"opinion",
"outspoken",
"persist",
"principle",
"reckless",
"self-confiden",
"self-relian",
"self-sufficien",
"selfconfiden",
"selfrelian",
"selfsufficien",
"stubborn",
"superior",
"unreasonab"
]
explanations = {
"feminine-coded": (
"This job ad uses more words that are subtly coded as feminine than words that are subtly coded as masculine (according to the research). Fortunately, the research suggests this will have only a slight effect on how appealing the job is to men, and will encourage women applicants."
),
"masculine-coded": (
"This job ad uses more words that are subtly coded as masculine than words that are subtly coded as feminine (according to the research). It risks putting women off applying, but will probably encourage men to apply."
),
"strongly feminine-coded": (
"This job ad uses more words that are subtly coded as feminine than words that are subtly coded as masculine (according to the research). Fortunately, the research suggests this will have only a slight effect on how appealing the job is to men, and will encourage women applicants."
),
"strongly masculine-coded": (
"This job ad uses more words that are subtly coded as masculine than words that are subtly coded as feminine (according to the research). It risks putting women off applying, but will probably encourage men to apply."
),
"empty": (
"This job ad doesn't use any words that are subtly coded as masculine or feminine (according to the research). It probably won't be off-putting to men or women applicants."
),
"neutral": (
"This job ad uses an equal number of words that are subtly coded as masculine and feminine (according to the research). It probably won't be off-putting to men or women applicants."
),
}
def men_coded_words(masc_bag, text):
add_text = text
man_coded_bag = masc_bag
for word in masculine_coded_words:
if word in add_text:
man_coded_bag.append(word)
return man_coded_bag
def women_coded_words(fem_bag, text):
add_text = text
woman_coded_bag = fem_bag
for word in feminine_coded_words:
if word in add_text:
woman_coded_bag.append(word)
return woman_coded_bag
def analise_and_explain_results(text, count_man, count_fem):
count_man_words = count_man
count_man_words = len(man_coded_bag)
count_woman_words = count_fem
count_woman_words = len(woman_coded_bag)
coding_score = count_woman_words - count_man_words
strengths_of_coding = ""
if coding_score == 0:
if count_man_words:
strengths_of_coding = "neutral"
else:
strengths_of_coding = "empty"
elif coding_score >= 5:
strengths_of_coding = "strongly feminine-coded"
elif coding_score > 0:
strengths_of_coding = "feminine-coded"
elif coding_score <= -5:
strengths_of_coding = "strongly masculine-coded"
else:
strengths_of_coding = "masculine-coded"
return count_man_words, count_woman_words, strengths_of_coding
def get_results(text):
user_input = text
user_input = input("add text here:").lower()
res = analise_and_explain_results(user_input, man_coded_bag, woman_coded_bag)
# i am trying to use the returned variable strengths_of_coding and is not available.
explain_results = explanations[strengths_of_coding]
return res, explain_results
get_results("random text added here, really whatever for testing purposes")
是的,所以当我调用 get_results('text') 时,我收到了这个错误,我知道它来自哪里,“名称 'strengths_of_coding' 未定义”,但我只是不知道如何访问那个变量,我已经尝试了所有我知道我可以尝试的东西......我被困在这里并且有点沮丧,因为我知道这是一个菜鸟错误,但在两周的压力之后我仍然无法掌握它和沮丧。
欢迎任何反馈。
【问题讨论】:
-
我没有看到您调用 get_results 的代码...
-
就在最后一行代码之后,我将编辑我的帖子并添加它。