from Simhash import Simhash
from teamque1.referrences.definitions import define
from teamque1.referrences.definitions import teamscore
from teamque1.referrences.definitions import teamplayers
from teamque1.referrences.definitions import teamtable
from teamque1.referrences.definitions import teamposition
from teamque1.referrences.definitions import teamsgame
import re
import sys
import difflib   
name = input
def search_soccerteams():
    """Searches through the system finding the team stuff e.g. if you enter Arsenal, you get the score results of the last game"""
while True:        
    name = input ('Please Type your Question:  ').lower().split()
    name2 = name[:]
    for item in name2:break
    name3 = ' '.join(name2)
    import sqlite3
    id = 0
    location = ""
    conn = sqlite3.connect("team.db")
    c = conn.cursor()
    c.execute('select * from scores')
    records = c.fetchall()
    for record in records:
        id = record[0]
        location = record[15]
        if id == name3:
            print (name3.capitalize(),':' '\n',location)
            break
    else:
        for item in name2:        
            def FindName1(name,list1):
                """ searches for a pattern in the list, returns the complete list entry. Is case sensitive. May return incomplete value if ' is in the list value"""                                
                if name in list1:
                    return name
                else:
                    string1 = str(list1).replace('"',"'")
                    pattern = re.compile("[']"+name+"[^']*")
                    match = re.search(pattern,string1)
                    if match:
                        if match.group().lstrip("'") in list1:
                            return match.group().lstrip("'")
                        else:
                            print( 'Fail %s' %match.group().lstrip("'"))
                            return None
                    else:
                        return ' '.join(name2) + '\n' 'Result.'            

            this = ' '.join(name2) 
            text1 = this
            text2 = FindName1(this,define)
            simhash1 = Simhash(text1)
            simhash2 = Simhash(text2)
            similarity = simhash1.similarity(simhash2)
            if similarity < 100:
                this = ' '.join(name2)[:-1]
                text2 = this.split()[:-1]
                text3 = ' '.join(text2)
                text4 = FindName1(text3,teamscore)
                simhash1 = Simhash(text3)
                simhash2 = Simhash(text4)
                similarity = simhash1.similarity(simhash2)
                if similarity == 100:
                    name3 = ' '.join(name2[-1:])               
                    for item in name3:
                        if similarity == 100:
                            import sqlite3
                            id = 0
                            location = ""
                            conn = sqlite3.connect("teams.db")
                            c = conn.cursor()
                            c.execute('select * from scores')
                            records = c.fetchall()
                            for record in records:
                                id = record[0]
                                location = record[10]
                                if id == name3:
                                    print ('\n''Your team details:' , FindName1(this,define))
                                    print ('Score of:', name3.capitalize(),":",'\n', location )                                
                                    exit = input("Press 1 to exit: ")# or press close to exit
                                    while True:
                                        if int(exit) == 1:
                                            sys.exit();
                                            print('Please Enter Your Next Question: ')
            this = ' '.join(name2) 
            text1 = this
            text2 = FindName1(this,define)
            simhash1 = Simhash(text1)
            simhash2 = Simhash(text2)
            similarity = simhash1.similarity(simhash2)
            if similarity < 100:
                this = ' '.join(name2)[:-1]
                text2 = this.split()[:-1]
                text3 = ' '.join(text2)
                text4 = FindName1(text3,teamstable)
                simhash1 = Simhash(text3)
                simhash2 = Simhash(text4)
                similarity = simhash1.similarity(simhash2)
                if similarity == 100:
                    name3 = ' '.join(name2[-1:])               
                    for item in name3:
                        if similarity == 100:
                            import sqlite3
                            id = 0
                            location = ""
                            conn = sqlite3.connect("teams.db")
                            c = conn.cursor()
                            c.execute('select * from position')
                            records = c.fetchall()
                            for record in records:
                                id = record[0]
                                location = record[10]
                                if id == name3:
                                    print ('\n''Your team details:' , FindName1(this,define))
                                    print ('Table position of:', name3.capitalize(),":",'\n', location )                                
                                    exit = input("Press 1 to exit: ")# or press close to exit
                                    while True:
                                        if int(exit) == 1:
                                            sys.exit();
                                            print('Please Enter Your Next Question: ')
            this = ' '.join(name2) 
            text1 = this
            text2 = FindName1(this,define)
            simhash1 = Simhash(text1)
            simhash2 = Simhash(text2)
            similarity = simhash1.similarity(simhash2)
            if similarity < 100:
                this = ' '.join(name2)[:-1]
                text2 = this.split()[:-1]
                text3 = ' '.join(text2)
                text4 = FindName1(text3,teamsgame)
                simhash1 = Simhash(text3)
                simhash2 = Simhash(text4)
                similarity = simhash1.similarity(simhash2)
                if similarity == 100:
                    name3 = ' '.join(name2[-1:])               
                    for item in name3:
                        if similarity == 100:
                            import sqlite3
                            id = 0
                            location = ""
                            conn = sqlite3.connect("teams.db")
                            c = conn.cursor()
                            c.execute('select * from games')
                            records = c.fetchall()
                            for record in records:
                                id = record[0]
                                location = record[10]
                                if id == name3:
                                    print ('\n''Your team details:' , FindName1(this,define))
                                    print ('The next game for:', name3.capitalize(),":",'\n', location )                                
                                    exit = input("Press 1 to exit: ")# or press close to exit
                                    while True:
                                        if int(exit) == 1:
                                            sys.exit();
                                            print('Please Enter Your Next Question: ')
            this = ' '.join(name2) 
            text1 = this
            text2 = FindName1(this,define)
            simhash1 = Simhash(text1)
            simhash2 = Simhash(text2)
            similarity = simhash1.similarity(simhash2)
            if similarity < 100:
                this = ' '.join(name2)[:-1]
                text2 = this.split()[:-1]
                text3 = ' '.join(text2)
                text4 = FindName1(text3,teamplayers)
                simhash1 = Simhash(text3)
                simhash2 = Simhash(text4)
                similarity = simhash1.similarity(simhash2)
                if similarity == 100:
                    name3 = ' '.join(name2[-1:])               
                    for item in name3:
                        if similarity == 100:
                            import sqlite3
                            id = 0
                            location = ""
                            conn = sqlite3.connect("teams.db")
                            c = conn.cursor()
                            c.execute('select * from players')
                            records = c.fetchall()
                            for record in records:
                                id = record[0]
                                location = record[10]
                                if id == name3:
                                    print ('\n''Your team details:' ,    FindName1(this,define))
                                    print ('Team players for:', name3.capitalize(),":",'\n', location )                                
                                    exit = input("Press 1 to exit: ")# or press close to exit
                                    while True:
                                        if int(exit) == 1:
                                            sys.exit();
                                            print('Please Enter Your Next Question: ')

I also have other modules for other teams such as NBA, Hockey,Rugby etc. Every time I run the module separately, it works, but I need a way to import into a main to allow a user to enter any key word such as 'Chicago Bulls players' and the module runs or 'Arsenal score' and the module runs from the main. The modules contain quite a long detail and that is why I have separated the long code into these modules. I have also put them into packages.Please help

Sorry but the code is not good at all.
Now you have a while True loop start at line 14 with a lot of code in it.
You have to use function or class when code get longer.

Look at code in first part here
See how function get_name() show_name() process_name() are short and give a hint what they do,this make code easy to test and read.

A single function should pretty much always be less than 15 lines,and even that is pushing it.
In your code you have all code in global space,wish make it hard to read and test.

Edited 2 Years Ago by snippsat

Also, functions Click Here make it easy to test each module individually, and to see things that may be burried in a larger body of code like the duplicate similarity == 100 test here

                if similarity == 100:
                    name3 = ' '.join(name2[-1:])               
                    for item in name3:
                        if similarity == 100:

Edited 2 Years Ago by woooee

Wow, Got it! So how do I edit this post, am having trouble doing this

Edited 2 Years Ago by maurice.waka

This question has already been answered. Start a new discussion instead.