在python中读取和写入JSON文件

时间:2017-07-30 12:59:01

标签: python json

我正在尝试根据用户输入编写JSON文件,如果我的条目是新的,我成功地能够写入我的文件,但是我想要实现的部分是查看是否有某些数据已存在于JSON文件中,此前我已经有了#34; Raj正在前往伦敦"有意图"问候"因此我不需要添加新条目,我想在这里检查文本是否已经存在然后根据用户输入更改它或保持不变

请询问是否需要澄清。

但是我无法做到这一点,任何人都可以告诉我我的代码有什么问题。

from rasa_nlu.model import Metadata, Interpreter
from rasa_nlu.config import RasaNLUConfig
import logging
from pprint import pprint
import json
from rasa_nlu.converters import load_data
from rasa_nlu.model import Trainer


result_dict = {}
change_dict = {}

# Open the training file
input_training_file = '/home/{user.name}/AIData/demo-rasa.json'

with open(input_training_file) as infile:
    data = json.load(infile)

outfile = open(input_training_file, 'w')


example_data = [[data['rasa_nlu_data']['common_examples'][i]['text'], data['rasa_nlu_data']['common_examples'][i]['intent']]
                 for i in range(0, len(data['rasa_nlu_data']['common_examples']))]

def retrain_model(input_file):
    training_data = load_data(input_file)
    trainer = Trainer(RasaNLUConfig('config_spacy.json'))
    trainer.train(training_data)
    model_directory = trainer.persist('/home/{user.name}/AIData/models/', model_name='my_model')


model_directory = '/home/{user.name}/AIData/models/my_model'
metadata = Metadata.load(model_directory)
interpreter = Interpreter.load(metadata, RasaNLUConfig('config_spacy.json'))

# Parsing the text
user_text = u"Raj is travelling to London"
result_dict = interpreter.parse(user_text)

outfile = open(input_training_file, 'w')

intent = result_dict['intent']['name']
print "Intent: " + intent

print "Is " + intent + " the correct intent? "

response = raw_input("Yes or No ").lower()

if response == 'yes':
    outfile.write(json.dumps(data, indent=4))
elif response == 'no':
    change_dict['text'] = user_text
    print change_dict['text']
    for text_examples in example_data:
        print text_examples
        if change_dict['text'].lower() in map(lambda x: x.lower(), text_examples):
            print "Text already exists with the intent " + text_examples[1]
            change_input = raw_input("Do you want to change the intent?")
            if change_input.lower() == 'yes':
                training_response = raw_input("What should be the intent?")
                # change_dict['intent'] = training_response.lower()
                data['rasa_nlu_data']['common_examples'][text_examples]['intent'] = training_response.lower()
                outfile.write(json.dumps(data, indent=4) + '\n')
                outfile.close()
            elif change_input.lower() == 'no':
                pass
            else:
                print "Please enter yes or no"
        else:
            training_response = raw_input("What should be the intent? ")
            change_dict['intent'] = training_response
            change_dict['entities'] = []
            data['rasa_nlu_data']['common_examples'].append(change_dict)
            outfile.write(json.dumps(data, indent=4) + '\n')
            outfile.close()
            retrain_model(input_training_file)
            break
else:
    print "Please enter 'Yes' or 'No' "

以下是我要写的示例JSON文件:

{
    "rasa_nlu_data": {
        "common_examples": [
            {
                "text": "hey", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "howdy", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "hey there", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "hello", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "hi", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "good morning", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "good evening", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "dear sir", 
                "intent": "greet", 
                "entities": []
            }, 
            {
                "text": "yes", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "yep", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "yeah", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "indeed", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "that's right", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "ok", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "great", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "right, thank you", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "correct", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "great choice", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "sounds really good", 
                "intent": "affirm", 
                "entities": []
            }, 
            {
                "text": "i'm looking for a place to eat", 
                "intent": "restaurant_search", 
                "entities": []
            }, 
            {
                "text": "I want to grab lunch", 
                "intent": "restaurant_search", 
                "entities": []
            }, 
            {
                "text": "I am searching for a dinner spot", 
                "intent": "restaurant_search", 
                "entities": []
            }, 
            {
                "text": "i'm looking for a place in the north of town", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 31, 
                        "end": 36, 
                        "value": "north", 
                        "entity": "location"
                    }
                ]
            }, 
            {
                "text": "show me chinese restaurants", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 8, 
                        "end": 15, 
                        "value": "chinese", 
                        "entity": "cuisine"
                    }
                ]
            }, 
            {
                "text": "show me chines restaurants", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 8, 
                        "end": 14, 
                        "value": "chinese", 
                        "entity": "cuisine"
                    }
                ]
            }, 
            {
                "text": "show me a mexican place in the centre", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 31, 
                        "end": 37, 
                        "value": "centre", 
                        "entity": "location"
                    }, 
                    {
                        "start": 10, 
                        "end": 17, 
                        "value": "mexican", 
                        "entity": "cuisine"
                    }
                ]
            }, 
            {
                "text": "i am looking for an indian spot called olaolaolaolaolaola", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 20, 
                        "end": 26, 
                        "value": "indian", 
                        "entity": "cuisine"
                    }
                ]
            }, 
            {
                "text": "search for restaurants", 
                "intent": "restaurant_search", 
                "entities": []
            }, 
            {
                "text": "anywhere in the west", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 16, 
                        "end": 20, 
                        "value": "west", 
                        "entity": "location"
                    }
                ]
            }, 
            {
                "text": "anywhere near 18328", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 14, 
                        "end": 19, 
                        "value": "18328", 
                        "entity": "location"
                    }
                ]
            }, 
            {
                "text": "I am looking for asian fusion food", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 17, 
                        "end": 29, 
                        "value": "asian fusion", 
                        "entity": "cuisine"
                    }
                ]
            }, 
            {
                "text": "I am looking a restaurant in 29432", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 29, 
                        "end": 34, 
                        "value": "29432", 
                        "entity": "location"
                    }
                ]
            }, 
            {
                "text": "I am looking for mexican indian fusion", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 17, 
                        "end": 38, 
                        "value": "mexican indian fusion", 
                        "entity": "cuisine"
                    }
                ]
            }, 
            {
                "text": "central indian restaurant", 
                "intent": "restaurant_search", 
                "entities": [
                    {
                        "start": 0, 
                        "end": 7, 
                        "value": "central", 
                        "entity": "location"
                    }, 
                    {
                        "start": 8, 
                        "end": 14, 
                        "value": "indian", 
                        "entity": "cuisine"
                    }
                ]
            }, 
            {
                "text": "bye", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "goodbye", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "good bye", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "stop", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "end", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "farewell", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "Bye bye", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "have a good one", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "Raj is travelling to London", 
                "intent": "goodbye", 
                "entities": []
            }, 
            {
                "text": "Raj is travelling to London", 
                "intent": "goodbye", 
                "entities": []
            }
        ], 
        "entity_synonyms": [
            {
                "synonyms": [
                    "Chinese", 
                    "Chines", 
                    "chines"
                ], 
                "value": "chinese"
            }
        ], 
        "regex_features": [
            {
                "pattern": "[0-9]{5}", 
                "name": "zipcode"
            }
        ]
    }
}

0 个答案:

没有答案