def extract_info(text, price):
company_name = ['BMW','AUDI','MERCEDES','TOYOTA']
car_type = ['Saloon', 'Hatchback','Sedan']
if any(word in text for word in company_name):
for word in company_name:
matchWord = text.split()
if word in matchWord:
return (word)
elif any(word in text for word in car_type):
for word in car_type:
matchWord = text.split()
if word in matchWord:
return (word)
else:
productType = re.search('car', text)
if productType is not None:
productvalue = productType.group(0)
return (productvalue)
numDoor = re.search('[0-9]-door',text)
if numDoor is not None:
matchDoor = numDoor.group(0)
return (matchDoor)
else:
return ('No match')
I am trying to return a value but, couldn't get all of the value . Although i am getting my output while printing them.
I was using a string "A low maintenance, 5-door car that's safe"
The code is fetching if the string get any of the word or match with regex.
Because a function can only return once. So after line 18 :
return productType
it return the final return of function and it will never execute after line 18. Your code after line 19 is not executing that's why you are not getting rest of result. If you want to return a bunch of output then just append in a list or use dict to save different result then return that variable at last and use that variable data as you want.
An example :
import re
def extract_info(text, price):
final_output={}
company_name = ['BMW','AUDI','MERCEDES','TOYOTA']
car_type = ['Saloon', 'Hatchback','Sedan']
if any(word in text for word in company_name):
for word in company_name:
matchWord = text.split()
if word in matchWord:
final_output['word']=word
elif any(word in text for word in car_type):
for word in car_type:
matchWord = text.split()
if word in matchWord:
final_output['word']=word
else:
productType = re.search('car', text)
if productType is not None:
final_output['product_type']=productType.group(0)
numDoor = re.search('[0-9]-door', text)
if numDoor is not None:
matchDoor = numDoor.group(0)
final_output['matchdoor']=matchDoor
else:
final_output['matchdoor']='None'
return final_output
print(extract_info("A low maintenance, 5-door car that's safe",23))
You can replace print statements with return
if numDoor is not None:
matchDoor = numDoor.group(0)
return matchDoor
else:
return 'No match'
Related
with open('words.txt', 'r') as read:
line = read.readlines()
key_list = []
def make_anagram_dict(line):
word_list = {}
for word in line:
word = word.lower()
key = ''.join(sorted(word))
if key in word_list and len(word) > 5:
word_list[key].append(word)
key_list.append(key)
else:
word_list[key] = [word]
return word_list
if __name__ == '__main__':
word_list = make_anagram_dict(line)
for words in word_list.values():
if len(words) > 1:
print('Words: {}'.format(', '.join(words)))
I.e I need it to look like this:
Key:
aeehrtw
Words:
weather
, whereat
, wreathe
I also have a problem where words in the .txt file are duplicated but one word starts with a capital, i.e Zipper and zipper. How can I have it so that it only uses one of the words?
To get the exact same output, you can try :
if __name__ == '__main__':
word_list = make_anagram_dict(line)
for key, words in word_list.items():
if len(words) > 1:
print('Key:')
print(key)
print()
print('Words:')
print('\n, '.join(words))
This is the code i've wrote so far:
def first_word(text: str) -> str:
while text.find(' ') == 0:
text = text[1:]
while text.find('.') == 0:
text = text[1:]
while text.find(' ') == 0:
text = text[1:]
while text.find('.') == 0:
text = text[1:]
if text.find('.') != -1:
text = text.split('.')
elif text.find(',') != -1:
text = text.split(',')
elif text.find(' ') != -1:
text = text.split(' ')
text = text[0]
return text
it's supposed to isolate the first word in a string, it shold delete any ".", " ", "," and keep only the word itself.
Using re, and split():
import re
ss = 'ba&*(*seball is fun'
print(''.join(re.findall(r'(\w+)', ss.split()[0])))
Output:
baseball
sentence="bl.a, bla bla"
first_word=first_word.replace(".","").replace(",","")
first_word=sentence.split(" ")[0]
print(first_word)
Or you could try a list comprehension:
sentence="bl.a, bla bla"
first_word=''.join([e for e in first_word if e not in ".,"]) #or any other punctuation
first_word=sentence.split(" ")[0]
print(first_word)
What's the quickest way (processing sense) to iterate over a list and validate them according to set flags?
Or, in other words, what's the optimal approach to filter list with statement depending on configuration.
Example below is on a list of strings and depending on flags I'd like to filter some of them.
class Validator(object):
def __init__(self, digit=False, tag=False, short=False):
self.digit = digit
self.tag = tag
self.short = short
def __call__(self, words):
good_words = []
for word in words:
if self.digit:
if word.is_digit(): continue
if self.tag:
if word[0] == "<": continue
if self.short:
if len(word) < 3: continue
good_words.append(word)
return good_words
Use of Validator
val = Validator(digit=True, short=True)
words = "An apple a day is 20 dollars a month"
print(val(words))
# ["apple", "day", "dollars", "month"]
To avoid creating a new (and potentially long) list, you can instead make a generator function with yield for each succeeded word instead of return for a whole list:
class Validator(object):
def __init__(self, digit=False, tag=False, short=False, *more_filters):
self.digit = digit
self.tag = tag
self.short = short
self.more_filters= more_filters
def __call__(self, words):
for word in words:
if self.digit:
if word.is_digit(): continue
if self.tag:
if word[0] == "<": continue
if self.short:
if len(word) < 3: continue
if any(f(word) for f in self.more_filters):
continue
yield word
Use:
other_tests = [lambda w: w.startswith('#'), lambda w: w.endswith('?')]
val = Validator(digit=True, short=True, *other_tests)
words = "An apple a day is 20 dollars a month #healty toomuch? livelong!"
print(val(words))
# apple day dollars month livelong!
Element by element approach using python filter
from itertools import filterfalse
class Validator(object):
def __init__(self, digit=False, tag=False, short=False):
self.digit = digit
self.tag = tag
self.short = short
def __call__(self, word):
if self.digit:
if word.isdigit(): return True
if self.tag:
if word[0] == "<": return True
if self.short:
if len(word) < 3: return True
return False
val = Validator(digit=True, short=True)
words = "An apple a day is 20 dollars a month".split()
assert list(filter(val, words)) == ['An', 'a', 'is', '20', 'a']
assert list(filterfalse(val, words)) == ['apple', 'day', 'dollars', 'month']
This function will search for anagrams in a list from a .txt file, I want to be able to check for anagrams and return all anagrams of the word that I input, and if it's not an anagram it will return the input, when I do it in the code below, it iterates through the for loop then ignores my first if statement and heads directly to my else statement. How can I fix this?
def find_in_dict():
input_word = input("Enter input string)")
sorted_word = ''.join(sorted(input_word.strip()))
a_word = ''.join((input_word.strip()))
word_file = open("filename", "r")
word_list = {}
for text in word_file:
simple_text = ''.join(sorted(text.strip()))
word_list.update({text.strip(): simple_text})
alist = []
for key, val in word_list.items():
if val == sorted_word:
alist.append(key)
return alist
else:
return "No words can be formed from:" + a_word
you are making a return statement in the if and else branch, that will break the for (because return invoked inside a function do exactly that, interrupt the execution and return the value) , so, don't do that, just ask if the word is equal, and in the end, check if there is none occurrences (empty list)
for text in word_file:
simple_text = ''.join(sorted(text.strip()))
word_list.update({text.strip(): simple_text})
alist = []
for key, val in word_list.items():
if val == sorted_word:
alist.append(key)
if alist == []: print("No words can be formed from: " + a_word)
I am writing a function where a user puts in text and a word and if the word is in the list, it returns the location of the word in the list.
list = ["hello", "goodbye", "name"]
def fact(txt, my_list):
text = txt.split()
for i in range(0, len(my_list)):
for j in range(0, len(text)):
if(my_list[i] == text[i]):
return my_list[i]
value = fact("hello, my name is", "name")
print(value)
However, this only seems to return none every time. Is there any particular reason it is not working?
Example:
def f(text, search):
if search in text.split():
print('Word "{}" has been found # index: {}'.format(search, text.split().index(search)))
Output:
data = 'hello world, my name is -e'
f(data, '-e')
Word "-e" has been found # index: 5
this works fine
def getword(word, text):
text = text.replace(',', '') # remove ',' by nothing
tmp = text.split(' ')
if word in tmp:
print("word: [%s] find at index %s in this text:[ %s]" % (word, tmp.index(word), text))
return tmp.index(word)
else:
print("Did not find [%s] in [%s]" % (word, text))
return -1
word = "what"
text = "Hello, I am groot, what is your name"
index = getword(word, text)