aokitools commited on
Commit
635a691
·
1 Parent(s): e66554e
Files changed (2) hide show
  1. app.py +120 -4
  2. requirements.txt +1 -0
app.py CHANGED
@@ -465,8 +465,15 @@ def f_formattext_ja(e_text1, e_checkbox1):
465
  bunkai = Bunkai()
466
 
467
  l1 = []
468
- for token in bunkai(s1):
469
- l1.append(token.strip())
 
 
 
 
 
 
 
470
  pass
471
 
472
  result = "\n".join(l1)
@@ -536,6 +543,104 @@ def f_janome(e_text1):
536
 
537
  # // ---------------------------------------------- // #
538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
539
  global_google_encoder = None
540
 
541
  def f_googlesentenceencoder(e_text1, e_checkbox1):
@@ -1092,8 +1197,19 @@ with gr.Blocks() as sample01:
1092
  pass
1093
  pass
1094
 
 
 
 
 
 
 
 
 
 
 
 
1095
 
1096
- with gr.Tab("文章ベクトル"):
1097
  e_text1 = gr.TextArea(show_label=True, label="TEXT")
1098
  e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
1099
 
@@ -1122,7 +1238,7 @@ with gr.Blocks() as sample01:
1122
  pass
1123
 
1124
  e_text1 = gr.TextArea(s1, show_label=True, label="入力ベクトル(空白区切り,31行以上~1000行まで)")
1125
- e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
1126
  e_checkbox2 = gr.Checkbox(label="ラベルをプロットする")
1127
  e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
1128
  e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
 
465
  bunkai = Bunkai()
466
 
467
  l1 = []
468
+
469
+ import re
470
+ l2 = re.split(r"\r|\n|\r\n|。", s1)
471
+ for i2 in l2:
472
+ i2 = i2.strip()
473
+ for token in bunkai(i2):
474
+ l1.append(token.strip())
475
+ print("TOKEN: {0}".format(len(l1)))
476
+ pass
477
  pass
478
 
479
  result = "\n".join(l1)
 
543
 
544
  # // ---------------------------------------------- // #
545
 
546
+ def f_gensim(e_text1, e_checkbox1):
547
+ result = {}
548
+ result["output1"] = ""
549
+ result["output2"] = ""
550
+ try:
551
+
552
+ s1 = e_text1
553
+
554
+ documents = []
555
+
556
+ list_labels = []
557
+ list1 = []
558
+ index1 = 0
559
+ for i1 in s1.splitlines()[:1000]:
560
+ s1 = i1.strip()
561
+
562
+ if e_checkbox1:
563
+ ss1 = s1.split()
564
+ s2 = ss1[0]
565
+ s1 = " ".join(ss1[1:])
566
+ pass
567
+ else:
568
+ s2 = "{0:08}".format(index1)
569
+ s1 = " ".join(s1.split())
570
+ pass
571
+
572
+ d1, s1 = f_janome(s1)
573
+
574
+ import gensim
575
+ document1 = gensim.models.doc2vec.TaggedDocument(words=s1.split(), tags=[s2])
576
+ documents.append(document1)
577
+
578
+ list1.append(s1)
579
+ list_labels.append(s2)
580
+
581
+ index1 += 1
582
+ pass
583
+
584
+ import gensim
585
+ model = gensim.models.doc2vec.Doc2Vec(documents=documents, dm=1, vector_size=512, window=8, min_count=10, workers=64, epochs=100)
586
+ print(dir(model))
587
+ print(model.wv)
588
+
589
+
590
+ vectors = []
591
+ for i1 in range(len(list1)):
592
+ s2 = list_labels[i1]
593
+ s1 = model.docvecs[s2]
594
+ vectors.append(s1)
595
+
596
+ #print("{0}: {1}".format(s2, s1))
597
+ pass
598
+
599
+ list_text1 = []
600
+ list_vector1 = []
601
+
602
+ if True:
603
+ for i1 in range(len(list1)):
604
+ import io
605
+ buf = io.StringIO()
606
+
607
+ import numpy
608
+ numpy.savetxt(buf, vectors[i1])
609
+
610
+ s2 = "\t".join(buf.getvalue().split())
611
+ d1 = {}
612
+ d1["text"] = list1[i1]
613
+ d1["vector"] = s2
614
+
615
+ import json
616
+ s3 = json.dumps(d1, ensure_ascii=False)
617
+ list_vector1.append(s3)
618
+
619
+ s4 = "{0}\t{1}".format(list_labels[i1], s2)
620
+ list_text1.append(s4)
621
+ pass
622
+ pass
623
+
624
+ if len(list_vector1) > 0:
625
+ result["output1"] = "\n".join(list_vector1)
626
+ pass
627
+
628
+ if len(list_text1) > 0:
629
+ result["output2"] = "\n".join(list_text1)
630
+ pass
631
+ pass
632
+ except Exception as e:
633
+ result["output1"] = "ERROR: {0}".format(e)
634
+
635
+ import traceback
636
+ print(traceback.format_exc())
637
+ pass
638
+
639
+ return (result["output1"], result["output2"])
640
+ pass
641
+
642
+ # // ---------------------------------------------- // #
643
+
644
  global_google_encoder = None
645
 
646
  def f_googlesentenceencoder(e_text1, e_checkbox1):
 
1197
  pass
1198
  pass
1199
 
1200
+ with gr.Tab("文章ベクトル(Doc2Vec)"):
1201
+ e_text1 = gr.TextArea(show_label=True, label="TEXT")
1202
+ e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
1203
+
1204
+ e_button1 = gr.Button("変換")
1205
+
1206
+ e_text2 = gr.TextArea(show_label=True, label="JSON LINES")
1207
+ e_text3 = gr.TextArea(show_label=True, label="ベクトル")
1208
+
1209
+ e_button1.click(f_gensim, inputs=[e_text1, e_checkbox1], outputs=[e_text2, e_text3])
1210
+ pass
1211
 
1212
+ with gr.Tab("文章ベクトル(GoogleSentenceEncoder)"):
1213
  e_text1 = gr.TextArea(show_label=True, label="TEXT")
1214
  e_checkbox1 = gr.Checkbox(label="1カラム目をラベルとして設定")
1215
 
 
1238
  pass
1239
 
1240
  e_text1 = gr.TextArea(s1, show_label=True, label="入力ベクトル(空白区切り,31行以上~1000行まで)")
1241
+ e_checkbox1 = gr.Checkbox(value=True, label="1カラム目をラベルとして設定")
1242
  e_checkbox2 = gr.Checkbox(label="ラベルをプロットする")
1243
  e_radio1 = gr.Radio(["2D", "3D"], value="2D", label="dimension")
1244
  e_radio2 = gr.Radio(["TSNE", "UMAP"], value="TSNE", label="algorithm")
requirements.txt CHANGED
@@ -13,6 +13,7 @@ scipy
13
  tensorflow
14
  tensorflow_hub
15
  tensorflow_text
 
16
 
17
 
18
 
 
13
  tensorflow
14
  tensorflow_hub
15
  tensorflow_text
16
+ gensim
17
 
18
 
19