コード例 #1
0
def parse_ingredients():
    """
    This endpoint takes a url-encoded ingredient string and runs it through the
    tagger. Requests should be in the form of:
        http://example.com/parse?ingredient=some%20ingredient%20here

    Multiple ingredients can be delimited with a newline character. (%0A url-encoded)

    :return: JSON containing the tagged ingredient data
    """

    ingredient_input = request.args.get("ingredient").split('\n')
    input_ok, bad_resp = validate_input(ingredient_input)
    json_result_fname = "%s.txt" % str(uuid.uuid4())
    if not input_ok:
        return bad_resp

    _, tmp_file = tempfile.mkstemp()
    with open(tmp_file, 'w') as input_file:
        input_file.write(utils.export_data(ingredient_input))
        input_file.flush()
        input_file.close()

    os.system("crf_test -v 1 -m %s %s > %s" % (modelFilename, tmp_file, json_result_fname))
    os.remove(tmp_file)
    json_output = json.dumps(utils.import_data(open(json_result_fname)), indent=4)
    os.remove(json_result_fname)

    resp = Response(u'%s' % json_output)
    resp.headers['Content-Type'] = 'application/json; charset=utf-8'

    return resp
コード例 #2
0
def _exec_crf_test(input_text, model_path):
    with tempfile.NamedTemporaryFile() as input_file:
        input_file.write(utils.export_data(input_text))
        input_file.flush()
        return subprocess.check_output([
            "crf_test", "--verbose=1", "--model", model_path, input_file.name
        ]).decode("utf-8")
コード例 #3
0
def _exec_crf_test(input_text, model_path):
    with tempfile.NamedTemporaryFile(mode='w') as input_file:
        input_file.write(utils.export_data(input_text))
        input_file.flush()
        return subprocess.check_output(
            ['crf_test', '--verbose=1', '--model', model_path,
             input_file.name]).decode('utf-8')
コード例 #4
0
def tag(ingredient_phrase):
    with tempfile.NamedTemporaryFile(mode='w+',
                                     encoding='utf-8') as input_file:
        input_file.write(utils.export_data(ingredient_phrase))
        input_file.flush()
        processOutput = subprocess.check_output([
            'crf_test', '--verbose=1', '--model', model_path, input_file.name
        ]).decode('utf-8')
        return utils.import_data(processOutput.split('\n'))
コード例 #5
0
def tag(ingredients_string):
    _, tmpFile = tempfile.mkstemp()

    with open(tmpFile, 'w') as outfile:
        outfile.write(utils.export_data(ingredients_string.splitlines()))

    modelFilename = "/ingredient-phrase-tagger/tmp/model_file"
    result = subprocess.check_output("crf_test -v 1 -m %s %s" %
                                     (modelFilename, tmpFile),
                                     shell=True)
    os.system("rm %s" % tmpFile)
    return json.dumps(utils.import_data(result.splitlines()), indent=4)
コード例 #6
0
def parse_ingredients(ingredients):
    # take out any characters not suited for json
    formatted_ingredients = [
        i.replace(u'\xa0', ' ') \
        .encode('ascii', 'ignore') \
        .encode('utf-8') \
    for i in ingredients ]
    _, tmpFile = tempfile.mkstemp()

    with open(tmpFile, 'w') as outfile:
        outfile.write(utils.export_data(formatted_ingredients))

    tmpFilePath = "tmp/model_file"
    modelFilename = os.path.join(os.path.dirname(__file__), tmpFilePath)
    command = ("crf_test -v 1 -m %s %s" % (modelFilename, tmpFile)).split(" ")

    proc = subprocess.Popen(command, stdout=subprocess.PIPE)
    output = proc.stdout.read()
    parsed_ingredients = utils.import_data(output.split('\n'))
    os.system("rm %s" % tmpFile)
    return [i for i in parsed_ingredients if i.get('name')]
コード例 #7
0
#!/usr/bin/env python
from __future__ import print_function

import os
import sys
import tempfile

from ingredient_phrase_tagger.training import utils

if len(sys.argv) < 2:
    sys.stderr.write('Usage: parse-ingredients.py FILENAME')
    sys.exit(1)

FILENAME = str(sys.argv[1])
_, tmpFile = tempfile.mkstemp()

with open(FILENAME) as infile, open(tmpFile, 'w') as outfile:
    outfile.write(utils.export_data(infile.readlines()))

tmpFilePath = "../tmp/model_file"
modelFilename = os.path.join(os.path.dirname(__file__), tmpFilePath)
os.system("crf_test -v 1 -m %s %s" % (modelFilename, tmpFile))
os.system("rm %s" % tmpFile)
コード例 #8
0
#!/usr/bin/env python
from __future__ import print_function

import sys
import os
import tempfile

from ingredient_phrase_tagger.training import utils


if len(sys.argv) < 2:
    sys.stderr.write('Usage: parse-ingredients.py FILENAME')
    sys.exit(1)

FILENAME = str(sys.argv[1])
_, tmpFile = tempfile.mkstemp()

with open(FILENAME) as infile, open(tmpFile, 'w') as outfile:
    outfile.write(utils.export_data(infile.readlines()))

tmpFilePath = "../tmp/model_file"
modelFilename = os.path.join(os.path.dirname(__file__), tmpFilePath)
os.system("crf_test -v 1 -m %s %s" % (modelFilename, tmpFile))
os.system("rm %s" % tmpFile)
コード例 #9
0
from __future__ import print_function

import sys
import os
import tempfile
import json
import subprocess

from ingredient_phrase_tagger.training import utils

_, tmpFile = tempfile.mkstemp()

with open(tmpFile, 'w') as outfile:
    outfile.write(
        utils.export_data(
            ['2 tablespoons cornstarch, dissolved in 2 tablespoons water']))

tmpFilePath = "tmp/model_file"
modelFilename = os.path.join(os.path.dirname(__file__), tmpFilePath)
command = ("crf_test -v 1 -m %s %s" % (modelFilename, tmpFile)).split(" ")

proc = subprocess.Popen(command, stdout=subprocess.PIPE)
output = proc.stdout.read()
print(json.dumps(utils.import_data(output.split('\n'))))

os.system("rm %s" % tmpFile)
コード例 #10
0
import sys
import os
import tempfile
import StringIO
import json
import subprocess
from ingredient_phrase_tagger.training import utils

if len(sys.argv) < 2:
    sys.stderr.write('Usage: parse-ingredients.py TEXT')
    sys.exit(1)

text = sys.argv[1]

_, tmpFile = tempfile.mkstemp()

string_file = StringIO.StringIO(text)
text_file = open(tmpFile, "wt")
text_file.write(utils.export_data(string_file.readlines()))
text_file.close()

tmpFilePath = "../tmp/model_file"
modelFilename = os.path.join(os.path.dirname(__file__), tmpFilePath)
results = StringIO.StringIO(
    subprocess.check_output(
        ["crf_test", "-v", "1", "-m", modelFilename, tmpFile]))
os.system("rm %s" % tmpFile)
results_json = (json.dumps(utils.import_data(results), indent=4))
print(results_json)
コード例 #11
0
from __future__ import print_function

import sys
import os
import tempfile
import fileinput
import pdb
import json
import subprocess

from ingredient_phrase_tagger.training import utils

_, tmpFile = tempfile.mkstemp()

with open(tmpFile, 'w') as outfile:
    outfile.write(utils.export_data(['1 cup of white wine', '3 cups of vinegar']))

tmpFilePath = "../tmp/model_file"
modelFilename = os.path.join(os.path.dirname(__file__), tmpFilePath)
command = ("crf_test -v 1 -m %s %s" % (modelFilename, tmpFile)).split(" ")

proc = subprocess.Popen(command, stdout=subprocess.PIPE)
output = proc.stdout.read()

print(json.dumps(utils.import_data(iter(output.split('\n')))))
# os.system("crf_test -v 1 -m %s %s" % (modelFilename, tmpFile))
os.system("rm %s" % tmpFile)

fileinput.close()
コード例 #12
0
ファイル: parse-ingredients.py プロジェクト: leearaneta/frtt
#!/usr/bin/env python
from __future__ import print_function

import sys
import os
import tempfile
import fileinput
import pdb

from ingredient_phrase_tagger.training import utils

_, tmpFile = tempfile.mkstemp()

with open(tmpFile, 'w') as outfile:
    outfile.write(utils.export_data(fileinput.input()))

tmpFilePath = "../tmp/model_file"
modelFilename = os.path.join(os.path.dirname(__file__), tmpFilePath)
os.system("crf_test -v 1 -m %s %s" % (modelFilename, tmpFile))
os.system("rm %s" % tmpFile)

fileinput.close()