我已经编写了要在清除的文本上执行的代码。我已经写了代码。但是它显示出各种错误。
代码如下:
class a:
def __init__(self):
self.text = " "
def __str__(self):
return self.text
def fun1(self,b):
self.d1 = dict()
ch = '@#$%^&*)('
with open(b,'r') as g:
g = g.read()
if any(m in ch for m in g):
print("hi")
else:
w = g.split()
for i in w:
if in self.d1:
self.d1[i] += 1
else:
self.d1[i] = 1
return self.d1
def fun2(self):
b = self.fun1()
w1 = dict()
c = len(b.d1)
for w in b.d1:
w1[w] = b[d1]/c
return w1
f = a()
f.fun2(b)
引发错误fun1缺少1个必需的位置参数:b
功能fun1
需要参数b
,但您不传递它。如果不需要参数b
,请从fun1
的参数中删除它,否则在调用b
时传递参数fun1
的值。
几乎很难理解您想做什么,但是我认为您先对单词进行计数,然后针对句子词汇的长度对其进行规范化。这绝对不是最好的方法。但是现在至少它可以正常工作并且有意义:
class a: def __init__(self): self.text = " " def __str__(self): return self.text def fun1(self, filepath): d1 = {} with open(filepath,'r') as g: g = g.read() w = g.split() #example input for test #g = "I love pizza and I love chips" # fake input #w = g.split() for i in w: d1[i] = d1[i] + 1 if i in d1 else 1 return d1 def fun2(self): d1 = self.fun1(filepath='corpus.txt') w1 = {} c = len(d1) # Or did you mean by the total number of words? Which you can calculate like this: sum(d1.values())) for w in d1: w1[w] = d1[w]/c return w1 f = a() f.fun2()
以及输入示例(有注释)给出:
# {'I': 0.4, 'love': 0.4, 'pizza': 0.2, 'and': 0.2, 'chips': 0.2}