Python text error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Python text error (/thread-13339.html) |
Python text error - Prashant93 - Oct-10-2018 Hi, I am getting error in 'TEXTS' that 'too many blank lines, and 'missing white space around operator' and also I am not able to run that program. Please help. from __future__ import unicode_literals, print_function import plac import spacy TEXTS ='Microsoft was founded by Bill Gates and Paul Allen on April 4 1975 to develop and sell BASIC interpreters for the Altair 8800. Since the 1990, it has increasingly diversified from the operating system market and has made a number of corporate acquisitions. ', @plac.annotations(model=("Model to load (needs parser and NER)", "positional", None, str)) def main(model='en_core_web_sm'): nlp = spacy.load(model) print("Loaded model '%s'" % model) print("Processing %d texts" % len(TEXTS)) for text in TEXTS: doc = nlp(text) relations = extract_currency_relations(doc) for r1, r2 in relations: print('{:<10}\t{}\t{}'.format(r1.text, r2.ent_type_, r2.text)) def extract_currency_relations(doc): # merge entities and noun chunks into one token spans = list(doc.ents) + list(doc.noun_chunks) for span in spans: span.merge() relations = [] for money in filter(lambda w : w.ent_type_ == 'PERSON', doc): if money.dep_ in ('attr', 'dobj'): subject = [w for w in money.head.lefts if w.dep_ == 'nsubj'] if subject: subject = subject[0] relations.append((subject, money)) elif money.dep_ == 'pobj' and money.head.dep_ == 'prep': relations.append((money.head.head, money)) return relations if __name__ == '__main__': plac.call(main) RE: Python text error - ichabod801 - Oct-10-2018 For multi line code use python tags, not icode tasks. I'll fix it for you this time. Those sound like errors you would get from a style checking program, not Python. How exactly are you running the code? Why is there a comma after the definition of TEXTS? RE: Python text error - Prashant93 - Oct-10-2018 I am using visual code. Let's say for this example it gives me error: Loaded model 'en_core_web_sm' and then it stops and does not gives me anything. Please help # pip install spacy # python -m spacy download en_core_web_sm import spacy # Load English tokenizer, tagger, parser, NER and word vectors nlp = spacy.load('en_core_web_sm') # Process whole documents text = (u"When Sebastian Thrun started working on self-driving cars at " u"Google in 2007, few people outside of the company took him " u"seriously. “I can tell you very senior CEOs of major American " u"car companies would shake my hand and turn away because I wasn’t " u"worth talking to,” said Thrun, now the co-founder and CEO of " u"online higher education startup Udacity, in an interview with " u"Recode earlier this week.") doc = nlp(text) # Find named entities, phrases and concepts for entity in doc.ents: print(entity.text, entity.label_) # Determine semantic similarities doc1 = nlp(u"my fries were super gross") doc2 = nlp(u"such disgusting fries") similarity = doc1.similarity(doc2) print(doc1.text, doc2.text, similarity) RE: Python text error - Prashant93 - Oct-11-2018 I tried to work on this code but it is giving me output only for money and not org and person. Any help would be greatly appreciated. Thanks. from __future__ import unicode_literals, print_function import plac import spacy TEXTS = [ 'Net income was $9.4 million compared to the prior year of $2.7 million.', 'Revenue exceeded twelve billion dollars, with a loss of $1b.', ] @plac.annotations( model=("Model to load (needs parser and NER)", "positional", None, str)) def main(model='en_core_web_sm'): nlp = spacy.load(model) print("Loaded model '%s'" % model) print("Processing %d texts" % len(TEXTS)) for text in TEXTS: doc = nlp(text) relations = extract_currency_relations(doc) for r1, r2 in relations: print('{:<10}\t{}\t{}'.format(r1.text, r2.ent_type_, r2.text)) def extract_currency_relations(doc): # merge entities and noun chunks into one token spans = list(doc.ents) + list(doc.noun_chunks) for span in spans: span.merge() relations = [] for money in filter(lambda w: w.ent_type_ == 'MONEY', doc): if money.dep_ in ('attr', 'dobj'): subject = [w for w in money.head.lefts if w.dep_ == 'nsubj'] if subject: subject = subject[0] relations.append((subject, money)) elif money.dep_ == 'pobj' and money.head.dep_ == 'prep': relations.append((money.head.head, money)) return relations relations = [] for person in filter(lambda w: w.ent_type_ == 'PERSON', doc): if person.dep_ in ('attr', 'dobj'): subject = [w for w in person.head.lefts if w.dep_ == 'nsubj'] if subject: subject = subject[0] relations.append((subject, person)) elif person.dep_ == 'pobj' and person.head.dep_ == 'prep': relations.append((person.head.head, person)) return relations relations = [] for org in filter(lambda w: w.ent_type_ == 'ORG', doc): if org.dep_ in ('attr', 'dobj'): subject = [w for w in org.head.lefts if w.dep_ == 'nsubj'] if subject: subject = subject[0] relations.append((subject, org)) elif org.dep_ == 'pobj' and org.head.dep_ == 'prep': relations.append((org.head.head, org)) return relations if __name__ == '__main__': plac.call(main) main() |