Ejemplo n.º 1
0
    def test_get_vocabulary_with_multiple_text(self, client):
        text = Text()
        text.text = "Um simples teste"
        db.session.add(text)
        db.session.flush()

        text = Text()
        text.text = "Pytest é a melhor biblioteca de testes"
        db.session.add(text)
        db.session.flush()

        text = Text()
        text.text = "Eu estou utilizando Flask framework"
        db.session.add(text)
        db.session.flush()

        db.session.commit()

        response = client.get(url_for("api.isolated_vocabulary_api"))
        assert response.status_code == HTTPStatus.OK
        assert response.get_json() == {
            "vocabulary": [
                "simples",
                "teste",
                "pytest",
                "melhor",
                "biblioteca",
                "testes",
                "estou",
                "utilizando",
                "flask",
                "framework",
            ]
        }
Ejemplo n.º 2
0
def text(section):
    sections = ['home', 'quem-somos', 'faq']
    if section not in sections:
        return error_response('Seção não existente', 400)

    text = Text.get_from_section(section)

    if request.method == 'GET':
        if text == None:
            return error_response('Seção não existente', 400)
        return {"body": text.body}, 200

    if is_valid_admin(request):
        if request.method == 'POST':
            if text != None:
                return error_response('Seção já existente', 400)
            text = Text.add(section, request.get_json()['body'])
            if text == None:
                return error_response('Falha na inserção', 500)
            return {"body": text.body}, 200

        elif request.method == 'PUT':
            if text == None:
                return error_response('Seção não existente', 400)
            text = Text.update_body(section, request.get_json()['body'])
            if text == None:
                return error_response('Falha na atualização', 500)
            return {"body": text.body}, 200
    else:
        return error_response('Permissão negada', 401)
