Ejemplo n.º 1
0
def sens_calc(data, hm1):
    # cambio un valore (se si ripete devo variare anche l'altro)
    # lista valori -> prendo un valore random-> ricerco tutti quei valori nella lista e li sostituisco
    # ECCEZIONE: se tutti i valori sono uguali salto tutto
    # per comnodità varieremo solo il primo
    v = data[0]

    maxW = collectionw.count()
    data = [str(l) for l in data]
    datam = [w.replace(str(v), str(maxW)) for w in data]
    datam = [float(l) for l in datam]
    hm2 = harmonic_mean(datam)
    hmm = (hm2 / hm1)
    pw = 0.333333
    grow = (math.pow(hmm, pw) - 1) * 100
    vari = percentage(grow, hm1)
    vmin = hm1 - vari
    vmax = hm1 + vari

    return [vmin, vmax]
Ejemplo n.º 2
0
def interact(stri):
    stri = stri.decode()

    # split speak
    maxEndR = collectionw.find().sort('end', -1).limit(1)
    maxEnd = maxEndR[0]["end"]
    next = 0
    str1 = []
    aws = []
    stop = 0
    row = []
    i = 0
    i2 = 0
    hm = 0
    s1 = 0
    e = 0
    words = stri.split()

    # liv = len(words)
    for word in words:
        liv = i
        if findW(word) == 0:
            # trovo id associati alle parole se non c'è la inserisco
            collectionw.insert({
                "_id": getNextSequence(db.counters, "nodeid"),
                "word": word,
                "start": 0,
                "end": 0
            })
        str1.append(str(findW(word)).strip())
        i = +1

    str2 = list(str1)
    if deb == 1:
        pprint("coversion:" + str(str2))
    story = ''.join("=".join(str(x) for x in str1))

    #cerco story che INIZIANO con quella appena passata
    rex = "^(" + story + ").*"
    if deb == 1:
        pprint(rex)

    rows = collectiont.find({"story": {'$regex': rex}})
    # pprint(str1)
    vartemp = story
    # se non ci sono risultati diminuisco di uno l'array
    # pprint(rows.count())
    # row count
    rc = rows.count()
    # len str2

    ls2 = len(str2)

    if rc == 0:
        stop = 0
    # non va x me

    # pprint("stoppo con array "+str(str1))

    while (next != ""):

        if stop == 0:
            if deb == 1:
                pprint("meno uno")
            str2.pop(-1)
            ls2 = ls2 - 1

            story0 = ''.join("=".join(str(x) for x in str2))
            rex = "^(" + story0 + ").*"
            if deb == 1:
                pprint("cerco" + str(str2))
            rows = collectiont.find({"story": {'$regex': rex}})
            rc = rows.count()

            if rc != 0:
                i2 = -1
                stop = 2
            elif ls2 == 0:
                next = ""

                #pprint("Loop, stop:" + str(stop))

        if stop == 1:
            if deb == 1:
                pprint("ciclo armonico")
            # cerco quindi in Logic una regola tipo 1=2=0 con campo wtd (what to do) che deve essere tipo [1,4] e vuol dire che quello che è nella pos 1 della
            # mia story lo metto anche nella posizione 4 della story finale
            # scritta nel campo fstory della regola
            try:
                aws = rrule(vartemp)
                # pprint(len(aws))
                # no rule
                if len(aws) == 0:
                    # nessun risultato
                    # provo con la media armonica
                    story_list = [float(y) for y in str1]
                    # media armonica
                    hm = harmonic_mean(story_list)
                    rows = findH2(story_list, hm, liv)
                    # pprint(liv)
                    # se cmq è ancora a 0
                    if rows.count() == 0:
                        stop = 2
                    else:
                        i2 = -1
                        stop = 2

            except:
                stop = 2
                pass
        if stop == 2:
            if deb == 1:
                pprint("costruttore next")
            # test: inizio con il primo e basta.
            #pprint("inizio ciclo 2 con i2:" + str(i2))

            if i2 == 0:
                rex = "^(" + story + ").*"

                rows = collectiont.find({"story": {'$regex': rex}})
                if deb == 1:
                    pprint("RIGHE RISULTATO" + str(rows.count()))
                row = rows[0]
                next = row['next']
                #if hm != 0:
                story = row['story']
                s2 = story.split("=")
                for s in s2:
                    s = int(s)
                    w1, s1, e = findWById(s)
                    aws.append(w1)
                w2, s1, e = findWById(next)
                aws.append(w2)

            else:
                if rows.count() != 0:
                    row = rows[0]
                    next = row['next']
                    for row in rows:
                        next = row['next']
                        w3, s1, e = findWById(next)
                        aws.append(w3)
                        if e > 10:
                            next = ""
                            pprint("ESCO")
                            break
                else:
                    pprint("PAROLA NON CONOSCIUTA")
                    aws.append("Non conosco questa parola")
                    next = ""
                    answer = ' '.join(aws)
                    break

            # aggiorno la story per cercare il prossimo next
            story = story + '=' + str(next)
            rows = collectiont.find({
                "story": story
            }).sort("weight", -1)  # per il peso

            if deb == 1:
                pprint("STORY: " + story)
                pprint("ARRAY: " + str(aws))
            #nn=row['next']
            #cerco il valore END dell'ultima parola della risposta creata
            wn = findWByIdFull(next)
            #end

            if wn != '':
                end = wn['end']
            else:
                end = 0
            #use fibonacci for calulate end value
            if i2 > 0:
                endMax = maxEnd / F(i2)
            else:
                endMax = 999999

            if rows.count() == 0:
                next = ""

            if deb == 1:
                pprint("ME: " + str(endMax) + " END " + str(end))

            if row['next'] == "" or end > endMax:
                next = ""

                # next = ""
            #   pprint("nores")

            #next = ""
            # fermo il ciclo

        i2 = i2 + 1
        answer = ' '.join(aws)
    return answer
