Você está na página 1de 34

RECOMMENDER SYSTEM

A project report submitted in partial fulfillment of the requirements for


the award of the degree of

Bachelor of Technology
in

Computer Science & Engineering

Submitted by
SARATH S
NADISH SHAHJAHAN
KRISHNANAND V

FEDERAL INSTITUTE OF SCIENCE AND


TECHNOLOGY (FISAT)
ANGAMALY-683577
Affiliated to

MAHATMA GANDHI UNIVERSITY


Kottayam-686560

FEDERAL INSTITUTE OF SCIENCE AND TECHNOLOGY


(FISAT)
Mookkannoor(P.O), Angamaly-683577

CERTIFICATE

This is to certify that project report entitled RECOMMENDER SYSTEM


is a bonafide report of the project presented during V I th semester by SARATH
S, in partial fulfillment of the requirements for the award of the degree of Bachelor
of Technology (B.Tech) in Computer Science & Engineering.

Staff in Charge

External Examiner

Dr. Prasad J C
Head of the Department

Internal Examiner

ABSTRACT

Recommendation systems or recommendation engines form or work from


a specific type of information filtering system technique that attempts to
recommend information items (films, television, video on demand, music,
books, news, images, web pages, etc) that are likely to be of interest to the
user. Initially, recommendation technology was relatively crude. It basically
just recommended products like other items the buyer had purchased. However, the technology has become considerably more sophisticated and is now
an essential part of many online retailers economic models. The approach
uses complex algorithms to analyze large volumes of data and determine
what products that potential customers might want to buy based on their
stated preferences, online shopping choices, and the purchases of people with
similar tastes or demographics. We in our project will setup a site with a
recommendation system utilizing user based user based, item based and
pattern matching techniques. It basically just recommended products like
other items the buyer had purchased. However, the technology has become
considerably more sophisticated and is now an essential part of many online retailers economic models. So we innovated a system that recommends
products based on usage history, user profile and product schema.

ACKNOWLEDGEMENT

If the words were considered as symbols of Approval and token of Acknowledgement, then let the words pay the heralding role of expressing our
gratitude. First and Foremost we praise the God Almighty for the grace
he showered on us during out studies as well as our day-to-day life activities. Dreams never run to reality unless a lot of effort and hard work is put
into it. And no efforts bears fruit in the absence of support and guidance.
It takes a lot of effort to work your way through this. We would like to
take this chance to thank the Principal, Dr.George Issac for providing us
with such an environment, where students can explore their creative ideas.
Equally eligible is the Head of the department of computer science, Mr.
Prasad J.C for encouraging the students to make these nations true. We are
extremly greateful to the project in-charge, Mr.Jestin Joy, Mr.Sreeraj for
their valuable suggestions for the project.we are extremely thankful to both
of them for encouraging us. We are deeply indebted to our Project guide
Mr.Mahesh C, who always extended his help and support from the beginning for the successful completion of the project. We also sincerely thank
the Computer Science Engineering Faculty for providing us with invaluble
help.Overall nothing compares the support and hardwork equally put in by
our team mates. Last but not the least, we thank all our families and friends
for giving us the help, strength and courage for accomplishing the task.

SARATH S

Contents
List of Figures

List of Tables

vi

1 INTRODUCTION
1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1
1

2 RELATED WORK
2.1 Amazon Recommendation System . . . . . . . . . . . . . . . . . . .

2
2

3 DESIGN
3.1 Introduction . . . . . . . . . . .
3.2 System Archictecture . . . . . .
3.3 Logic Design . . . . . . . . . . .
3.3.1 Algorithm . . . . . . . .
3.3.2 Flow Chart . . . . . . .
3.3.3 System Requirements . .
3.3.4 Software Requirements .
3.3.5 Hardware Requirements

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

3
3
3
4
4
4
4
5
7

4 TESTING
4.1 Introduction . . . .
4.2 White-box Testing
4.3 Unit Testing . . . .
4.4 Integration Testing
4.5 System Testing . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

8
. 8
. 8
. 9
. 9
. 10

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

5 RESULTS

11

6 CONCLUSION

12

Appendices

14

A Source Code
A.1 Views . . . . . . . . .
A.1.1 Cart Views . .
A.1.2 Login Views . .
A.1.3 Product Views
A.1.4 Search Views .
A.2 Models . . . . . . . . .
A.2.1 Product Models
A.2.2 Cart Models . .
A.3 Urls . . . . . . . . . .
A.3.1 Shopper Urls .
A.3.2 Cart Urls . . .
A.3.3 Product Urls .
A.3.4 Search Urls . .

15
15
15
17
18
20
21
21
22
22
22
23
23
23

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
iii

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

B Screenshots
B.1 User Login . . . . .
B.2 User Register Page
B.3 Product Page . . .
B.4 User Cart Page . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

iv

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

25
25
25
26
26

List of Figures
3.1

Flow Chart of system . . . . . . . . . . . . . . . . . . . . . . . . . .

B.1
B.2
B.3
B.4

User Login Page .


User Register . .
Product Page . .
User Cart . . . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

5
25
25
26
26

List of Tables
3.1

Hardware Requirements . . . . . . . . . . . . . . . . . . . . . . . .

vi

Chapter 1
INTRODUCTION
1.1

Overview

As the World Wide Web becomes increasingly important as an information source


and a place to conduct commerce, Web surfers face the daunting challenge on how
to sift through a morass of information to get to the needed one. One solution
to this information overload problem is the use of recommender system. Recommender systems are changing from novelties used by a few e-commerce sites
to serious business tools that are re-shaping the world of e-commerce. Many of
the largest commerce Web sites are already using recommender systems to help
their customers find products to purchase. The products can be recommended
based on the top overall sellers on a site, based on the demographics of the customer, or based on an analysis of the past buying behaviour of the customer as
a prediction for future buying behaviour. Broadly, these techniques are part of
personalization on a site, because they help the site adapt itself to each customer.
Thus, recommender systems automate personalization on the Web, enabling individual personalization for each customer. Personalization is one of the elements
that may well improve the interaction between people and computers and offer
possibilities for establishing long-term customer relations. Now more than ever,
the promise of electronic commerce and online shopping will depend to a great
extent upon the interface and how people interact with the computer the online
shopping experience. This feature is especially important for sites that offer a large
spectrum of products and services to their customers, as is the case for grocery
retail sites. In this project, we study the application of recommendation systems
for electronic retail sites, focusing on the peculiar characteristics and requirements
of this environment. More specifically, we look into the various forms that online
recommendations may take in the eyes of the online retail shopper, the services
associated with them, and the technical ground upon which they are built. Based
on this discussion, we present a hybrid model for dynamic recommendations in
electronic retail sites and conclude by illustrating its application in a prototype
development.

Chapter 2
RELATED WORK
2.1

Amazon Recommendation System

Amazon.com uses recommendations as a targeted marketing tool in many email


campaigns and on most of its Web sites pages, including the hightraffic Amazon.com homepage. Clicking on the Your Recommendations link leads customers
to an area where they can filter their recommendations by product line and subject area, rate the recommended products, rate their previous purchases, and see
why items are recommended. our shopping cart recommendations, which offer customers product sug- gestions based on the items in their shopping cart. The feature
is similar to the impulse items in a supermarket checkout line, but our impulse
items are targeted to each customer. Amazon.com extensively uses recommendation algorithms to personalize its Web site to each customers interests. Because
existing recommendation algorithms cannot scale to Amazon.coms tens of millions
of customers and products, we developed our own. Our algorithm, item-to-item
collaborative filtering, scales to massive data sets and produces high-quality recommendations in real time. Rather than matching the user to similar customers,
item-to-item collaborative filtering match- es each of the users purchased and rated
items to similar items, then combines those similar items into a recommendation
list. To determine the most-similar match for a given item, the algorithm builds a
similar-items table by finding items that customers tend to purchase together. We
could build a product-to-product matrix by iterating through all item pairs and
computing a similarity metric for each pair. However, many product pairs have
no common customers, and thus the approach is inefficient in terms of processing
time and memory usage. Amazon.com has more than 29 million customers and
several million catalog items. Other major retailers have comparably large data
sources. While all this data offers opportunity, its also a curse, breaking the backs
of algorithms designed for data sets three orders of magnitude smaller. Almost all
existing algorithms were evaluated over small data sets. For example, the MovieLens data set contains 35,000 customers and 3,000 items, and the EachMovie data
set contains 4,000 customers and 1,600 items.

Chapter 3
DESIGN
3.1

Introduction

This Recommender System works by monitoring the users shopping activities.


There is a shopping site that enables a user to login and he can view products
that are listed in the page. Search facility is also provided for the user to search
the products they need. Also the customers can add the products to their cart. A
customer can give ratings to the product that they have viewed. In the backend the
products visited by the user are noted with the time they spent on each product
page. So when the user again purchase they will be shown products that are
recommended to them based on their previous shopping history. Also the products
that are rated high , the products with in a price range , products based on category
are clustered and then shown to the user.An interface to the administrator is also
given and thus the admin can get information about the overall user activity. A
database is used to store the informations.

3.2

System Archictecture

A system architecture or systems architecture is the conceptual model that defines


the structure, behavior, and more views of a system. An architecture description is
a formal description and representation of a system, organized in a way that supports reasoning about the structures and behaviors of the system. It can comprise
system components, the externally visible properties of those, the relationships
(e.g. the behavior) between them. The basic logic of prediction of the products
that are recommended is by k Nearest Neighbor alogrithm. Based on the user
viewing history, the products that are related to the viewing history is showned
to the user. The user history is stored in to a database and we apply kNN for the
dataset that are obtained by this database. An Interface is given to administrator
to track the user login , viewing details. For a user an Interface is also given to
view the products and to add the products in their cart. Also the user can give
ratings to the products that they have liked. A list of recommended products are
showned to them and this is based on their previous buying history. User is also
given a search facility to search among the products. Also the items are clustered
based on the price range,ratings,and category.

3.3

Logic Design

For an effective and organized throughput, the project is divided into several modules. To achieve integrity of the output and final results these modules are combined at the end. Each and every module is designed against specific goals through
every stage of the project. Logic design includes data flow diagrams, code designing, and algorithm designing.

3.3.1

Algorithm

1) Display the User Login Interface.


2) User enters the site by giving username and password.
3) If user successfully login, go to product page. Else go to register page.
3) User is allowed to browse products and add to cart.
4) User browsing history is stored in database with details like product name,user
id,time spent.
5) Based on the previous buying history, recommended products are shown.
6) Display the Admin Login Interface.
7) Admin can add or remove item.
7) Admin can view the users browsing history.

3.3.2

Flow Chart

A flowchart is a type of diagram that represents an algorithm, work-flow or process,


showing the steps as boxes of various kinds, and their order by connecting them
with arrows. This diagrammatic representation illustrates a solution model to a
given problem. Flowcharts are used in designing and documenting simple processes
or programs. Like other types of diagrams, they help visualize what is going on and
thereby help understand a process, and perhaps also find flaws, bottlenecks, and
other less-obvious features within it. There are many different types of flowcharts,
and each type has its own repertoire of boxes and notational conventions.

3.3.3

System Requirements

In the context of computer-based systems (and software), the term specification


means different things to different people. A specification can be a written document, a graphical model, a formal mathematical model, a collection of usage
scenarios, a prototype, or any combination of these. However, it is some- times
necessary to remain flexible when a specification is to be developed. For large
systems, a written document, combining natural language descriptions and graphical models may be the best approach. However, usage scenarios may be all that
are required for smaller products or systems that reside within well-understood
technical environments. The System Specification is the final work product produced by the system and requirements engineer. It serves as the foundation for
hardware engineering, soft ware engineering, database engineering, and human engineering. It describes the function and performance of a computer-based system
and the constraints that will govern its development. The specification bounds
each allocated system element.
4

Figure 3.1: Flow Chart of system

3.3.4

Software Requirements

LINUX
Linux is a Unix-like computer operating system assembled under the model
of free and open source software development and distribution. The defining
component of Linux is the Linux, an operating system kernel first released
October 5, 1991 by Linus Torvalds. Linux was originally developed as a free
operating system for Intel x86 based personal computers. It has since been
5

ported to more computer hardware platforms than any other operating system. It is a leading operating system on servers and other big iron systems
such as mainframe computers and supercomputers. More than 90 percentage of todays top 500 supercomputers run some variant of Linux, including
the 10 fastest. Linux also runs on embedded systems (devices where the
operating system is typically built into the firmware and highly tailored to
the system) such as mobile phones, tablet computers, network routers , televisions and video game consoles; the Android system in wide use on mobile
devices is built on the Linux kernel. The development of Linux is one of the
most prominent examples of free and open source. The primary difference
between Linux and many other popular contemporary operating systems is
that the Linux kernel and other components are free and open source software. Linux is not the only such operating system, although it is by far the
most widely used. Some free and open source software licenses are based
on the principle of copyleft, a kind of reciprocity: any work derived from a
copyleft piece of software must also be copyleft itself. The most common
free software license, the GNUGPL, is a form of
PYTHON
Guido van Rossum created Python in 1990. The primary impetus for Python
was to make a language that was powerful and capable and at the same time
very easy to use and learn. Guido named Python after the British comedy
program Monty Pythons Flying Circus. Perhaps this name was chosen to
communicate the groundbreaking notion that programming should be fun
and playful. Python is one of those rare languages which can claim to be
both simple and powerful.
DJANGO
Django is a free and open-source web framework, written in Python, which
follows the modelviewcontroller (MVC) architectural pattern.It is maintained
by the Django Software Foundation (DSF), an independent organization established as a non-profit.
Djangos primary goal is to ease the creation of complex, database-driven
websites. Django emphasizes reusability and pluggability of components,
rapid development, and the principle of dont repeat yourself. Python is used
throughout, even for settings, files, and data models. Django also provides
an optional administrative create, read, update and delete interface that
is generated dynamically through introspection and configured via admin
models. Despite having its own nomenclature, such as naming the callable
objects generating the HTTP responses views, the core Django framework
can be seen as an MVC architecture.It consists of an object-relational mapper
(ORM) that mediates between data models (defined as Python classes) and
a relational database (Model); a system for processing HTTP requests
with a web templating system (View) and a regular-expression-based URL
dispatcher (Controller).

MySQL Database
MySQL is (as of July 2013) the worlds second most widely used relational
database management system (RDBMS) and most widely used open-source
RDBMS. It is named after co-founder Michael Wideniuss daughter, My. The
SQL acronym stands for Structured Query Language.MySQL is a popular
choice of database for use in web applications, and is a central component
of the widely used LAMP open source web application software stack (and
other AMP stacks). LAMP is an acronym for Linux, Apache, MySQL,
Perl/PHP/Python.

3.3.5

Hardware Requirements

The hardware mostly used in this project are given in the table shown below.
No of personal computers

Processor

Pentium 4 or above

Main Memory

512 MB or above

Hard disk

60 GB or above

Base memory

1 GB

Cache memory

1 MB

Keyboard

101 keys

Table 3.1: Hardware Requirements

Chapter 4
TESTING
4.1

Introduction

Software testing is the process of evaluation a software item to detect differences


between given input and expected output. Also to assess the feature of A software
item. Testing assesses the quality of the product. Software testing is a process that
should be done during the development process. In other words software testing is
a verification and validation process. Verification is the process to make sure the
product satisfies the conditions imposed at the start of the development phase. In
other words, to make sure the product behaves the way we want it to. Validation is
the process to make sure the product satisfies the specified requirements at the end
of the development phase. In other words, to make sure the product is built as per
customer requirements. There are two basics of software testing: Black-box testing
and White-box testing. Black box testing is a testing technique that ignores the
internal mechanism of the system and focuses on the output generated against any
input and execution of the system. It is also called functional testing. White box
testing is a testing technique that takes into account the internal mechanism of a
system. It is also called structural testing and glass box testing. Black box testing
is often used for validation and white box testing is often used for verification.

4.2

White-box Testing

White-box testing (also known as clear box testing, glass box testing, transparent
box testing, and struc- tural testing) is a method of testing software that tests internal structures or workings of an application, as opposed to its functionality (i.e.
Black-box testing). In white-box testing an internal perspective of the system,
as well as programming skills, are used to design test cases. The tester chooses
inputs to exercise paths through the code and determine the appropriate outputs.
White-box testing can be applied at the unit, integration and system levels of the
software testing process. Although traditional testers tended to think of white-box
testing as being done at the unit level, it is used for integration and system testing more frequently today. It can test paths within a unit, paths between units
during integration, and between subsystems during a system level test. Though
this method of test design can uncover many errors or problems, it has the potential to miss unimplemented parts of the specification or missing requirements.The
components of this project were tested to understand the flow of data and control.

4.3

Unit Testing

In computer programming, unit testing is a method by which individual units


of source code, sets of one or more computer program modules together with
associated control data, usage procedures, and operating procedures, are tested to
determine if they arent for use. Intuitively, one can view a unit as the smallest
testable part of an application. In procedural programming a unit could be an
entire module but is more commonly an individual function or procedure. In
object-oriented programming a unit is often an entire interface, such as a class,
but could be an individual method. Unit tests are created by programmers or
occasionally by white box testers during the development process. Ideally, each
test case is independent from the others: substitutes like method stubs, mock
objects, fakes and test harnesses can be used to assist testing a module in isolation.
Unit tests are typically written and run by software developers to ensure that code
meets its design and behaves as intended. Its implementation can vary from being
very manual (pencil and paper) to being formalized as part of build automation.
The goal of unit testing is to isolate each part of the program and show that the
individual parts are correct. A unit test provides a strict, written contract that
the piece of code must satisfy. The different units in the sytem are cart app,user
login app,product app.
Product app: It is tested by adding about 1000 products and checked whether
all of the products are visible or not.
User Login app: It is tested by an unregistered user login and testing whether
the user who is not registered is able to login. And the user is registered and
then again checked whether he is able to login or not. In the testing phase,
we registered about 50 users and check whether the app is functioning or
not.
Cart app: It is tested by checking that whether a user is able to add products
to their cart. Also the functions to check cart,remove cart are also tested for
errors.So many items are added to cart and tested whether its been added
or not.

4.4

Integration Testing

Integration testing (sometimes called integration and testing, abbreviated IandT)


is the phase in software testing in which individual software modules are combined
and tested as a group. It occurs after unit testing and before validation testing.
Integration testing takes as its input modules that have been unit tested, groups
them in larger aggregates, applies tests defined in an integration test plan to those
aggregates, and delivers as its output the integrated system ready for system testing.
Bottom Up Testing is an approach to integrated testing where the lowest level
components are tested first, then used to facilitate the testing of higher level components. The process is repeated until the component at the top of the hierarchy
is tested. All the bottom or low-level modules, procedures or functions are integrated and then tested. After the integration testing of lower level integrated
modules, the next level of modules will be formed and can be used for integration
9

testing. This approach is helpful only when all or most of the modules of the same
development level are ready. This method also helps to determine the levels of
software developed and makes it easier to report testing progress in the form of a
percentage.
Top Down Testing is an approach to integrated testing where the top integrated
modules are tested and the branch of the module is tested step by step until the
end of the related module.
Sandwich Testing is an approach to combine top down testing with bottom up
testing. The Integration Testing strategy we used is the Bottom Up testing. The
individual units were each built and tested on go. Each time a new unit was ready,
it was integrated to the system and tested.

4.5

System Testing

System testing of software or hardware is testing conducted on a complete, integrated system to evaluate the systems compliance with its specified requirements.
System testing falls within the scope of black box testing, and as such, should
re- quire no knowledge of the inner design of the code or logic.As a rule, system
testing takes, as its input, all of the integrated software components that have
success- fully passed integration testing and also the software system itself integrated with any applicable hardware system(s). The purpose of integration testing
is to detect any inconsistencies between the software units that are integrated together (called assemblages) or between any of the assemblages and the hardware.
System test- ing is a more limited type of testing; it seeks to detect defects both
within the inter- assemblages and also within the system as a whole. Generally
System testing is performed on the entire system in the context of a Functional
Requirement Specification(s) (FRS) and/or a System Requirement Specification
(SRS).This project was not implemented for a commercial purpose.Hence there was
no SRS document drafting involved.However the system as a whole was tested to
check for basic requirements of the project.