Ejemplo n.º 3
0
    def test_get_frequency_distribution_multiple_texts(self, client):
        text = Text()
        text.text = "Um simples teste"
        db.session.add(text)
        db.session.flush()

        text = Text()
        text.text = "Pytest Python é a melhor biblioteca de testes do Python"
        db.session.add(text)
        db.session.flush()

        text = Text()
        text.text = "Eu estou utilizando a linguagem Flask framework"

        db.session.add(text)
        db.session.flush()

        db.session.commit()

        response = client.get(url_for("api.n_gram_frequency_distribution_api"))

        assert response.status_code == HTTPStatus.OK

        assert response.get_json() == {
            "frequency": {
                "text1": [1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                "text2": [0, 1, 1, 1, 1, 1, 0, 0, 0, 0],
                "text3": [0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
            }
        }
Ejemplo n.º 4
0
    def test_get_frequency_distribution_one_texts(self, client):
        text = Text()
        text.text = "Just a simple text"
        db.session.add(text)
        db.session.commit()

        response = client.get(url_for("api.n_gram_frequency_distribution_api"))
        assert response.status_code == HTTPStatus.OK
        assert response.get_json() == {"frequency": {"text1": [1, 1]}}
Ejemplo n.º 5
0
def process():
    value = request.args['text']

    existing_entry = Text.query.filter_by(text_value=value).first()
    if existing_entry is None:
        new_db_entry = Text(value)
        db.session.add(new_db_entry)
        db.session.commit()

    return render_template('index.html', title='Input Received', text=value)
Ejemplo n.º 6
0
    def test_get_vocabulary_with_one_text(self, client):
        text = Text()
        text.text = "Just a simple text"
        db.session.add(text)
        db.session.commit()

        response = client.get(url_for("api.isolated_vocabulary_api"))
        assert response.status_code == HTTPStatus.OK
        assert response.get_json() == {
            "vocabulary": ["just", "simple", "text"]
        }
Ejemplo n.º 7
0
def add_text():
    x = request.form.get("x")
    y = request.form.get("y")
    width = request.form.get("width")
    height = request.form.get("height")
    text = request.form.get("text")

    new_text = Text(x, y, width, height, text)
    db_session.add(new_text)
    db_session.commit()
    return jsonify({'id' : new_text.id})
Ejemplo n.º 8
0
def upload_text():
    form = Text_upload_form()
    if request.method == 'POST':
        title = request.form['title']
        text = request.form['text']
        if title and text:
            newFile = Text(title=title, text=text)
            db.session.add(newFile)
            db.session.commit()
            return redirect(url_for('main.upload_mainpage'))
        return redirect(url_for('main.upload_text'))
    return render_template('text.html', form=form)
Ejemplo n.º 9
0
    def test_01_add(self):
        text = Text.add(
            'home', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. \
Ut vel massa arcu. Ut tincidunt vestibulum eros, congue tempus dolor ultricies sodales. \
Praesent vel dui pellentesque, condimentum nulla id, efficitur metus. Morbi at porta nisl,\
ac venenatis massa. Mauris ut ultrices libero. Vivamus vitae augue vulputate, ultricies enim \
sit amet, imperdiet nunc. Curabitur egestas eget erat eu elementum. Nullam non ullamcorper\
 arcu. Duis pulvinar eu felis eget placerat. Nullam sed lacus vel nisi porttitor interdum \
scelerisque id velit. Pellentesque facilisis, magna ac porttitor feugiat, ligula nulla scelerisque \
nibh, eu tincidunt ipsum urna sed nisi. Donec tincidunt nulla a molestie fermentum. Suspendisse.'
        )
        assert text is not None
Ejemplo n.º 10
0
def add_text_to_database(file_path):
    d1 = datetime.strptime('1/1/2020 1:30 PM', '%m/%d/%Y %I:%M %p')
    d2 = datetime.strptime('5/30/2020 4:50 AM', '%m/%d/%Y %I:%M %p')
    df = pd.read_csv(file_path, delimiter=",")
    for index, row in df.iterrows():
        text = Text(user_id=1,
                    body=row["Tweet"],
                    title="",
                    stance=row["Stance"],
                    target=row["Target"],
                    opinion_towards=row["Opinion towards"],
                    sentiment=row["Sentiment"])
        text.publication_date = random_date(d1, d2)
        db.session.add(text)
    db.session.commit()
Ejemplo n.º 11
0
def register(artifact_id, artifact_hash):
    artifact = Artifact.query.get(artifact_id)
    form = RegisterForm(request.form)
    print(request.method)
    if form.is_submitted():
        print("submitted")
        print(form.email.data)
    if form.validate_on_submit():
        print("valid")
    else:
        print(form.errors)
    if artifact_id is None or artifact.access_hash != artifact_hash:
        flash("invalid artifact id and access hash combination")
        return redirect(url_for('index'))
    if request.method == 'POST' and form.validate_on_submit():
        user = User.get_or_create_user(form.email.data, form.name.data)
        predecessor = Handover.query.join(Artifact).filter(
            Artifact.id == Handover.artifact_id).filter(
                Artifact.id == artifact.id).order_by(
                    Handover.id.desc()).limit(1).one_or_none()
        if predecessor != None:
            predecessor_id = predecessor.id
        else:
            predecessor_id = None
        if form.text.data != "":
            media = Media(type=MediaType.text)
            db.session.add(media)
            db.session.commit()
            text = Text(media_id=media.id, text=form.text.data)
            db.session.add(text)
            db.session.commit()
        handover = Handover(artifact_id=artifact_id,
                            predecessor_id=predecessor_id,
                            lat=form.lat.data,
                            lon=form.lon.data,
                            user_id=user.id)
        handover.media_id = media.id
        db.session.add(handover)
        db.session.commit()
        return redirect(url_for('handover', handover_id=handover.id))
    handover_count = Handover.query.join(Artifact).filter(
        Artifact.id == Handover.artifact_id).count()
    return render_template('register.html',
                           title="Register Handover",
                           form=form,
                           artifact_id=artifact_id,
                           handover_count=handover_count,
                           access_hash=artifact_hash)
Ejemplo n.º 12
0
def add_text(payload: Dict) -> Text:
    """Add new text"""

    _check_anonymity()
    tags_string = ','.join(get_tags(payload['text']))

    new_text = Text(text=payload['text'],
                    creation_dt=datetime.now(),
                    tags=tags_string,
                    user_id=current_user.id)

    db.session.add(new_text)
    db.session.commit()
    print('id:', new_text.id)

    return new_text
Ejemplo n.º 13
0
def classify_text():
    # text = None
    form = TextInput()
    if form.validate_on_submit():
        text = Text(text=form.text.data)
        text.classify_themes()
        db.session.add(text)
        db.session.commit()
        session['text_id'] = text.id
        # session['text'] = form.text.data
        # session['sentences'] = classifier.predict(session['text'])
        return redirect(url_for('main.classify_text'))
    text = Text.query.filter_by(id=session.pop('text_id', None)).first()
    # text = session.pop('text', None)
    # sentences = session.pop('sentences', [])
    return render_template('classify.html', form=form, text=text)
Ejemplo n.º 14
0
def add_texts_from_file():
    input_path = "C:\\Users\\Mateusz\\OneDrive\\NLP\\stance-data-all-annotations\\data-all-annotations\\testdata-taskA-all-annotations.txt"
    stances = pd.read_csv(input_path, header=0, sep='\t')
    stances['Tweet length'] = stances['Tweet'].apply(len)
    for i, row in stances.iterrows():
        if row["Stance"] == 'NONE':
            is_stance = False
        else:
            is_stance = True
        text = Text(user_id=1,
                    is_stance=is_stance,
                    title=row["Target"],
                    characters=row["Tweet length"],
                    body=row["Tweet"],
                    category=row["Target"],
                    stance=row["Stance"],
                    opinion_towards=row["Opinion towards"])
        db.session.add(text)
    db.session.commit()
Ejemplo n.º 15
0
    def post(self):
        json_data = request.get_json(force=True)
        schema = SendTextSchema()

        try:

            data = schema.load(json_data)

            text = Text()
            text.text = data.get("text")

            db.session.add(text)
            db.session.commit()

            data.update({"id": text.id})

            return schema.dump(data), 201

        except ValidationError as error:
            return error.messages, 400
Ejemplo n.º 16
0
def index():
	title = 'This is in apppy give to base'
	form = Texts()
	if form.validate_on_submit():
		text = form.text.data
		post = Text(body=text)
		current_user.texts.append(post)
		print('current_user::::', current_user)
		db.session.commit()
		flash('发表成功', category='success')

	#len来计算粉丝与订阅别人有多少人
	count_fans = len(current_user.fans)
	count_subscribe = len(current_user.subscribe)
	page = request.args.get('page', 1, type=int) # 在网页url上取到?后面的page=多少, 默认是第1页,Int类型
	print('当前页是第几页:::', page)
	# posts = Text.query.order_by(Text.timestamp.desc()).all()
	posts = Text.query.order_by(Text.timestamp.desc()).paginate(page, 4, False)
	print('显示前面是第几页',posts.prev_num)
	print('显示后面是第几页',posts.next_num)
	#.all()查询时返回的是List, .paginate()返回的是对象
	#paginage第一个参数是在url上获取的第几页,也就是上面page拿到的值 , 第二个参数一页上有几条信息, 第三个参数不满足4条一页时,不报错
	return render_template('index.html', title=title, form=form, posts=posts,count_fans=count_fans,count_subscribe=count_subscribe)
Ejemplo n.º 17
0
def add_text():
    data = request.get_json() or {}
    user = User.query.filter_by(email='*****@*****.**').first()
    g.current_user = user
    if not token_auth.verify_token(data['token']):
        print('token not verified')
        return  #authentication token is bad
    if 'body' not in data:
        return bad_request('Please specify text body and try again.')
    if len(data['body']) < 10:
        return bad_request('Text is too short try again.')
    # if Text.query.filter_by(body=data['body']).first():
    #     return bad_request('This text has already been marked.')
    text = Text(user_id=g.current_user.id, body=data['body'])
    text.from_dict(data)
    db.session.add(text)
    db.session.flush()
    db.session.commit()
    model_target = fasttext.load_model(
        "..\\stance-tagger-kedro\\data\\06_models\\model_classification.bin")
    model_prediction = model_target.predict(data["body"])[0][0]
    print(model_prediction)
    stances_dict = {
        '__label__legalization_of_abortion':
        'Legalization of Abortion',
        '__label__hillary_clinton':
        "Hillary Clinton",
        '__label__feminist_movement':
        "Feminist Movement",
        '__label__atheism':
        "Atheism",
        '__label__climate_change_is_a_real_concern':
        "Climate Change is a Real Concern"
    }
    text.target = stances_dict[model_prediction]
    ############################################
    model_target = fasttext.load_model(
        "..\\stance-tagger-kedro\\data\\06_models\\model_favour_against.bin")
    model_prediction = model_target.predict(data["body"])[0][0]
    favour_against_dict = {
        '__label__favour': 'FAVOUR',
        '__label__none': "NONE",
        '__label__against': "AGAINST"
    }
    text.stance = favour_against_dict[model_prediction]
    ############################################
    model_target = fasttext.load_model(
        "..\\stance-tagger-kedro\\data\\06_models\\model_opinion_towards.bin")
    model_prediction = model_target.predict(data["body"])[0][0]
    favour_against_dict = {
        '__label__target': 'TARGET',
        '__label__other': "OTHER",
        '__label__noone': "NO ONE"
    }
    text.opinion_towards = favour_against_dict[model_prediction]
    ############################################
    model_target = fasttext.load_model(
        "..\\stance-tagger-kedro\\data\\06_models\\model_sentiment.bin")
    model_prediction = model_target.predict(data["body"])[0][0]
    sentiment_dict = {
        '__label__positive': 'POSITIVE',
        '__label__negative': "NEGATIVE",
        '__label__neither': "NEITHER"
    }
    text.sentiment = sentiment_dict[model_prediction]

    response = jsonify(text.to_dict())
    response.status_code = 201
    response.headers['Location'] = url_for('api.add_text', id=text.id)
    return response
Ejemplo n.º 18
0
    def cancel(self, bot, udpate):
        msg = "Thank for joining us. welcome bank again"
        bot.send_message(chat_id=udapte.message.chat_id, text=msg)
        return ConversationHandler.END


'''
The Customer 
'''
'''
TheConverstion Handlers 
'''
#instances
invite = Invitation()
text = Text()
# the invitation Conversation Handlers
invitation_handler = ConversationHandler(
    entry_points=[CommandHandler('order', invite.order_entry)],
    states={
        INVITE_PHONE: [MessageHandler(Filters.text, invite.phone)],
        INVITE_COLLECTION: [MessageHandler(Filters.text, invite.collection)],
        QUANTITY:
        [MessageHandler(filters=Filters.text, callback=invite.quantity)],
        INVITE_LOCATION:
        [MessageHandler(filters=Filters.text, callback=invite.location)],
        TIME: [MessageHandler(filters=Filters.text, callback=invite.time)],
        CONFIRM: [
            MessageHandler(filters=Filters.text,
                           callback=invite.invitation_confirm)
        ],
Ejemplo n.º 19
0
def postBookText():

    # check if book is finished
    if finish_perc() >= 100:
        return jsonify(result='6', error='The Book is finish!')

    # text post by user
    text = request.form['txt']

    # check text length
    if len(text) > max_length:
        return jsonify(result='0', error='len > {}'.format(max_length))

    # check if text is chars or ,,!?-\n
    char = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!?.,\' "')
    if not any((i in char) for i in text):
        return jsonify(result='4', error='unauthorized char')

    # double space check
    double_space = re.match(r'^((?!\s{2}).)*$', text)
    if double_space is None:
        return jsonify(result='6', error='Double Space, please check')

    # check if is the last text posted
    lastTextID = int(request.form['lastTextID'])
    last_text_id = Text.query.order_by(
        Text.id.desc()).first()  # get last text submited

    if last_text_id.id != lastTextID:
        # A new text was posted in this location id.
        return jsonify(result='2', error='Someone posted in that location', last_text_id=last_text_id.id)

    # check if words are valid english word
    # remove pontuation and split words
    words = re.sub('\!|\?|\.|\,|\-|\"', '', text).lower().split()

    if words == []:
        # just space no words
        return jsonify(result='6', error='Empty field')

    words_not_valid = []
    for word in words:
        if word not in eng_words:
            words_not_valid.append(word)
    if words_not_valid != []:
        return jsonify(result='5', error='Not valid english word', words=words_not_valid)

    # get IP
    if dev is True:
        ip = (str(randint(0, 9)) + str(randint(0, 9)) + str(randint(0, 9)) + '.' + str(randint(0, 9)) +
              str(randint(0, 9)) + str(randint(0, 9)) + '.' + str(randint(0, 9)) + str(randint(0, 9)) + str(randint(0, 9)))
    else:
        ip = request.headers['X-Real-IP']

    post_writer = Writer.query.filter(Writer.ip == ip).first()

    # remove space trailling
    text = text.strip()

    # if user not in db
    if post_writer is None:

        # create user and save in db
        writer = Writer(ip=ip)
        db.session.add(writer)

        # save post in db
        post_text = Text(text=text)
        writer.texts.append(post_text)
        db.session.commit()

        return jsonify(result='1', error='success')  # no erros

    # if user exist
    else:

        # check last post timestamp
        last_text_timestamp = post_writer.texts.all(
        )[-1].timestamp  # get last text's timestamp

        duration = datetime.utcnow() - last_text_timestamp
        # get time last submiting in hours
        last_text_post = duration.total_seconds() // 3600

        if last_text_post > min_post_interval:  # if last user's post time >  min post

            post_text = Text(text=text)
            post_writer.texts.append(post_text)
            db.session.commit()

            return jsonify(result='1', error='success')  # no erros

        else:
            # if user submited not in the min_post_interval range
            return jsonify(result='3', error='Can only submit every {} hours'.format(min_post_interval), min_post_interval=min_post_interval, reset=last_text_post - min_post_interval)