GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — PythonDataAnalysisCookbook/Chapter 8/terms_database.py at master · spacetolive/PythonDataAnalysisCookbook Expand file tree/ Copy pathterms_database.pyMore file actions 117 lines (87 loc) · 3.16 KB / Copy pathterms_database.pyFile metadata and controls117 lines (87 loc) · 3.16 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column from sqlalchemy import ForeignKey from sqlalchemy import Float from sqlalchemy import Integer from sqlalchemy import String from sqlalchemy.orm import backref from sqlalchemy.orm import relationship import os import dautil as dl from nltk.corpus import brown from sqlalchemy import func import ch8util Base = declarative_base() class Text(Base): __tablename__ = 'texts' id = Column(Integer, primary_key=True) file = Column(String, nullable=False, unique=True) terms = relationship('Term', secondary='text_terms') def __repr__(self): return "Id=%d file=%s" % (self.id, self.file) class Term(Base): __tablename__ = 'terms' id = Column(Integer, primary_key=True) word = Column(String, nullable=False, unique=True) def __repr__(self): return "Id=%d word=%s" % (self.id, self.word) class TextTerm(Base): __tablename__ = 'text_terms' text_id = Column(Integer, ForeignKey('texts.id'), primary_key=True) term_id = Column(Integer, ForeignKey('terms.id'), primary_key=True) tf_idf = Column(Float) text = relationship('Text', backref=backref('term_assoc')) term = relationship('Term', backref=backref('text_assoc')) def __repr__(self): return "text_id=%s term_id=%s" % (self.text_id, self.term_id) def populate_texts(session): if dl.db.not_empty(session, Text): # Cowardly refusing to continue return fids = brown.fileids(categories='news') for fid in fids: session.add(Text(file=fid)) session.commit() def populate_terms(session): if dl.db.not_empty(session, Term): # Cowardly refusing to continue return terms = ch8util.load_terms() for term in terms: session.add(Term(word=term)) session.commit() def populate_text_terms(session): if dl.db.not_empty(session, TextTerm): # Cowardly refusing to continue return text_ids = dl.collect.flatten(session.query(Text.id).all()) term_ids = dl.collect.flatten(session.query(Term.id).all()) tfidf = ch8util.load_tfidf() logger = dl.log_api.conf_logger(__name__) for text_id, row, in zip(text_ids, tfidf): logger.info('Processing {}'.format(text_id)) arr = row.toarray()[0] session.get_bind().execute( TextTerm.__table__.insert(), [{'text_id': text_id, 'term_id': term_id, 'tf_idf': arr[i]} for i, term_id in enumerate(term_ids) if arr[i] > 0] ) session.commit() def search(session, keywords): terms = keywords.split() fsum = func.sum(TextTerm.tf_idf) return session.query(TextTerm.text_id, fsum).\ join(Term, TextTerm).\ filter(Term.word.in_(terms)).\ group_by(TextTerm.text_id).\ order_by(fsum.desc()).all() if __name__ == "__main__": dbname = os.path.join(dl.data.get_data_dir(), 'news_terms.db') session = dl.db.create_session(dbname, Base) populate_texts(session) populate_terms(session) populate_text_terms(session) printer = dl.log_api.Printer() printer.print('id, tf_idf', search(session, 'baseball game')) |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |