Exemplo n.º 1
0
def up(goal, userID):

  friends = []
  followers = []
  flag = database.check(userID)

  if goal == 'friends_only':
    if flag == 'followers_only':
      friends = use_API(userID, 'friends')
      if friends is not None: database.select('UPDATE checked_list SET state = \'all\' WHERE userID = \'{0}\''.format(userID))
    elif flag == '***':
      friends = use_API(userID, 'friends')
      if friends is not None: database.select('UPDATE checked_list SET state = \'friends_only\' WHERE userID = \'{0}\''.format(userID))
    elif flag == 'friends_only' or flag == 'all':
      friends = database.select('SELECT followerID FROM follow_graph WHERE userID = \'{0}\''.format(userID))
      friends = tuple2list(friends)

    if friends is None: return []
    return friends

  else:
    if flag == 'friends_only':
      followers = use_API(userID, 'followers')
      if followers is not None: database.select('UPDATE checked_list SET state = \'all\' WHERE userID = \'{0}\''.format(userID))
    elif flag == '***':
      followers = use_API(userID, 'followers')
      if followers is not None: database.select('UPDATE checked_list SET state = \'followers_only\' WHERE userID = \'{0}\''.format(userID))
    elif flag == 'followers_only' or flag == 'all':
      followers = database.select('SELECT userID FROM follow_graph WHERE followerID = \'{0}\''.format(userID))
      followers = tuple2list(followers)

    if followers is None: return []
    return followers
Exemplo n.º 2
0
def update(goal, userID):

    friends = []
    followers = []

    if goal == "friends_only":
        friends = database.select(
            'select followerID from follow_graph where userID = \'' + userID +
            '\'')
        friends = tuple2list(friends)
        return friends
    elif goal == "followers_only":
        followers = database.select(
            'select userID from follow_graph where followerID = \'' + userID +
            '\'')
        followers = tuple2list(followers)
        return followers
    else:
        friends = database.select(
            'select followerID from follow_graph where userID = \'' + userID +
            '\'')
        friends = tuple2list(friends)
        followers = database.select(
            'select userID from follow_graph where followerID = \'' + userID +
            '\'')
        followers = tuple2list(followers)
        return friends, followers
Exemplo n.º 3
0
def nDCG(queryID):
  rec_list = []
  ideal_list = []

  SQL = database.select("SELECT ID, result from query where queryID = \'" + queryID + "\' and result <> 'None' order by ID")

  true = len(database.select("SELECT * from query where queryID = \'" + queryID + "\' and result = '2'"))
  half = len(database.select("SELECT * from query where queryID = \'" + queryID + "\' and result = '1'"))
  false = len(database.select("SELECT * from query where queryID = \'" + queryID + "\' and result = '0'"))

  for user in SQL: rec_list.append(user[1])

  TRUE = [2 for i in range(true)]
  HALF = [1 for i in range(half)]
  FALSE = [0 for i in range(false)]

  ideal_list = TRUE + HALF + FALSE

  return DCG(rec_list)/DCG(ideal_list), rec_list, ideal_list
Exemplo n.º 4
0
def use_API(userID, api):

  values = []
  if not check_lang(userID): return None
  print("{0} using {1} API".format(userID,api))

  return_list = acsessAPI(userID, api)
  if return_list is None: return None
  if len(return_list) == 0: return []

  if api == 'friends':
    inserted = database.select('SELECT followerID FROM follow_graph WHERE userID = \'{0}\''.format(userID))
    inserted = tuple2list(inserted)
    for i, friend in enumerate(return_list):
      if friend in inserted: continue
      sql = 'INSERT IGNORE INTO follow_graph VALUES (\'{0}\',\'{1}\')'.format(userID,friend)
      database.select(sql)
      #values.append("(\'{0}\',\'{1}\')".format(userID,friend))
      #if i % 1000 == 0:
          #sq =','.join(values)
          #database.select("INSERT IGNORE INTO follow_graph VALUES " + sq  )
          #values = []
    #sq =','.join(values)
    #database.select("INSERT IGNORE INTO follow_graph VALUES " + sq )

  else:
    inserted = database.select('SELECT userID FROM follow_graph WHERE followerID = \'{0}\''.format(userID))
    inserted = tuple2list(inserted)
    for i,follower in enumerate(return_list):
      if follower in inserted: continue
      sql = 'INSERT IGNORE INTO follow_graph VALUES (\'{0}\',\'{1}\')'.format(follower,userID)
      database.select(sql)
      #values.append("(\'{0}\', \'{1}\')".format(follower,userID))
      #if i % 1000 == 0:
          #sq =','.join(values)
          #database.select("insert into follow_graph values " + sq )
          #values = []
  #sq =','.join(values)
  #database.select("insert into follow_graph values " + sq )'''

  #print("get use length : {0}".format(len(return_list)))
  return return_list
Exemplo n.º 5
0
def check_lang(userID):

    language = database.select("SELECT language FROM checked_list WHERE userID = \'{0}\'".format(userID))
    if len(language) == 0:
        responce = twitter.show(userID)
        if responce.status_code != 200:
            if responce.status_code == 401:
                sql = 'INSERT INTO checked_list (userID, state) VALUES (\'{0}\', \'protected \')'.format(userID)
                database.select(sql)
            elif responce.status_code == 404:
                sql = 'INSERT INTO checked_list (userID, state) VALUES (\'{0}\', \'NotFound \')'.format(userID)
                database.select(sql)
            return False
        else:
            ress = json.loads(responce.text)
            language = ress["lang"]
            followers_count = ress["followers_count"]
            friends_count = ress["friends_count"]
            sql = 'INSERT INTO checked_list VALUES (\'{0}\',\'***\',\'{1}\',\'{2}\',\'{3} \')'.format(userID,language,str(friends_count),str(followers_count))
            database.select(sql)
    else: language = language[0][0]

    if language == 'ja': return True
    else: return False
Exemplo n.º 6
0
def input_database(judg, queryID, user):

  ID = database.select("SELECT MAX(ID) from query where queryID = \'" + queryID + "\'")[0][0]
  if ID is None: ID = 0
  database.insert("query", (ID + 1, user, queryID, judg_dic[judg]))
  print("{0} people checked!!".format(ID + 1))
Exemplo n.º 7
0
    ress = json.loads(responce.text)
    for res in ress:
      if len(match_list) < count: match_list.append(res["id_str"])

  return match_list


if __name__ == "__main__":

  while(1):
    print("input queryID")

    queryID = input('>>> ')

    if len(database.select("SELECT * from query where queryID = \'" + queryID + "\'")) != 0: break
    else: print("\ninput again!!\n\n")

  SQL = database.select("SELECT userID from query where queryID = \'" + queryID + "\'" + "and ID = \'0\'")

  seeds = [s[0] for s in SQL]

  print("seeds : {0}".format(seeds))

  match_user = []


  while(1):
    print("input using comparative method : {0}".format(methods[:-2]))

    method = input('>>> ')
Exemplo n.º 8
0
def personal_check(queryID, pattern, match_list, match_seeds, seeds_score):

    match_users = []

    for user in match_list:

        if len(
                database.select('SELECT * from query where userID = \'' +
                                user[0] + '\' AND queryID = \'' + queryID +
                                '\'')) != 0:
            continue

        responce = twitter.show(user[0])
        if responce.status_code != 200:
            print("Error code: %d" % (responce.status_code))
            continue

        ress = json.loads(responce.text)
        print("score : {0}".format(user[1]))
        print("\n\nhttps://twitter.com/intent/user?user_id=" + user[0])
        print(
            "screen_name:{0}\nuserID:{1}\nusername:{2}\nprofile:{3}\n".format(
                ress["screen_name"], user[0], ress["name"],
                ress["description"]))

        webbrowser_flag = False
        while (1):
            print("input true or false or half (help = h)")
            input_flag = input('>>>  ')
            '''if input_flag == "h":
        driver = webdriver.Chrome("./chromedriver")
        driver.get(":)
        webbrowser_flag = True

      elif input_flag == "y" or input_flag == "n":
        y_n[user] = input_flag
        if webbrowser_flag: driver.close()
        break'''

            if input_flag == "true":
                ID = database.select(
                    "SELECT MAX(ID) from query where queryID = \'" + queryID +
                    "\'")
                database.insert(
                    "query", (str(int(ID[0][0]) + 1), user[0], queryID, "2"))
                break
            elif input_flag == "false":
                ID = database.select(
                    "SELECT MAX(ID) from query where queryID = \'" + queryID +
                    "\'")
                database.insert(
                    "query", (str(int(ID[0][0]) + 1), user[0], queryID, "0"))
                break
            elif input_flag == "half":
                ID = database.select(
                    "SELECT MAX(ID) from query where queryID = \'" + queryID +
                    "\'")
                Mydatavase.insert(
                    "query", (str(int(ID[0][0]) + 1), user[0], queryID, "1"))
                break
            else:
                print("input again!!")

        print("{0} people checked!!".format(int(ID[0][0]) + 1))
        seeds = match_seeds[user[0]]
        if input_flag == "true":
            seeds_score = update_score(input_flag, pattern, seeds, seeds_score)
            seeds_score = init_score(user[0], seeds_score)
            match_users.append(user[0])
        else:
            seeds_score = update_score(input_flag, pattern, seeds, seeds_score)

        Mypickle.save(path, seeds_score, "seeds_score_" + queryID)

        continue_flag, next_pattern = passcheck_continue(pattern, seeds_score)
        if continue_flag is True or (continue_flag is False
                                     and input_flag == "true"):
            break

    return match_users, next_pattern, seeds_score
Exemplo n.º 9
0
        if d == "new":
            d_flag = True
            break
        elif d == "old":
            d_flag = False
            break

    while (1):
        print("input queryID")

        queryID = input('>>> ')
        break

    print("queryID is {0}\n".format(queryID))
    c_flag = database.select("SELECT * from query where queryID = \'" +
                             queryID + "\'")
    if len(c_flag) > len(seeds_list):
        from mymodule import Mypickle
        seeds_score = Mypickle.load(path, "seeds_score_" + queryID)
        _, next_pattern = recommend.passcheck_continue("0", seeds_score)
        seeds_list = [i for i in seeds_score.keys()]

    else:
        seed_score = {p: [start_score, 0, 0, 0]
                      for p in path_pattern}  #[precision, good, bad]
        seeds_score = {i: seed_score for i in seeds_list}

        import random
        next_pattern = random.choice(path_pattern)
        for seed in seeds:
            database.insert("query", (0, seed, queryID, "None"))
Exemplo n.º 10
0
                if u[0] not in users: users[u[0]] = path_set
                if p_dic[k] not in users[u[0]]: users[u[0]].append(p_dic[k])
                v = [a for a in v if u != a]

    return users.values()


if __name__ == "__main__":

    while (1):
        print("input queryID")

        queryID = input('>>> ')

        if len(
                database.select("SELECT * from query where queryID = \'" +
                                queryID + "\'")) != 0:
            break
        else:
            print("\ninput again!!\n\n")

    while (1):
        print("input using database : old or new")

        d = input('>>> ')

        if d == "new":
            import graph
            break
        elif d == "old":
            import graph_old as graph
            break
Exemplo n.º 11
0
      p_index.append(i)
      ans += len(p_index) / (i+1) * 1.0

  ans = ans /len(p_index) * 1.0

  return ans, p_index


if __name__ == "__main__":

  while(1):
    print("input queryID")

    queryID = input('>>> ')

    if len(database.select("SELECT * from query where queryID = \'" + queryID + "\'")) != 0: break
    else: print("input again!!")

  for method in methods:
    add_queryID = method + queryID
    if len(database.select("SELECT * from query where queryID = \'" + add_queryID + "\'")) == 0: continue

    '''calucrate score'''
    nDCG_score, rec_list, ideal_list = nDCG(add_queryID)
    AP_score, p_index = AP(rec_list)

    '''output nDCG'''
    texts = [str(nDCG_score), "\nrecommendation \t ideal"]
    for rec, ideal in zip(rec_list, ideal_list): texts.append("\n" + str(rec) + '\t' + str(ideal))
    f = open(path + add_queryID + '_nDCG.txt', 'w')
    f.writelines(texts)