10

Chapter 5
RESULTS

11

Chapter 6
CONCLUSION
We have successfully implemented a system that could recommend products to the
customers that is based on the previous buying history. In the developing process,
we have learned the basic and popular technology of recommender system . We
also implemented the item-based and user-based Collaborative Filtering Recommendation. Compare with the user-based, the relationship between item and item
is fairly static at item-based, hence the large calculation of items similarity may
compute off-line to improve the online performance. But the interest of user is
variability at user-based, users similarity calculates online in order to increase accuracy of user-based recommendation. And the growth of user generally is faster
than the growth of item, so the calculations time of user-based is more than the
item-based, hence the calculations time of user-based make a bottleneck for the
algorithm. So if the number of user is large, online performance of item-based
is better than the user based.Successful recommender system combines different
approach of recommender to improve the accuracy usually, so we will learn more
methods of recommender such as content-based and association rule-based etc. to
set up a perfect system in future.

12

Bibliography
[1] Jim McGaw, Beginning Django E-Commerce APress Special Publication, no.
2009, p. 2009.
[2] Django documentation, [online] https://docs.djangoproject.com/en/1.9/.

13

Appendices

14

Appendix A
Source Code
A.1

Views

A.1.1

Cart Views

from django . http import HttpResponse


from django . s h o r t c u t s import r en de r , g e t o b j e c t o r 4 0 4
from django . c o n t r i b . auth . models import User
from django . s h o r t c u t s import r e n d e r t o r e s p o n s e
import models
import d a t e t i m e
from . models import Cart , Item
from product . models import Product
#from u s e r l o g . models import P r o d u c t v i e w
from product . v i e w s import t i m e s p e n t
from django . http import H t t p R e s p o n s e R e d i r e c t
def a d d t o c a r t ( r e q u e s t , id=None ) :
current user = request . user
i f Viewing in r e q u e s t . s e s s i o n :
i f r e q u e s t . s e s s i o n [ Viewing ] == 1 :
timespent ( request )
i f request . user . is authenticated ( ) :
i n s t a n c e = g e t o b j e c t o r 4 0 4 ( Product , p r o d u c t i d=id )
c a r t s = Cart . o b j e c t s . a l l ( )
exists = 0
for o b j in c a r t s :
i f o b j . u s e r == c u r r e n t u s e r :
exists = 1
c u r r e n t c a r t = obj
break
i f e x i s t s == 0 :
c a r t = models . Cart ( c r e a t i o n d a t e=d a t e t i m e . d a t e t i m e . now ( ) )
cart . user = current user
cart . save ( )
current cart = cart
it exists = 0
c a r t i t e m s = Item . o b j e c t s . f i l t e r ( c a r t = c u r r e n t c a r t )
for i o b in c a r t i t e m s :
15

i f i o b . item == i n s t a n c e :
it exists = 1
break
i f i t e x i s t s == 1 :
i o b . q u a n t i t y = i o b . q u a n t i t y+1
iob . save ( )
else :
item = models . Item ( )
item . c a r t = c u r r e n t c a r t
item . p r i c e = i n s t a n c e . p r i c e
item . item = i n s t a n c e
item . q u a n t i t y = 1
item . s a v e ( )
c u r r e n t c a r t . t o t a l p r i c e += i n s t a n c e . p r i c e
c u r r e n t c a r t . count += 1
c u r r e n t c a r t . save ( )
return r e n d e r ( r e q u e s t , added . html )
else :
return H t t p R e s p o n s e R e d i r e c t ( / l o g i n / )
def r e m o v e f r o m c a r t ( r e q u e s t , id=None ) :

current user = request . user


i f Viewing in r e q u e s t . s e s s i o n :
i f r e q u e s t . s e s s i o n [ Viewing ] == 1 :
timespent ( request )
i f request . user . is authenticated ( ) :
c u r r e n t c a r t = Cart . o b j e c t s . g e t ( u s e r=c u r r e n t u s e r )
p o b j=g e t o b j e c t o r 4 0 4 ( Product , p r o d u c t i d=id )
i o b j=Item . o b j e c t s . g e t ( item=p o b j , c a r t u s e r=c u r r e n t u s e r )
i f i o b j . quantity > 1:
i o b j . q u a n t i t y = 1
i o b j . save ( )
else :
i obj . delete ()
c u r r e n t c a r t . count = c u r r e n t c a r t . count 1
c u r r e n t c a r t . t o t a l p r i c e = i o b j . item . p r i c e
c u r r e n t c a r t . save ( )
return g e t c a r t ( r e q u e s t )
else :
return H t t p R e s p o n s e R e d i r e c t ( / l o g i n / )
def g e t c a r t ( r e q u e s t ) :
current user = request . user
i f Viewing in r e q u e s t . s e s s i o n :
i f r e q u e s t . s e s s i o n [ Viewing ] == 1 :
timespent ( request )
i f request . user . is authenticated ( ) :
c a r t s = Cart . o b j e c t s . a l l ( )
exists = 0
for o b j in c a r t s :
16

