アットウィキロゴ

Python3版ContextTape


20171110 00:12
 

# coding:utf-8
from requests_oauthlib import OAuth1Session
import json
import settings
import pprint


twitter = OAuth1Session(settings.CONSUMER_KEY, settings.CONSUMER_SECRET, settings.ACCESS_TOKEN, settings.ACCESS_TOKEN_SECRET)

params = {}
req = twitter.get("https://api.twitter.com/1.1/statuses/home_timeline.json", params = params)


#pprint.pprint(req.text) 
timeline = json.loads(req.text)
textForSave = json.dumps(timeline , ensure_ascii=False, indent=4)
with open("gottimeline.json","wb") as fh:
     fh.write(textForSave.encode("utf-8"))

gtl_gettimeline.py 

# coding: utf-8
import pprint
import json
import re
import sys

args = sys.argv

# open json Timeline
#with open("tlsample.json") as fh:
#     theTimeline = json.loads(fh.read(), "utf-8")

theTimeline = json.loads(args[1], "utf-8")


theEline = []
for theStatus in theTimeline:
   print(theStatus["text"])
   #theStatus["context"] = ["aiueo","kakikukeko"]
   theStatus["context"] = []
   pattern = u'おは'
   matchOB = re.search(pattern , theStatus["text"])
   if matchOB:
      theStatus["context"].append("おはよう")
   pattern = u'少女試験中'
   matchOB = re.search(pattern , theStatus["text"])
   if matchOB:
      theStatus["context"].append("TEST")
   theEline.append(theStatus)
   #pprint.pprint(theStatus.text)

textForSave = json.dumps(theEline , ensure_ascii=False)
with open("etimeline.json","wb") as fh:
     fh.write(textForSave.encode("utf-8"))

tl2c_timeline2context.py

# coding: utf-8
import pprint
import json
import sys

 

# open json ContextTape 
with open("ContextTape.json") as fh:
     theContextTape = json.loads(fh.read(), "utf-8")

 

def ContextToIntent ( Context , ContextTape ):
    theAnswer = { "condition": [""] , "intent": { "text":"" } }
    for theTape in ContextTape:
        if (set(theTape["condition"]) <= set( Context )):
           #pprint.pprint(theTape["intent"])
           if ( len(theTape["condition"]) >= len(theAnswer["condition"]) ):
              theAnswer = theTape
    return theAnswer

args = sys.argv
theTimeline = json.loads(args[1], "utf-8")
for theStatus in theTimeline:
    pprint.pprint(theStatus["context"])
    answer = ContextToIntent( theStatus["context"] , theContextTape )
    theStatus["condition"] = answer["condition"]
    theStatus["intent"] = answer["intent"]

for theStatus in theTimeline:
    pprint.pprint(theStatus["condition"])
    pprint.pprint(theStatus["intent"])

textForSave = json.dumps(theTimeline , ensure_ascii=False)
with open("itimeline.json","wb") as fh:
     fh.write(textForSave.encode("utf-8"))

c2i_context2intent.py

# coding: utf-8
import pprint
import json
import datetime
import tweepy
import settings
import time
import sys

args = sys.argv


auth = tweepy.OAuthHandler(settings.CONSUMER_KEY, settings.CONSUMER_SECRET)
auth.set_access_token(settings.ACCESS_TOKEN, settings.ACCESS_TOKEN_SECRET)

api = tweepy.API(auth)

 

# open json ContextTape 
#with open("tt_testtweet.json") as fh:
#     theIntent = json.loads(fh.read(), "utf-8")

theIntent = json.loads(args[1], "utf-8")

def IntentToAction ( ContextTape ):
    for theTape in ContextTape:
        if (not "intent" in theTape):
           continue
        else:
           if (not "text" in theTape["intent"]):
              continue
           elif theTape["intent"]["text"] == "":
              continue
           else:
              print(theTape["intent"]["text"] + " #少女試験中 " + str(datetime.datetime.today()))
              pprint.pprint(api.update_status(status=theTape["intent"]["text"] + " #少女試験中 " + str(datetime.datetime.today())))
              time.sleep(10)
        
        

IntentToAction( theIntent )
#pprint.pprint(answer)

i2a_intent2action.py 


20171103 16:18

# coding: utf-8
import pprint
import json


# open json Timeline
with open("tlsample.json") as fh:
#with open("ContextTape.json") as fh:
     theTimeline = json.loads(fh.read(), "utf-8")

theEline = []
for theStatus in theTimeline:
   print(theStatus["text"])
   theStatus["condition"] = ["aiueo","kakikukeko"]
   theEline.append(theStatus)
   #pprint.pprint(theStatus.text)

textForSave = json.dumps(theEline , ensure_ascii=False)
with open("eline.json","wb") as fh:
     fh.write(textForSave.encode("utf-8"))

tl2c.py

# coding:utf-8
from requests_oauthlib import OAuth1Session
import json
import settings
import pprint


twitter = OAuth1Session(settings.CONSUMER_KEY, settings.CONSUMER_SECRET, settings.ACCESS_TOKEN, settings.ACCESS_TOKEN_SECRET)

params = {}
req = twitter.get("https://api.twitter.com/1.1/statuses/home_timeline.json", params = params)


#pprint.pprint(req.text) 
timeline = json.loads(req.text)
textForSave = json.dumps(timeline , ensure_ascii=False)
with open("gottimeline.json","wb") as fh:
     fh.write(textForSave.encode("utf-8"))

gettimeline.py
CONSUMER_KEY = "XXXXXXXX"
CONSUMER_SECRET = "XXXXXXXX"
ACCESS_TOKEN = "XXXXXXXX"
ACCESS_TOKEN_SECRET = "XXXXXXXX"
settings.py

# coding: utf-8
import pprint
import json
theContextTape = []
theContextTape.append( { "condition": ["午前"] , "intent": { "text":"おはようございます。" } }  )
theContextTape.append( { "condition": ["昼","昼"] , "intent": { "text":"こんにちは。" } }  )
theContextTape.append( { "condition": ["昼"] , "intent": { "text":"どうも。" } }  )
theContextTape.append( { "condition": ["夜"] , "intent": { "text":"こんばんは。" } }  )
#pprint.pprint(theContextTape)
theContext = [ "昼","verval","13時"]

#textForSave = json.dumps(theContext , sort_keys=True, ensure_ascii=False)
#with open("Context.json","wb") as fh:
#     fh.write(textForSave.encode("utf-8"))

# open json ContextTape 
with open("ContextTape.json") as fh:
     theContextTape = json.loads(fh.read(), "utf-8")

# open json Context
with open("Context.json") as fh2:
     theContext = json.loads(fh2.read(), "utf-8")


def ContextToIntent ( Context , ContextTape ):
    theAnswer = { "condition": [""] , "intent": { "text":"" } }
    for theTape in ContextTape:
        if (set(theTape["condition"]) <= set( Context )):
           #pprint.pprint(theTape["intent"])
           if ( len(theTape["condition"]) >= len(theAnswer["condition"]) ):
              theAnswer = theTape
    return theAnswer


answer = ContextToIntent( theContext , theContextTape )
pprint.pprint(answer)

contexttape2017.py
["夜", "verval", "13時"] Context.json
[
{"condition": ["午前"], "intent": {"text": "おはようございます。"}}, 
{"condition": ["昼", "昼", "昼", "昼"], "intent": {"text": "こんにちは。"}}, 
{"condition": ["昼"], "intent": {"text": "どうも。"}}, 
{"condition": ["夜"], "intent": {"text": "こんばんは。"}}
]
ContextTape.json

[upgm@localhost ~]$ 

 

20171013 17:06
Cent OS 64 ビット_for_meacb-padic-NEologdで稼動中。

# coding: utf-8
import pprint
import json
theContextTape = []
theContextTape.append( { "condition": ["午前"] , "intent": { "text":"おはようございます。" } }  )
theContextTape.append( { "condition": ["昼","昼"] , "intent": { "text":"こんにちは。" } }  )
theContextTape.append( { "condition": ["昼"] , "intent": { "text":"どうも。" } }  )
theContextTape.append( { "condition": ["夜"] , "intent": { "text":"こんばんは。" } }  )
#pprint.pprint(theContextTape)
theContext = [ "昼","verval","13時"]

#textForSave = json.dumps(theContext , sort_keys=True, ensure_ascii=False)
#with open("Context.json","wb") as fh:
#     fh.write(textForSave.encode("utf-8"))

# open json ContextTape 
with open("ContextTape.json") as fh:
     theContextTape = json.loads(fh.read(), "utf-8")

# open json Context
with open("Context.json") as fh2:
     theContext = json.loads(fh2.read(), "utf-8")


def ContextToIntent ( Context , ContextTape ):
    theAnswer = { "condition": [""] , "intent": { "text":"" } }
    for theTape in ContextTape:
        if (set(theTape["condition"]) <= set( Context )):
           #pprint.pprint(theTape["intent"])
           if ( len(theTape["condition"]) >= len(theAnswer["condition"]) ):
              theAnswer = theTape
    return theAnswer


answer = ContextToIntent( theContext , theContextTape )
pprint.pprint(answer)

contexttape2017.py
["夜", "verval", "13時"] Context.json
[
{"condition": ["午前"], "intent": {"text": "おはようございます。"}}, 
{"condition": ["昼", "昼", "昼", "昼"], "intent": {"text": "こんにちは。"}}, 
{"condition": ["昼"], "intent": {"text": "どうも。"}}, 
{"condition": ["夜"], "intent": {"text": "こんばんは。"}}
]
ContextTape.json

[upgm@localhost ~]$ ls -al |grep json
-rw-rw-r--.   1 upgm upgm     27 10月 13 17:06 Context.json
-rwxrw-rw-.   1 upgm upgm    306 10月 13 16:54 ContextTape.json



20171013 16:30

# coding: utf-8
import pprint
import json
theContextTape = []
theContextTape.append( { "condition": ["午前"] , "intent": { "text":"おはようございます。" } }  )
theContextTape.append( { "condition": ["昼","昼"] , "intent": { "text":"こんにちは。" } }  )
theContextTape.append( { "condition": ["昼"] , "intent": { "text":"どうも。" } }  )
theContextTape.append( { "condition": ["夜"] , "intent": { "text":"こんばんは。" } }  )
print("テスト中")
pprint.pprint(theContextTape)
theContext = [ "昼","verval","13時"]

textForSave = json.dumps(theContextTape , sort_keys=True, ensure_ascii=False, indent=2)
with open("ContextTape.json","wb") as fh:
     fh.write(textForSave.encode("utf-8"))

def ContextToIntent ( Context , ContextTape ):
    print("test")
    theAnswer = { "condition": [""] , "intent": { "text":"" } }
    for theTape in ContextTape:
        pprint.pprint(theTape["condition"])
        if (set(theTape["condition"]) <= set( Context )):
           pprint.pprint(theTape["intent"])
           if ( len(theTape["condition"]) >= len(theAnswer["condition"]) ):
              theAnswer = theTape
    return theAnswer


answer = ContextToIntent( theContext , theContextTape )
pprint.pprint(answer)

20171013 16:00

# coding: utf-8
import pprint
import json
theContextTape = []
theContextTape.append( { "condition": ["午前"] , "intent": { "text":"おはようございます。" } }  )
theContextTape.append( { "condition": ["昼","昼"] , "intent": { "text":"こんにちは。" } }  )
theContextTape.append( { "condition": ["夜"] , "intent": { "text":"こんばんは。" } }  )
print("テスト中")
pprint.pprint(theContextTape)
theContext = [ "昼","verval","13時"]

textForSave = json.dumps(theContextTape , sort_keys=True, ensure_ascii=False, indent=2)
with open("ContextTape.json","wb") as fh:
     fh.write(textForSave.encode("utf-8"))

def ContextToIntent ( Context , ContextTape ):
    print("test")
    for theTape in ContextTape:
        pprint.pprint(theTape["condition"])
        if (set(theTape["condition"]) <= set( Context )):
           pprint.pprint(theTape["intent"])


ContextToIntent( theContext , theContextTape )

 

# coding: utf-8
import pprint
import json
theContextTape = []
theContextTape.append( { "condition": ["午前"] , "intent": { "text":"おはようございます。" } }  )
theContextTape.append( { "condition": ["昼","昼"] , "intent": { "text":"こんにちは。" } }  )
theContextTape.append( { "condition": ["昼"] , "intent": { "text":"どうも。" } }  )
theContextTape.append( { "condition": ["夜"] , "intent": { "text":"こんばんは。" } }  )
#pprint.pprint(theContextTape)
theContext = [ "昼","verval","13時"]

#textForSave = json.dumps(theContext , sort_keys=True, ensure_ascii=False)
#with open("Context.json","wb") as fh:
#     fh.write(textForSave.encode("utf-8"))

# open json ContextTape 
with open("ContextTape.json") as fh:
     theContextTape = json.loads(fh.read(), "utf-8")

# open json Context
with open("Context.json") as fh2:
     theContext = json.loads(fh2.read(), "utf-8")


def ContextToIntent ( Context , ContextTape ):
    theAnswer = { "condition": [""] , "intent": { "text":"" } }
    for theTape in ContextTape:
        if (set(theTape["condition"]) <= set( Context )):
           #pprint.pprint(theTape["intent"])
           if ( len(theTape["condition"]) >= len(theAnswer["condition"]) ):
              theAnswer = theTape
    return theAnswer


answer = ContextToIntent( theContext , theContextTape )
pprint.pprint(answer)

contexttape2017.py
["夜", "verval", "13時"] Context.json
[
{"condition": ["午前"], "intent": {"text": "おはようございます。"}}, 
{"condition": ["昼", "昼", "昼", "昼"], "intent": {"text": "こんにちは。"}}, 
{"condition": ["昼"], "intent": {"text": "どうも。"}}, 
{"condition": ["夜"], "intent": {"text": "こんばんは。"}}
]
ContextTape.json

[upgm@localhost ~]$ 

最終更新:2017年11月10日 00:14