Ejemplo n.º 3
0
def trainAI(stri):
    # split speak
    words = stri.split()

    # create insert word and create link
    i = 0

    maxx = len(words)  # numero parole
    maxxr = maxx - 1
    story = ""
    idsin = ""

    for word in words:
        liv = i
        sax = 0
        if liv == 0:
            if findW(word) == 0:
                # trovo id associati alle parole altrimenti la inserisco
                _id = collectionw.insert({
                    "_id":
                    getNextSequence(db.counters, "nodeid"),
                    "word":
                    word,
                    "start":
                    1,
                    "end":
                    0
                })
                story = _id
            else:
                story = findW(word)
                # se è la prima parola della frase incremento il valore di
                # start
                collectionw.update_one({"_id": story}, {"$inc": {"start": 1}})

        elif liv != 0 and i < maxx:

            if findW(word) == 0:
                # se è l'ultima parola e non esiste

                if i == maxxr:
                    _id = collectionw.insert({
                        "_id":
                        getNextSequence(db.counters, "nodeid"),
                        "word":
                        word,
                        "start":
                        0,
                        "end":
                        1
                    })
                else:
                    _id = collectionw.insert({
                        "_id":
                        getNextSequence(db.counters, "nodeid"),
                        "word":
                        word,
                        "start":
                        0,
                        "end":
                        0
                    })

            else:
                # se è l'ultima parola ed esiste
                _id = findW(word)
                if i == maxxr:
                    collectionw.update_one({"_id": _id}, {"$inc": {"end": 1}})

            story = str(story) + "=" + str(_id)
            # da stringa a lista
            story_list = story.split("=")
            story_list = [float(y) for y in story_list]
            # media armonica
            hm = harmonic_mean(story_list)
            # calcolo la somma assoluta
            sax = nsax(story_list, 0)

            # aggiorno ultimo sin con prossimo id
            upcolNext(idsin, _id)
            # detect repetition

            # inserisco nuovo sin se non esite
            if findS(story) == 0:
                if liv == 1:
                    idsin = collectiont.insert({
                        "liv": liv,
                        "start": 1,
                        "story": story,
                        "next": "",
                        "hm": hm,
                        "sax": sax,
                        "weight": 0
                    })
                else:
                    idsin = collectiont.insert({
                        "liv": liv,
                        "start": 0,
                        "story": story,
                        "next": "",
                        "hm": hm,
                        "sax": sax,
                        "weight": 0
                    })
            else:
                idsin = findS(story)
                collectiont.update_one({"_id": idsin}, {"$inc": {"weight": 1}})
                if liv == 1:
                    collectiont.update_one({"_id": idsin},
                                           {"$inc": {
                                               "start": 1
                                           }})

            findH(story_list, story, sax, liv)
            #             print("esiste")
        i += 1
    return "ok"
Ejemplo n.º 4
0
def interact(stri):
    stri = stri.decode()

    # split speak
    next = 0
    str1 = []
    aws = []
    stop = 1
    row = []
    i = 0
    i2 = 0
    hm = 0
    s1 = 0
    e = 0
    words = stri.split()

    # liv = len(words)
    for word in words:
        liv = i
        if findW(word) == 0:
            # trovo id associati alle parole se non c'è la inserisco
            collectionw.insert({
                "_id": getNextSequence(db.counters, "nodeid"),
                "word": word,
                "start": 0,
                "end": 0
            })
        str1.append(str(findW(word)).strip())
        i = +1

    str2 = list(str1)
    pprint(str2)
    story = ''.join("=".join(str(x) for x in str1))

    #cerco story che INIZIANO con quella appena passata
    rex = "^(" + story + ").*"
    pprint(rex)
    rows = collectiont.find({"story": {'$regex': rex}})
    # pprint(str1)
    vartemp = story
    # se non ci sono risultati diminuisco di uno l'array
    # pprint(rows.count())
    # row count
    rc = rows.count()
    # len str2

    ls2 = len(str2)

    if rc == 0:
        stop = 1
    # non va x me

    # pprint("stoppo con array "+str(str1))

    while (next != ""):

        if stop == 0:
            pprint("meno uno")
            str2.pop(-1)
            ls2 = ls2 - 1

            story = ''.join("=".join(str(x) for x in str2))
            rex = "^(" + story + ").*"
            pprint("cerco" + str(str2))
            rows = collectiont.find({"story": {'$regex': rex}})
            rc = rows.count()

            if rc != 0:
                i2 = -1
                stop = 2
            elif ls2 == 0:
                next = ""

                #pprint("Loop, stop:" + str(stop))
        if stop == 2:
            pprint("costruttore next")
            # test: inizio con il primo e basta.
            #pprint("inizio ciclo 2 con i2:" + str(i2))

            if i2 == 0:
                rex = "^(" + story + ").*"

                rows = collectiont.find({"story": {'$regex': rex}})
                row = rows[0]
                next = row['next']
                if hm != 0:
                    story = row['story']
                    s2 = story.split("=")

                    for s in s2:
                        s = int(s)
                        w1, s1, e = findWById(s)
                        aws.append(w1)
                w2, s1, e = findWById(next)
                aws.append(w2)

            else:
                row = rows[0]
                next = row['next']
                for row in rows:
                    next = row['next']
                    w3, s1, e = findWById(next)
                    aws.append(w3)
                    if e > 10:
                        next = ""
                        pprint("ESCO")
                        break

            # aggiorno la story per cercare il prossimo next
            story = story + '=' + str(next)
            rows = collectiont.find({
                "story": story
            }).sort("weight", -1)  # per il peso
            #pprint("sss" + story)
            #pprint("sss" + str(aws))

            if rows.count() == 0:
                next = ""

            if row['next'] == "":
                next = ""

                # next = ""
            #   pprint("nores")

        if stop == 1:
            pprint("ciclo armonico")
            # cerco quindi in Logic una regola tipo 1=2=0 con campo wtd (what to do) che deve essere tipo [1,4] e vuol dire che quello che è nella pos 1 della
            # mia story lo metto anche nella posizione 4 della story finale
            # scritta nel campo fstory della regola
            try:
                aws = rrule(vartemp)
                # pprint(len(aws))
                # no rule
                if len(aws) == 0:
                    # nessun risultato
                    # provo con la media armonica
                    story_list = [float(y) for y in str1]
                    # media armonica
                    hm = harmonic_mean(story_list)
                    rows = findH2(story_list, hm, liv)
                    # pprint(liv)
                    # se cmq è ancora a 0
                    if rows.count() == 0:
                        stop = 2
                    else:
                        i2 = -1
                        stop = 2

            except:
                stop = 2
                pass

            #next = ""
            # fermo il ciclo

        i2 = i2 + 1
        answer = ' '.join(aws)
    return answer