i f o b j . u s e r == c u r r e n t u s e r :
exists = 1
break
i f e x i s t s == 1 :
c u r r e n t c a r t = Cart . o b j e c t s . g e t ( u s e r=c u r r e n t u s e r )
c a r t i t e m s = Item . o b j e c t s . f i l t e r ( c a r t = c u r r e n t c a r t )
i f c u r r e n t c a r t . count == 0 :
return r e n d e r ( r e q u e s t , emptycart . html )
else :
total = current cart . total price
context = {
user : current user ,
i t e m l i s t : cart items ,
total : total
}
return r e n d e r ( r e q u e s t , c a r t . html , c o n t e x t )
else :
return r e n d e r ( r e q u e s t , emptycart . html )
else :
return H t t p R e s p o n s e R e d i r e c t ( / l o g i n / )

A.1.2

Login Views

from l o g i n . forms import


from django . c o n t r i b . auth . d e c o r a t o r s import l o g i n r e q u i r e d
from django . c o n t r i b . auth import l o g o u t
from django . v i e w s . d e c o r a t o r s . c s r f import c s r f p r o t e c t
from django . s h o r t c u t s import r e n d e r t o r e s p o n s e
from django . http import H t t p R e s p o n s e R e d i r e c t
from django . t e m p l a t e import RequestContext
from product . v i e w s import t i m e s p e n t
@csrf protect
def r e g i s t e r ( r e q u e s t ) :
i f r e q u e s t . method == POST :
form = R e g i s t r a t i o n F o r m ( r e q u e s t .POST)
i f form . i s v a l i d ( ) :
u s e r = User . o b j e c t s . c r e a t e u s e r (
username=form . c l e a n e d d a t a [ username ] ,
password=form . c l e a n e d d a t a [ password1 ] ,
e m a i l=form . c l e a n e d d a t a [ e m a i l ]
)
return H t t p R e s p o n s e R e d i r e c t ( / r e g i s t e r / s u c c e s s / )
else :
form = R e g i s t r a t i o n F o r m ( )
v a r i a b l e s = RequestContext ( r e q u e s t , {
form : form
})
return r e n d e r t o r e s p o n s e (
r e g i s t r a t i o n / r e g i s t e r . html ,
variables ,
)
17

def r e g i s t e r s u c c e s s ( r e q u e s t ) :
return r e n d e r t o r e s p o n s e (
r e g i s t r a t i o n / s u c c e s s . html ,
)
def l o g o u t p a g e ( r e q u e s t ) :
i f Viewing in r e q u e s t . s e s s i o n :
i f r e q u e s t . s e s s i o n [ Viewing ] == 1 :
timespent ( request )
logout ( request )
return H t t p R e s p o n s e R e d i r e c t ( / )
@login required
def home ( r e q u e s t ) :
i f Viewing in r e q u e s t . s e s s i o n :
i f r e q u e s t . s e s s i o n [ Viewing ] == 1 :
timespent ( request )
return r e n d e r t o r e s p o n s e (
home . html ,
{ user : request . user }
)

A.1.3

Product Views

from django . http import HttpResponse


from django . s h o r t c u t s import r en de r , g e t o b j e c t o r 4 0 4
import models
from django . s h o r t c u t s import r e n d e r t o r e s p o n s e
from django . c o n t r i b . auth . models import User
from . models import Product , Productview
from d a t e t i m e import d a t e t i m e
from d a t e t i m e import t i m e d e l t a
from django . http import H t t p R e s p o n s e R e d i r e c t
VStart = d a t e t i m e . now ( )
def t i m e s p e n t ( r e q u e s t ) :
current user = request . user
i f request . user . is authenticated ( ) :
p id = r e q u e s t . s e s s i o n [ p r o d u c t i d ]
VEnd = d a t e t i m e . now ( )
fmt= %H:%M:%S
Vs = VStart . s t r f t i m e ( fmt )
Ve = VEnd . s t r f t i m e ( fmt )
View time=d a t e t i m e . s t r p t i m e ( Ve , fmt) d a t e t i m e . s t r p t i m e ( Vs , fmt )
logprodexists = 0
pvs = Productview . o b j e c t s . a l l ( )
for pv in pvs :
i f pv . u s e r == c u r r e n t u s e r :
i f pv . product . p r o d u c t i d == pi d :
logprodexists = 1
18

