acronym replacement with its value using python

2024/10/13 16:18:28

i have dictionary like that i need to replace acronyms in text with it's value in dictionary i use this code but it doesn't give me the appropriate result when i test the function using acronyms("we are gr8 and awsm") it should give me we are great and awesome

def acronyms(text):my_dict = {}with open('acronym.txt') as fileobj:for line in fileobj:key, value = line.split('\t')my_dict[key] = valueacronym_words = []words = word_tokenize(text)for word in words:for candidate_replacement in my_dict:if candidate_replacement in word:word = word.replace(candidate_replacement, my_dict[candidate_replacement])acronym_words.append(word)acronym_sentence = " ".join(acronym_words)return acronym_sentence
Answer

You can use split to break your sentence into individual words, then a simple list comprehension to replace desired values:

dct = {'gr8': 'great', 'awsm': 'awesome'}
s = "we are gr8 and awsm"def acronym(s, dct):return ' '.join([dct.get(i, i) for i in s.split()])print(acronym(s, dct))

Output:

we are great and awesome
https://en.xdnf.cn/q/118058.html

Related Q&A

Grako - How to do error handling?

How do I do error handling with Grako?EBNF (MyGrammar.ebnf):pattern = { tag | function }* ; tag = tag:( "%" name:id "%" ); function = function:("$" name:id "…

How get the softlayer storage credendials?

Im trying to get the Username,password and host IQN of a authorized Softlayer Network Storage. I used this python script, but the shell returns []import SoftLayerAPI_USERNAME = xxxAPI_KEY = yyyystorage…

dopy.manager.DoError: Unable to authenticate you

Im trying to configure a Virtual Machine(with Vagrant and Ansible), that needs a file.py to the full correct configuration of this machine (according to the book that Im studying),Im was using the Digi…

subprocess.Popen: OSError: [Errno 2] No such file or directory only on Linux

This is not a duplicate of subprocess.Popen: OSError: [Errno 13] Permission denied only on Linux as that problem occurred due to wrong permissions. That has been fixed and this is an entirely different…

Windows Theano Keras - lazylinker_ext\mod.cpp: No such file or directory

I am installing Theano and Keras follwing the How do I install Keras and Theano in Anaconda Python on Windows?, which worked fine for me with an older release before. Now I have upgraded to the latest…

Python Threading: Making the thread function return from an external signal

Could anyone please point out whats wrong with this code. I am trying to return the thread through a variable flag, which I want to control in my main thread. test27.pyimport threading import timelock …

Python join data lines together

Hello i have dataset a few thousand lines which is split in even and odd number lines and i cant find a way to join them together again in the same line. Reading the file and overwriting it is fine or …

How to undraw plot with Zelle graphics?

This is a code problem for Python 3.5.2 using John Zelles graphics.py:I have spent a good amount of time looking for the answer here, but just can not figure it out. The function undraw() exists just l…

/bin/sh: line 62: to: command not found

I have a python code in which I am calling a shell command. The part of the code where I did the shell command is:try:def parse(text_list):text = \n.join(text_list)cwd = os.getcwd()os.chdir("/var/…

Using PyMySQL with MariaDB

I have read this article about switching from MySQL to MariaDB on Ubuntu. The article claims that it would be not problem to switch between the two. Currently, I am using PyMySQL to connect to my MySQL…