-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (105 loc) · 3.7 KB
/
Copy pathmain.py
File metadata and controls
129 lines (105 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from tweepy import *
import sys
from Twapi import *
from Psgr import *
from Analyze import *
def set_default_encoding(charset):
try:
#print sys.getdefaultencoding()
sys.setdefaultencoding(charset)
except AttributeError:
reload(sys)
sys.setdefaultencoding(charset)
#print sys.getdefaultencoding()
# twt = Twapi()
# api = twt.getAPIInstance()
# try:
# for value in range(0,15):
# print api.home_timeline()[value].text
# except TweepError, e:
# print 'error'
# psgr = Psgr()
# cur = psgr.getCur()
#
# cur.execute("select * from users")
# for row in cur:
# print(row)
#
# psgr.dbCommit()
def main():
# utf-8に変更
set_default_encoding('utf-8')
# PsgrのInstance作成
# ツイートの取得
# twt = Twapi()
# api = twt.twitter_api
# status = api.home_timeline()[0]
#
# sentence = status.text
# tweet_id = status.id
# user_id = status.user.screen_name
# user_name = status.user.name
sentence = "すもももももももものうち"
tweet_id = 760430632317038592
user_id = "snow_moment09"
user_name = "雪村刹那"
# データベースInstanceの作成
psgr = Psgr()
try:
#トランザクション処理開始
psgr.begin()
values = (sentence,tweet_id,user_id,user_name)
sqlcom = "INSERT INTO sentences (sentence,tweet_id,user_id,user_name) VALUES (%s,%s,%s,%s) RETURNING sentence_id;"
psgr.execute(sqlcom,values)
sentence_id = psgr.lastrowid()
# テキストの解析
analyze = Analyze()
# print analyze.get_version()
parse_data = analyze.parse_sentence(sentence.encode('utf-8'))
for data in parse_data:
# print data[0],'\t',data[1].split(',')[0]
detail_array = ['*'] * 9
detail_array[:len(data[1].split(','))] = data[1].split(',')
# 登録用データ↓↓
word = data[0]
part_of_speech = detail_array[0]
if part_of_speech == "BOS/EOS":
continue
part_of_speech_detail1 = detail_array[1]
part_of_speech_detail2 = detail_array[2]
part_of_speech_detail3 = detail_array[3]
conjugate1 = detail_array[4]
conjugate2 = detail_array[5]
original = detail_array[6]
pronunciation1 = detail_array[7]
pronunciation2 = detail_array[8]
values = (word,part_of_speech,part_of_speech_detail1,part_of_speech_detail2,part_of_speech_detail3,conjugate1,conjugate2,original,pronunciation1,pronunciation2)
sqlcom = "INSERT INTO words (word,part_of_speech,part_of_speech_detail1,part_of_speech_detail2,part_of_speech_detail3,conjugate1,conjugate2,original,pronunciation1,pronunciation2) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) RETURNING word_id;"
psgr.execute(sqlcom,values)
word_id = psgr.lastrowid()
values = (sentence_id,word_id)
sqlcom = "INSERT INTO sentence_word (sentence_id,word_id) VALUES (%s,%s) RETURNING sentence_word_id;"
psgr.execute(sqlcom,values)
psgr.commit()
del analyze
except Exception as e:
print e
psgr.rollback()
del psgr
# del twt
if __name__ == "__main__":
main()
# psgr = Psgr()
# print "\n"
# psgr.showTable('sentences')
# print "\n"
# psgr.showTable('words')
# print "\n"
# psgr.showTable('sentence_word')
# analyze = Analyze()
# print analyze.get_version()
# parse_data = analyze.parse_sentence("太郎はこの本を二郎を見た女性に渡した。")
# for data in parse_data:
# print data[0],'\t',data[1]