import spacy
import scispacy
nlp = spacy.load('en_core_sci_lg')
for text1 in df1['priceDescription']:
doc1 = nlp(text1)
for text2 in df2['Description']:
doc2 = nlp(text2)
similarity = doc1.similarity(doc2)
#print(doc1.text, doc2.text, similarity)
output = (f'{doc1.text:26} | {doc2.text:26} | {similarity:.2}')
print(output)
This is my code so how I can store the above function in the list so that I can store a list in one variable.
Output may be something like this
mylist = [for text1 in df1['priceDescription'].....]
I want output above format
can anyone help me please to solve this problem?
Thank You.
CodePudding user response:
This takes the strings you were previously printing to stdout, and instead gathers them into a list and returns the list. For future reference, you should note how little I had to change to make this work.
import spacy
def convert(nlp):
result = []
for text1 in df1['priceDescription']:
doc1 = nlp(text1)
for text2 in df2['Description']:
doc2 = nlp(text2)
similarity = doc1.similarity(doc2)
result.append(f'{doc1.text:26} | {doc2.text:26} | {similarity:.2}')
return result
nlp = spacy.load('en_core_sci_lg')
mylist = convert(nlp)
print(mylist)
It's worth pointing out that this can also be done as a generator:
import spacy
def convert(nlp):
for text1 in df1['priceDescription']:
doc1 = nlp(text1)
for text2 in df2['Description']:
doc2 = nlp(text2)
similarity = doc1.similarity(doc2)
yield f'{doc1.text:26} | {doc2.text:26} | {similarity:.2}'
nlp = spacy.load('en_core_sci_lg')
mylist = list(convert(nlp))
print(mylist)