Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 替换列表中的多个单词_Python_Regex_List_Nltk - Fatal编程技术网

Python 替换列表中的多个单词

Python 替换列表中的多个单词,python,regex,list,nltk,Python,Regex,List,Nltk,我读了一本使用 file_directory = 'path' my_corpus = PlaintextCorpusReader(file_directory,'.*',encoding='latin1') 我执行预处理 totalwords = my_corpus.words() docs = [my_corpus.words(f) for f in fids] docs2 = [[w.lower()for w in doc]for doc in docs]

我读了一本使用

file_directory = 'path'
my_corpus = PlaintextCorpusReader(file_directory,'.*',encoding='latin1')
我执行预处理

    totalwords = my_corpus.words()
    docs = [my_corpus.words(f) for f in fids]
    docs2 = [[w.lower()for w in doc]for doc in docs]
    docs3 = [[w for w in doc if re.search('^[a-z]+$',w)]for doc in docs2]
    from nltk.corpus import stopwords
    stop_list = stopwords.words('english')
    docs4 = [[w for w in doc if w not in stop_list]for doc in docs3]
    wordscount = [w for doc in docs4 for w in doc]
    fd_dist_total = nltk.FreqDist(wordscount)
    print(fd_dist_total.most_common(common_words))
收到的输出为

words = [('ubs', 131), ('pacific', 130), ('us', 121), ('credit', 113), ('aum', 108), ('suisse', 102), ('asia', 98), ('arm', 95)]
我想知道是否有可能用“瑞士信贷”替换102个“瑞士”值。同样,将“亚洲”替换为“亚太地区”

预期产量--

我试着用

wordscount1 = [w.replace('asia','asia-pacific').replace('suisse', 'credit-suisse') for w in wordscount]
然而,我遇到了明显的错误


请指导我。

这是一个不太明确的问题,因为我们不知道如何确保,例如,
count('suisse')>=count('credit')
。特别是,您希望:

  • 将“瑞士信贷”替换为“瑞士信贷”,保留信贷(第一个期限)
    信贷减去瑞士信贷
  • 但是,与此同时,你想用“亚太地区”取代“亚洲”,保持在太平洋地区(第二个术语)
    太平洋地区减去亚洲地区(第一个案例的相反情况)
您必须明确说明该要求。也许你的替代条款有什么不同?无论如何,作为一个起点:

words = [('ubs', 131), ('pacific', 130), ('us', 121), 
         ('credit', 113), ('aum', 108), ('suisse', 102), 
         ('asia', 98), ('arm', 95)]

d = dict(words)

for terms in (('credit', 'suisse'), ('asia', 'pacific')):
    v1 = d.get(terms[1])
    if v1:
        d['-'.join(terms)] = v1
        v0 = d.get(terms[0],0)
        d[terms[0]] = v0-v1 # how to handle zero or negative values here ?
                            # it is unclear if it should be v1-v0 or v0-v1
                            # or even abs(v0-v1) 


from pprint import pprint

pprint(d)
pprint(d.items())
制作:

sh$ python3 p.py
{'arm': 95,
 'asia': -32,    # <- notice that value
 'asia-pacific': 130,
 'aum': 108,
 'credit': 11,   # <- and this one
 'credit-suisse': 102,
 'pacific': 130,
 'suisse': 102,
 'ubs': 131,
 'us': 121}
dict_items([('us', 121), ('suisse', 102), ('aum', 108), ('arm', 95),
            ('asia-pacific', 130), ('ubs', 131), ('asia', -32),
            ('credit', 11), ('credit-suisse', 102), ('pacific', 130)])
sh$python3 p.py
{'arm':95,

“亚洲”:-32,#为什么分配给瑞士信贷
102
而不是
113
113+102
?为什么您收到的输出与您的预期输出非常不同,特别是值!!!words=[('ubs',131),('pacific',130),('us',121),('credit',113),('aum',108),('suisse',102),('asia',98),('arm',95)]。仅举一个例子@Kasra。我希望所有的瑞士都被“瑞士信贷”取代。如何确保剩余的11(113-102)“信贷”也在输出中?如何确保('credit',11),('credit-suisse',102)?同样适用于“亚太地区”。@RohanManek我已经深入更新了我的答案,以向您展示如何处理您的列表。请注意,您必须根据您的具体情况调整该示例,因为某些需求没有明确规定。现在,我让您尝试一下。
sh$ python3 p.py
{'arm': 95,
 'asia': -32,    # <- notice that value
 'asia-pacific': 130,
 'aum': 108,
 'credit': 11,   # <- and this one
 'credit-suisse': 102,
 'pacific': 130,
 'suisse': 102,
 'ubs': 131,
 'us': 121}
dict_items([('us', 121), ('suisse', 102), ('aum', 108), ('arm', 95),
            ('asia-pacific', 130), ('ubs', 131), ('asia', -32),
            ('credit', 11), ('credit-suisse', 102), ('pacific', 130)])