c u r r e n t p v = pv
c u r r e n t p r o d u c t=pv . product . p r o d u c t i d
break
i f l o g p r o d e x i s t s == 0 :
pview = models . Productview ( )
pview . u s e r = c u r r e n t u s e r
c u r r e n t p v = pview
pview . v i e w t i m e = View time
pview . product=Product . o b j e c t s . g e t ( p r o d u c t i d=p id )
else :
pview = c u r r e n t p v
pview . v i e w t i m e = pview . v i e w t i m e + View time
pview . n o o f v i s i t s = pview . n o o f v i s i t s + 1
pview . s a v e ( )
r e q u e s t . s e s s i o n [ Viewing ] = 0
def p r o d u c t d e t a i l ( r e q u e s t , id=None ) :
current user = request . user
i f request . user . is authenticated ( ) :
i n s t a n c e = g e t o b j e c t o r 4 0 4 ( Product , p r o d u c t i d=id )
context = {
name : i n s t a n c e . name ,
product id : instance . product id ,
category : instance . category ,
price : instance . price ,
image url : instance . image url ,
instance : instance ,
}
VStart = d a t e t i m e . now ( )
Viewing = 1
r e q u e s t . s e s s i o n [ Viewing ] = Viewing
request . session [ product id ] = instance . product id
return r e n d e r ( r e q u e s t , p r o d u c t d e t a i l . html , c o n t e x t )
else :
return H t t p R e s p o n s e R e d i r e c t ( / l o g i n / )
def p r o d u c t l i s t ( r e q u e s t ) :
i f Viewing in r e q u e s t . s e s s i o n :
i f r e q u e s t . s e s s i o n [ Viewing ] == 1 :
timespent ( request )

current user = request . user


i f request . user . is authenticated ( ) :
q u e r y s e t = Product . o b j e c t s . a l l ( )
context = {
o b j e c t l i s t : queryset ,
19

t i t l e : Products
}

return r e n d e r ( r e q u e s t , i n d e x . html , c o n t e x t )
else :
return H t t p R e s p o n s e R e d i r e c t ( / l o g i n / )
def p r o d u c t u p d a t e ( r e q u e s t ) :
return HttpResponse ( <h1>Update</h1> )

def p r o d u c t d e l e t e ( r e q u e s t ) :
return HttpResponse ( <h1>D e l e t e </h1> )

A.1.4

Search Views

from django . s h o r t c u t s import r e n d e r


import r e
from product . models import Product
from django . db . models import Q
from django . s h o r t c u t s import r e n d e r t o r e s p o n s e
from django . t e m p l a t e import RequestContext
from django . http import H t t p R e s p o n s e R e d i r e c t
from product . v i e w s import t i m e s p e n t
def n o r m a l i z e q u e r y (
query string ,
f i n d t e r m s=r e . compile ( r ( [ ] + ) | ( \ S+) ) . f i n d a l l ,
normspace=r e . compile ( r \ s {2 ,} ) . sub
return [ normspace ( , t [ 0 ] or t [ 1 ] ) . s t r i p ( ) for t in f i n d t e r m s ( q u e r y s t r i n g )
def g e t q u e r y ( q u e r y s t r i n g , s e a r c h f i e l d s ) :
query = None
terms = n o r m a l i z e q u e r y ( q u e r y s t r i n g )
for term in terms :
o r q u e r y = None
for f i e l d n a m e in s e a r c h f i e l d s :
q = Q({ %s i c o n t a i n s % f i e l d n a m e : term })
i f o r q u e r y i s None :
or query = q
else :
or query = or query | q
i f query i s None :
query = o r q u e r y
else :
query = query & o r q u e r y
return query
def s e a r c h ( r e q u e s t ) :
20

current user = request . user


i f Viewing in r e q u e s t . s e s s i o n :
i f r e q u e s t . s e s s i o n [ Viewing ] == 1 :
timespent ( request )
i f request . user . is authenticated ( ) :
query string =
f o u n d e n t r i e s = None
i f ( q in r e q u e s t .GET) and r e q u e s t .GET[ q ] . s t r i p ( ) :
q u e r y s t r i n g=r e q u e s t .GET[ q ]
e n t r y q u e r y=g e t q u e r y ( q u e r y s t r i n g , [ name , c a t e g o r y , ] )
f o u n d e n t r i e s=Product . o b j e c t s . f i l t e r ( e n t r y q u e r y ) . o r d e r b y
( updated )
return r e n d e r t o r e s p o n s e ( s e a r c h r e s u l t s . html ,
{
query string : query string ,
found entries : found entries
},
c o n t e x t i n s t a n c e=RequestContext ( r e q u e s t ) )
else :
return H t t p R e s p o n s e R e d i r e c t ( / l o g i n / )

A.2
A.2.1

Models
Product Models

from
future
import u n i c o d e l i t e r a l s
from django . db import models
from django . c o r e . u r l r e s o l v e r s import r e v e r s e
#
from django . c o n f import s e t t i n g s
c l a s s Product ( models . Model ) :
name = models . Ch arF iel d ( Product , max length =50)
p r o d u c t i d = models . AutoField ( p r i m a r y k e y=True )
c a t e g o r y = models . Ch arF iel d ( max length =30)
d e s c r i p t i o n=models . T e x t F i e l d ( blank=True , n u l l=True , d e f a u l t=Buy I t ! )
p r i c e = models . D e c i m a l F i e l d ( m a x d i g i t s =8, d e c i m a l p l a c e s =2)
i m a g e u r l = models . URLField ( max length =200)
updated = models . DateTimeField ( auto now=True , auto now add=F a l s e )
timestamp = models . DateTimeField ( auto now=F a l s e , auto now add=True )
def g e t a b s o l u t e u r l ( s e l f ) :
return r e v e r s e ( p r o d u c t s : d e t a i l , kwargs={ i d : s e l f . p r o d u c t i d })
c l a s s Productview ( models . Model ) :
u s e r=models . ForeignKey ( s e t t i n g s .AUTH USER MODEL, n u l l=True , blank=True )
pr oduct = models . ForeignKey ( Product )
v i e w t i m e = models . D u r a t i o n F i e l d ( )
21

n o o f v i s i t s = models . P o s i t i v e I n t e g e r F i e l d ( d e f a u l t = 0 )
c l a s s Meta ( object ) :
verbose name = ( Productview )
o r d e r i n g = ( v i e w t i m e , )

A.2.2
from
from
from
from
from

Cart Models

future
import u n i c o d e l i t e r a l s
django . u t i l s . t r a n s l a t i o n import u g e t t e x t l a z y as
django . db import models
product . models import Product
django . c o n f import s e t t i n g s

c l a s s Item ( models . Model ) :


c a r t = models . ForeignKey ( Cart , o n d e l e t e=models .CASCADE, )
item = models . ForeignKey ( Product )
q u a n t i t y=models . P o s i t i v e I n t e g e r F i e l d ( verbose name= ( q u a n t i t y ) ,
d e f a u l t =1)
c l a s s Cart ( models . Model ) :
u s e r=models . ForeignKey ( s e t t i n g s .AUTH USER MODEL,
n u l l=True , blank=True )
c r e a t i o n d a t e=models . DateTimeField ( verbose name
= ( c r e a t i o n date )
t o t a l p r i c e=models . D e c i m a l F i e l d ( m a x d i g i t s =18 , d e c i m a l p l a c e s =2,
verbose name= ( Total p r i c e ) , d e f a u l t =0)
count=models . P o s i t i v e I n t e g e r F i e l d ( verbose name= ( Item Count ) ,
d e f a u l t =0)
c l a s s Meta :
verbose name = ( c a r t )
verbose name plural = ( carts )
o r d e r i n g = ( c r e a t i o n d a t e , )

A.3
A.3.1

Urls
Shopper Urls

from django . c o n f . u r l s import u r l , i n c l u d e


from django . c o n t r i b import admin
from l o g i n . v i e w s import
urlpatterns = [
u r l ( r admin/ , admin . s i t e . u r l s ) ,
u r l ( r l o g i n / , django . c o n t r i b . auth . v i e w s . l o g i n ) ,
u r l ( r l o g o u t /$ , l o g o u t p a g e ) ,
u r l ( r a c c o u n t s / l o g i n /$ , django . c o n t r i b . auth . v i e w s . l o g i n ) ,
u r l ( r r e g i s t e r /$ , r e g i s t e r ) ,
u r l ( r r e g i s t e r / s u c c e s s /$ , r e g i s t e r s u c c e s s ) ,
u r l ( r home/$ , home ) ,
u r l ( r $ , home ) ,
u r l ( r p r o d u c t s / , i n c l u d e ( product . u r l s , namespace= p r o d u c t s ) ) ,
22

u r l ( r c a r t / , i n c l u d e ( c a r t . u r l s , namespace= c a r t s ) ) ,
u r l ( r s e a r c h / , i n c l u d e ( s e a r c h . u r l s , namespace= s e a r c h ) ) ,
]

A.3.2

Cart Urls

from django . c o n f . u r l s import u r l


from django . c o n t r i b import admin
from . v i e w s import (
get cart ,
)
urlpatterns = [
u r l ( r $ , g e t c a r t ) ,
]

A.3.3

Product Urls

from django . c o n f . u r l s import u r l


from django . c o n t r i b import admin
from c a r t . v i e w s import (
add to cart ,
remove from cart ,
)
from . v i e w s import (
product list ,
product detail ,
product update ,
product delete ,
)
urlpatterns = [
u r l ( r $ , p r o d u c t l i s t ) ,
u r l ( r ( ?P<id >\d+)/$ , p r o d u c t d e t a i l , name= d e t a i l ) ,
u r l ( r update /$ , p r o d u c t u p d a t e ) ,
u r l ( r d e l e t e /$ , p r o d u c t d e l e t e ) ,
u r l ( r ( ?P<id >\d+)/ a d d t o c a r t /$ , a d d t o c a r t , name= a d d t o c a r t ) ,
u r l ( r ( ?P<id >\d+)/ r e m o v e f r o m c a r t /$ , r e m o v e f r o m c a r t ,
name= r e m o v e f r o m c a r t ) ,
]

A.3.4

Search Urls

from django . c o n f . u r l s import u r l


from django . c o n t r i b import admin
from . v i e w s import (
search ,
)

23

urlpatterns = [
url ( r . , search )
]

24

Appendix B
Screenshots
B.1

User Login

Figure B.1: User Login Page

B.2

User Register Page

Figure B.2: User Register

25

B.3

Product Page

Figure B.3: Product Page

B.4

User Cart Page

Figure B.4: User Cart

26