Input file is:
l1 = ['Passing much less urine', 'Bleeding from any body part', 'Feeling extremely lethargic/weak', 'Excessive sleepiness/restlessness', 'Altered mental status', 'Seizure/fits', 'Breathlessness', 'Blood in sputum', 'Chest pain', 'Sound/noise in breathing', 'Drooling of saliva', 'Difficulty in opening mouth'] k=[] for n in range(0,len(l1)): e = l1[n] doc =nlp(e) for token in doc: if token.lemma_ != "-PRON-": temp = token.lemma_.lower().strip() else: temp = token.lower_ k.append(temp) cleaned_tokens = [] t = [] d = [] for token in k: li = [] if token not in stopwords and token not in punct: cleaned_tokens.append(token) li= " ".join(cleaned_tokens) t.append(li) print(t)
This code gives output:
['pass urine'] ['pass urine bleed body'] ['pass urine bleed body feel extremely lethargic weak']
But I need output should be:
["pass urine", "bleed body", "feel extremely lethargic weak"]
Suggest me how can I get this result.
Advertisement
Answer
This produces the results you want:
import spacy nlp = spacy.load("en_core_web_md") l1 = ['Passing much less urine', 'Bleeding from any body part', 'Feeling extremely lethargic/weak', 'Excessive sleepiness/restlessness', 'Altered mental status', 'Seizure/fits', 'Breathlessness', 'Blood in sputum', 'Chest pain', 'Sound/noise in breathing', 'Drooling of saliva', 'Difficulty in opening mouth'] docs = nlp.pipe(l1) t= [] for doc in docs: clean_doc = " ".join([tok.text.lower() for tok in doc if not tok.is_stop and not tok.is_punct]) t.append(clean_doc) print(t) ['passing urine', 'bleeding body', 'feeling extremely lethargic weak', 'excessive sleepiness restlessness', 'altered mental status', 'seizure fits', 'breathlessness', 'blood sputum', 'chest pain', 'sound noise breathing', 'drooling saliva', 'difficulty opening mouth']
In case you need lemma:
t= [] for doc in docs: clean_doc = " ".join([tok.lemma_.lower() for tok in doc if not tok.is_stop and not tok.is_punct]) t.append(clean_doc) print(t) ['pass urine', 'bleed body', 'feel extremely lethargic weak', 'excessive sleepiness restlessness', 'alter mental status', 'seizure fit', 'breathlessness', 'blood sputum', 'chest pain', 'sound noise breathing', 'drool saliva', 'difficulty open mouth']