sdsdaab
2020-12-30 18:42
采纳率: 100%
浏览 1.6k
已采纳

出现了error: nothing to repeat at position 0,请问要怎么解决

我的目的是打开一个文本,查询里面每个单词在另外一个文本中出现的次数,最后乘于赋值再返回,但是出现了这样的error,帮我看看是什么原因

#Define a function to calculate the number of appearance
def poswordnumber(word):
    import re
    po = open('/home/1930018005/Project/positive.txt','r').read()
    return (len(re.findall(word, po)))
def neuwordnumber(word):
    import re
    nu = open('/home/1930018005/Project/neutral.txt','r').read()
    return (len(re.findall(word, nu)))
def negwordnumber(word):
    import re
    ne = open('/home/1930018005/Project/negative.txt','r').read()
    return (len(re.findall(word, ne)))
#Define a function to calculate the sentiment score if each sentence
def result1(sentence):
    #Define a function to calculate the sentiment score if each sentence
    sum1 = 0
    for i in sentence:
        posnumber = poswordnumber(i)
        sum1 += posnumber
    sum2 = 0
    for i in sentence:
        neunumber = neuwordnumber(i)
        sum2 += neunumber
    sum3 = 0
    for i in sentence:
        negnumber = negwordnumber(i)
        sum3 += negnumber
    score = sum1*0.001 + sum2*0 + sum3*-0.0022
    return score


fo = open('/home/1930018005/Project/test_x.txt','r+')
fw = open('./test_y.txt', 'w')
for line in fo:
    comma = line.find(',')
    index = line[0:comma]
    new_line = line[comma+1:]
    new_line = new_line.replace(',','')
    new_line = new_line.replace(':','')
    new_line = new_line.replace('"','')
    new_line = new_line.replace('.','')
    new_line = new_line.replace('(','')
    new_line = new_line.replace(')','')
    new_line = new_line.replace('`','')
    new_line = new_line.replace("'",'')
    input_line = new_line.lower()
    input_line = input_line.split(' ')
    result = result1(input_line)
    if result > 0:
        r = 'positive'
    elif result == 0:
        r = 'neutral'
    else:
        r = 'negative'
    fw.write(index + ','+ r + '\n')
    
fo.close()
fw.close()
    

运行之后就出现了以下问题

error                                     Traceback (most recent call last)
<ipython-input-163-f0f033565d29> in <module>
     15     input_line = new_line.lower()
     16     input_line = input_line.split(' ')
---> 17     result = result1(input_line)
     18 
     19 #     if result > 0:

<ipython-input-157-4497bd5e0ec5> in result1(sentence)
     17     sum1 = 0
     18     for i in sentence:
---> 19         posnumber = poswordnumber(i)
     20         sum1 += posnumber
     21     sum2 = 0

<ipython-input-160-c7be66550cd6> in poswordnumber(word)
      7     import re
      8     po = open('/home/1930018005/Project/positive.txt','r').read()
----> 9     return (len(re.findall(word, po)))
     10 def neuwordnumber(word):
     11     import re

/opt/anaconda3/envs/py37/lib/python3.7/re.py in findall(pattern, string, flags)
    223 
    224     Empty matches are included in the result."""
--> 225     return _compile(pattern, flags).findall(string)
    226 
    227 def finditer(pattern, string, flags=0):

/opt/anaconda3/envs/py37/lib/python3.7/re.py in _compile(pattern, flags)
    286     if not sre_compile.isstring(pattern):
    287         raise TypeError("first argument must be string or compiled pattern")
--> 288     p = sre_compile.compile(pattern, flags)
    289     if not (flags & DEBUG):
    290         if len(_cache) >= _MAXCACHE:

/opt/anaconda3/envs/py37/lib/python3.7/sre_compile.py in compile(p, flags)
    762     if isstring(p):
    763         pattern = p
--> 764         p = sre_parse.parse(p, flags)
    765     else:
    766         pattern = None

/opt/anaconda3/envs/py37/lib/python3.7/sre_parse.py in parse(str, flags, pattern)
    922 
    923     try:
--> 924         p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
    925     except Verbose:
    926         # the VERBOSE flag was switched on inside the pattern.  to be

/opt/anaconda3/envs/py37/lib/python3.7/sre_parse.py in _parse_sub(source, state, verbose, nested)
    418     while True:
    419         itemsappend(_parse(source, state, verbose, nested + 1,
--> 420                            not nested and not items))
    421         if not sourcematch("|"):
    422             break

/opt/anaconda3/envs/py37/lib/python3.7/sre_parse.py in _parse(source, state, verbose, nested, first)
    643             if not item or item[0][0] is AT:
    644                 raise source.error("nothing to repeat",
--> 645                                    source.tell() - here + len(this))
    646             if item[0][0] in _REPEATCODES:
    647                 raise source.error("multiple repeat",

error: nothing to repeat at position 0
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 八云黧 2020-12-31 11:23
    已采纳

    这个错误是因为正则表达式的pattern出问题导致的,传入findall的单词应该出现了re匹配的特殊符号却又没按照re的规则拼写。

    解决方案我觉得你要么把所有re中的pattern涉及的特殊符号都replace了,要么就用别用findall函数自己写一个。

    已采纳该答案
    评论
    解决 2 无用
    打赏 举报
  • sdsdaab 2020-12-31 12:56

    谢谢你 我做了五天的project就卡在这里 终于解决了哈哈哈哈哈哈

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题