Ejemplo n.º 5
0
def interact(stri):
    conn()
    # split speak
    next = 0
    str1 = []
    aws = []
    stop = 2
    i = 0
    i2 = 0
    hm = 0
    words = stri.split()

    # liv = len(words)
    for word in words:
        liv = i
        if findW(word) == 0:
            # trovo id associati alle parole se non c'è la inserisco
            conn.collectionw.insert({
                "_id":
                getNextSequence(conn.db.counters, "nodeid"),
                "word":
                word
            })
        str1.append(str(findW(word)).strip())
        i = +1

    str2 = list(str1)

    story = ''.join("=".join(str(x) for x in str1))
    rex = "^(" + story + ").*"
    rows = conn.collectiont.find({"story": {'$regex': rex}})
    # pprint(str1)
    vartemp = story
    # se non ci sono risultati diminuisco di uno l'array
    pprint(rows.count())
    # row count
    rc = rows.count()
    # len str2
    ls2 = len(str2)

    if rc == 0:
        stop = 0
    # non va x me
    while stop == 0:
        pprint("inutile")
        str2.pop(-1)
        ls2 = ls2 - 1
        story = ''.join("=".join(str(x) for x in str2))
        if rc != 0 or ls2 == 0:
            # se non c'è nemmeno un collegamento vado di regola... non so se
            # fare il contrario

            stop = 1

            # pprint("stoppo con array "+str(str1))

    while (next != ""):

        pprint("Loop, stop:" + str(stop))
        if stop == 2:
            # test: inizio con il primo e basta.
            pprint("inizio ciclo 2 con i2:" + str(i2))
            if i2 == 0:
                row = rows[0]
                next = row['next']
                if hm != 0:
                    story = row['story']
                    s2 = story.split("=")

                    for s in s2:
                        s = int(s)
                        aws.append(findWById(s))

                aws.append(findWById(next))

            else:
                for row in rows:
                    next = row['next']
                    aws.append(findWById(next))

            # aggiorno la story per cercare il prossimo next
            story = story + '=' + str(next)
            rows = conn.collectiont.find({"story": story})
            pprint("sss" + story)
            pprint("sss" + str(aws))

            if rows.count() == 0:
                next = ""

            if row['next'] == "":
                next = ""
                # next = ""
            #   pprint("nores")

        elif stop == 1:
            pprint("ciclo armonico")
            # cerco quindi in Logic una regola tipo 1=2=0 con campo wtd (what to do) che deve essere tipo [1,4] e vuol dire che quello che è nella pos 1 della
            # mia story lo metto anche nella posizione 4 della story finale
            # scritta nel campo fstory della regola
            try:
                aws = rrule(vartemp)
                pprint(len(aws))
                # no rule
                if len(aws) == 0:
                    # nessun risultato
                    # provo con la media armonica
                    story_list = [float(y) for y in str1]
                    # media armonica
                    hm = harmonic_mean(story_list)
                    rows = findH2(story_list, hm, liv)
                    # pprint(liv)
                    # se cmq è ancora a 0
                    if rows.count() == 0:
                        next = ""
                    else:
                        i2 = -1
                        stop = 2

            except:
                pass

            #next = ""
            # fermo il ciclo

        i2 = i2 + 1
        answer = ' '.join(aws)
    return answer