Пример #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 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'))
Пример #3
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)
Пример #4
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')]
#!/usr/bin/env python
from __future__ import print_function

import sys
import json

from ingredient_phrase_tagger.training import utils

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

# the latin 1 bit here is naughty - we need to do unicode properly
print(
    json.dumps(utils.import_data(open(sys.argv[1])),
               indent=4,
               encoding='latin1'))
Пример #6
0
def _convert_crf_output_to_json(crf_output):
    return json.dumps(utils.import_data(crf_output), indent=2, sort_keys=True)
Пример #7
0
#!/usr/bin/env python
from __future__ import print_function

import sys
import json

from ingredient_phrase_tagger.training import utils

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

print(json.dumps(utils.import_data(open(sys.argv[1])), indent=4))
Пример #8
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)
Пример #9
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)
#!/usr/bin/env python
from __future__ import print_function

import sys
import json

from ingredient_phrase_tagger.training import utils


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

print(json.dumps(utils.import_data(open(sys.argv[1])), indent=4))
Пример #11
0
#!/usr/bin/env python
from __future__ import print_function

import sys
import os
import tempfile
import json

from ingredient_phrase_tagger.training import utils

if len(sys.argv) < 2:
    sys.stderr.write('Usage: parse-ingredients-as-json.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)
parsed = os.popen("crf_test -v 1 -m %s %s" %
                  (modelFilename, tmpFile)).readlines()
os.system("rm %s" % tmpFile)
print(json.dumps(utils.import_data(parsed), indent=4))
Пример #12
0
#!/usr/bin/env python
from __future__ import print_function
import fileinput

import json

from ingredient_phrase_tagger.training import utils

print(json.dumps(utils.import_data(fileinput.input()), indent=4))
fileinput.close()