Exemplo n.º 12
0
def update(goal, userID, seed):

  friends = []
  followers = []
  flag = database.check(userID)

  if userID == seed:
    if goal == 'all':
      if os.path.isfile(DIR + seed + '_1' + '.pickle'):
          friends = Mypickle.load(DIR, seed + '_1')
      else:
          friends = up('friends_only', userID)
      if os.path.isfile(DIR + seed + '_2' + '.pickle'):
          followers = Mypickle.load(DIR, seed + '_2')
      else:
          followers = up('followers_only', userID)
      return friends, followers
    elif goal == 'friends_only':
      if os.path.isfile(DIR + seed + '_1' + '.pickle'): return Mypickle.load(DIR, seed + '_1')
    else:
      if os.path.isfile(DIR + seed + '_2' + '.pickle'): return Mypickle.load(DIR, seed + '_2')



  if goal == 'all':
    if flag == '***':
      friends = use_API(userID, 'friends')
      if friends is not None: database.select('UPDATE checked_list SET state = \'friends_only\' WHERE userID = \'{0}\''.format(userID))
      followers = use_API(userID, 'followers')
      if followers is not None: database.select('UPDATE checked_list SET state = \'all\' WHERE userID = \'{0}\''.format(userID))
    elif flag == 'followers_only':
      friends = use_API(userID, 'friends')
      followers = database.select('SELECT userID FROM follow_graph WHERE followerID = \'{0}\''.format(userID))
      followers = tuple2list(followers)
      if friends is not None: database.select('UPDATE checked_list SET state = \'all\' WHERE userID = \'{0}\''.format(userID))
    elif flag == 'friends_only':
      followers == use_API(userID, 'followers')
      friends = database.select('SELECT followerID FROM follow_graph WHERE userID = \'{0}\''.format(userID))
      friends = tuple2list(friends)
      if followers is not None: database.select('UPDATE checked_list SET state = \'all\' WHERE userID = \'{0}\''.format(userID))
    elif flag == "all":
      friends = database.select('SELECT followerID FROM follow_graph WHERE userID = \'{0}\''.format(userID))
      friends = tuple2list(friends)
      followers = database.select('SELECT userID FROM follow_graph WHERE followerID = \'{0}\''.format(userID))
      followers = tuple2list(followers)

    if friends is None or followers is None: return [], []
    return friends, followers

  elif goal == 'friends_only':
    if flag == 'followers_only':
      friends = use_API(userID, 'friends')
      if friends is not None: database.select('UPDATE checked_list SET state = \'all\' WHERE userID = \'{0}\''.format(userID))
    elif flag == '***':
      friends = use_API(userID, 'friends')
      if friends is not None: database.select('UPDATE checked_list SET state = \'friends_only\' WHERE userID = \'{0}\''.format(userID))
    elif flag == 'friends_only' or flag == 'all':
      friends = use_API(userID, 'friends')
      friends = database.select('SELECT followerID FROM follow_graph WHERE userID = \'{0}\''.format(userID))
      friends = tuple2list(friends)

    if friends is None: return []
    return friends

  elif goal == 'followers_only':
    if flag == 'friends_only':
      followers = use_API(userID, 'followers')
      if followers is not None: database.select('UPDATE checked_list SET state = \'all\' WHERE userID = \'{0}\''.format(userID))
    elif flag == '***':
      followers = use_API(userID, 'followers')
      if followers is not None: database.select('UPDATE checked_list SET state = \'followers_only\' WHERE userID = \'{0}\''.format(userID))
    elif flag == 'followers_only' or flag == 'all':
      followers = database.select('SELECT userID FROM follow_graph WHERE followerID = \'{0}\''.format(userID))
      followers = tuple2list(followers)

    if followers is None: return []
    return followers