Nested list comprehension - list

Can someone please help me to convert this code (which works well) to nested list comprehension? I'm stuck. Thank you.
corrected = []
for misspelled in entries:
shortest_dist = 1
shortest_word = ''
for word in correct_spellings:
if word[0] == misspelled[0]:
ng3_misspelled = set(nltk.ngrams(misspelled, n=3))
ng3_word = set(nltk.ngrams(word, n=3))
jd = nltk.jaccard_distance(ng3_misspelled, ng3_word)
if jd < shortest_dist:
shortest_dist = jd
shortest_word = word
corrected += [shortest_word]
I have tried this but it doesn't work as needed.
[word for word in correct_spellings
for misspelled in misspelled_entries
if word[0] == misspelled[0] and
min([ nltk.jaccard_distance(set(nltk.ngrams(misspelled, n=3)), set(nltk.ngrams(word, n=3))) ])
]

Related

I am working on search and get a index not found error?

I get a regular "index not found" in django, my code is as follow, I am trying to implement search. The string passed to ReceiveStr(String) will look for a file name, in this case "views.py" based on a search for '.' and ' ' (space)
def ReceiveStr(String):
n = len(String)
print(n)
d = String.find('.')
g =[]
t=' '
res1 = []
res2 = []
res = [i for i in range(len(String)) if String.startswith(t, i)]
m = len(res)
for x in String:
for i in range(0,len(res)-1):
if res[i] < d:
print(res[i])
print (d)
res1[i] = res[i]
print (res1)
break
ReceiveStr('Where can I find views.py ')
Error at res1[i] = res[i]
I have tried everything. The other thing I can't assign a string object to a list[i] ? What is the work around ?
Please change this line
res1[i] = res[i]
to
res1 = [res[i]]
as you cannot reference the first index of a list without actually having any item in it.

Text processing to get if else type condition from a string

First of all, I am sorry about the weird question heading. Couldn't express it in one line.
So, the problem statement is,
If I am given the following string --
"('James Gosling'/jamesgosling/james gosling) , ('SUN Microsystem'/sunmicrosystem), keyword"
I have to parse it as
list1 = ["'James Gosling'", 'jamesgosling', 'jame gosling']
list2 = ["'SUN Microsystem'", 'sunmicrosystem']
list3 = [ list1, list2, keyword]
So that, if I enter James Gosling Sun Microsystem keyword it should tell me that what I have entered is 100% correct
And if I enter J Gosling Sun Microsystem keyword it should say i am only 66.66% correct.
This is what I have tried so far.
import re
def main():
print("starting")
sentence = "('James Gosling'/jamesgosling/jame gosling) , ('SUN Microsystem'/sunmicrosystem), keyword"
splited = sentence.split(",")
number_of_primary_keywords = len(splited)
#print(number_of_primary_keywords, "primary keywords length")
number_of_brackets = 0
inside_quotes = ''
inside_quotes_1 = ''
inside_brackets = ''
for n in range(len(splited)):
#print(len(re.findall('\w+', splited[n])), "length of splitted")
inside_brackets = splited[n][splited[n].find("(") + 1: splited[n].find(")")]
synonyms = inside_brackets.split("/")
for x in range(len(synonyms)):
try:
inside_quotes_1 = synonyms[x][synonyms[x].find("\"") + 1: synonyms[n].find("\"")]
print(inside_quotes_1)
except:
pass
try:
inside_quotes = synonyms[x][synonyms[x].find("'") + 1: synonyms[n].find("'")]
print(inside_quotes)
except:
pass
#print(synonyms[x])
number_of_brackets += 1
print(number_of_brackets)
if __name__ == '__main__':
main()
Output is as follows
'James Gosling
jamesgoslin
jame goslin
'SUN Microsystem
SUN Microsystem
sunmicrosyste
sunmicrosyste
3
As you can see, the last letters of some words are missing.
So, if you read this far, I hope you can help me in getting the expected output
Unfortunately, your code has a logic issue that I could not figure it out, however there might be in these lines:
inside_quotes_1 = synonyms[x][synonyms[x].find("\"") + 1: synonyms[n].find("\"")]
inside_quotes = synonyms[x][synonyms[x].find("'") + 1: synonyms[n].find("'")]
which by the way you can simply use:
inside_quotes_1 = synonyms[x][synonyms[x].find("\x22") + 1: synonyms[n].find("\x22")]
inside_quotes = synonyms[x][synonyms[x].find("\x27") + 1: synonyms[n].find("\x27")]
Other than that, you seem to want to extract the words with their indices, which you can extract them using a basic expression:
(\w+)
Then, you might want to find a simple way to locate the indices, where the words are. Then, associate each word to the desired indices.
Example Test
# -*- coding: UTF-8 -*-
import re
string = "('James Gosling'/jamesgosling/james gosling) , ('SUN Microsystem'/sunmicrosystem), keyword"
expression = r'(\w+)'
match = re.search(expression, string)
if match:
print("YAAAY! \"" + match.group(1) + "\" is a match 💚💚💚 ")
else:
print('🙀 Sorry! No matches! Something is not right! Call 911 👮')

perform join python returns a "none"

my "output" seems to populate fine however when I perform the join I get "none returned.
Any ideas?
def englishify_sentence(s):
"""English"""
words = s.lower()
words = words.split()# splits sentence into individual words
output = []
for i in range(len(words)):
if words[i].endswith("way"):
opt1 = words[i][:-3]
letters = list(opt1)#breaks given word into individual letters
translate = ("("+opt1+" or "+"w"+opt1+")")
output.append(translate)
else:
opt2 = words[i][:-2]
opt2_letters = list(opt2)#breaks given word into individual letters
first_letter = (opt2_letters.pop(-1))
opt3 = words[i][:-3]
translate2 = (first_letter+opt3)
output.append(translate2)
english = " ".join(output)#removes speech marks and creates a "real" word
print(output)
english = englishify_sentence("oday ouyay antway anway eggway")
print(english)
You forgot to return the value.
return english
Is it the print(output) that's giving you a none or the print(english)?

Balanced brackets in regexp

How can I find maximum possible pattern in a string in Matlab, which matches some expression. Example will clarify what I mean:
str = 'tan(sin*cos)';
str = 'tan(sin(exp)*cos(exp))';
I want to find the patterns, which look like tan(\w*). But I want brackets in tan to be balanced. Is there any approach to do it?
It's not possible without recusrsive regular expressions. For example, this string:
str = 'tan(tan(tan(x) + 4) + cos(x))'
would have to be regex'ed "from the inside out", something only recursion can do.
Instead, I'd just use a more practical solution:
regexprep(str, 'tan', '')
and/or split further when necessary. Or, as Ruud already suggested, just use a loop:
str{1} = 'tan(x)';
str{2} = 'tan(sin(exp)*cos(exp)) + tan(tan(x) + 4)';
S = regexp(str, 'tan\(');
match = cell(size(str));
[match{:}] = deal({});
for ii = 1:numel(str)
if ~isempty(S{ii})
for jj = 1:numel(S{ii})
open = false;
start = S{ii}(jj)+4;
for kk = start : numel(str{ii})
switch str{ii}(kk)
case '('
open = true;
case ')'
if open
open = false;
else
match{ii}{end+1} = str{ii}(start:kk-1);
break;
end
end
end
end
end
end

Find and Replace with ASP Classic

I have an function in ASP VB. and I need to replace the exact word in it. For example I have an string like "wool|silk/wool|silk". I want to replace just silk and not silk/wool.
' "|" is a devider
cur_val = "wool|silk/wool|silk"
cur_val_spl = Split("wool|silk/wool|silk", "|")
key_val = "silk"
For Each i In cur_val_spl
If i = key_val Then
cur_val = Replace(cur_val, ("|" & i), "")
cur_val = Replace(cur_val, i, "")
End If
Next
Response.Write(cur_val)
In this case my result would be "wool/wool" but what I really want is this "wool|silk/wool".
I really appreciate any help.
You should build a new string as you go
' "|" is a devider
cur_val = "wool|silk/wool|silk"
cur_val_spl = Split("wool|silk/wool|silk", "|")
result = ""
key_val = "silk"
addPipe = false
For Each i In cur_val_spl
If i <> key_val Then
if addPipe then
result = result & "|"
else
addPipe = true
end if
result = result & i
End If
Next
Response.Write(result)
you could do it with a regular expression but this is shorter
cur_val = "wool|silk/wool|silk"
Response.Write left(mid(replace("|"&cur_val&"|","|wool|","|silk|"),2),len(cur_val))
'=>silk|silk/wool|silk
Too bad you allready accepted the other answer 8>)