简体   繁体   English

使用字典(文件)仅替换整个单词

[英]Replace only whole words using a dictionary (file)

I'm pretty new to python so please forgive me,if this is really obvious.我对 python 很陌生,所以如果这真的很明显,请原谅我。 I would like to replace all the words in file with alternative words based on a dictionary ( or dictionary file).我想用基于字典(或字典文件)的替代词替换文件中的所有单词。 I've been through a number of other posts and this code ( below) works quite well.我已经阅读了许多其他帖子,这段代码(如下)运行良好。 However, it will also replace substrings.但是,它也会替换子字符串。


text = "strings.txt"
fields = {"Cat": "Hello", "Hat": "Goodbye"}


for line in fileinput.input(text, inplace=True):
    line = line.rstrip()
    if not line:
        continue
    for f_key, f_value in fields.items():
        if f_key in line:
            line = line.replace(f_key, f_value)
    print (line)

So, caterpillar will become Helloerpillar and hatemonger will become Goodbyemonger.因此,卡特彼勒将成为 Helloerpillar,而仇恨者将成为 Goodbyemonger。 I would like substrings to be left alone, so only the full words will be replaced.我希望单独留下子字符串,因此只会替换完整的单词。 Can anyone advise me on how to do this?谁能建议我如何做到这一点?

Also.... This is less important but I have also tried to get the script to read the dictionary from a separate file.另外....这不太重要,但我也尝试让脚本从单独的文件中读取字典。 This isn't so important but it would be nice to have.这不是那么重要,但如果有它会很好。

I have tried to modify the script in the following way without luck.我试图通过以下方式修改脚本,但没有运气。

    import json
  
with open('dictionary.txt') as f:
    data = f.read()

text = "strings.txt"
fields = data

for line in fileinput.input(text, inplace=True):
    line = line.rstrip()
    if not line:
        continue
    for f_key, f_value in fields.items():
        if f_key in line:
            line = line.replace(f_key, f_value)
    print (line)

any advice that you could provide on either of these problems ( especially the first) would be greatly appreciated.您可以就这些问题(尤其是第一个)提供的任何建议将不胜感激。

Thanks谢谢

You can do:你可以做:

fields = {"Cat": "Hello", "Hat": "Goodbye"}
s = "This Is A Catterpilar Chatting With A Cat"


s =   ' '.join([fields[w] if w in fields else w for w in s.split(' ')])

output: output:

'This Is A Catterpilar Chatting With A Hello'

What the line do is split the sentence into words, replace every word by its equivalent if it is in the fields dict else leave it as is, then join all words into a new sentence.该行所做的是将句子拆分为单词,如果每个单词在fields中,则将其替换为等效单词,否则保持原样,然后将所有单词连接成一个新句子。

For simplicity sake, all words begin with a capital, but you can handle this easily aa second step.为简单起见,所有单词都以大写字母开头,但您可以在第二步轻松处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 中使用字典从文本文件中提取单词(查找和替换) - Extracting words from Text file (find and replace) using dictionary in Python 如何仅用字典或文本文件中存在的单词替换 pandas dataframe 的列? - How to replace a column of a pandas dataframe with only words that exist in the dictionary or a text file? 使用字典随机替换python中的某些单词 - Randomly replace certain words in python using a dictionary 如何使用字典替换字符串中的复合词? - How to replace compound words in a string using a dictionary? 使用字典替换DataFrame中句子中的单词 - using dictionary to replace words in sentence in DataFrame 如何使用字典映射替换字符串中的单词 - How to replace words in a string using a dictionary mapping 如何仅替换文件中的某些单词 - how to replace only certain words in a file 使用字典替换文本文件中的单词 - Replacing words in text file using a dictionary 什么是仅读取文本文件中完整单词的 python 代码(词法分析仅检测整个单词)? - What is the python code to read only full words in a text file ( lexical analysis to only detect whole words)? 使用字典和replace()函数替换字符串中的单词的问题 - Issues with replacing words in a string using a dictionary and the replace() function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM