質問編集履歴

3

 

2016/01/24 08:28

投稿

kamatmt
kamatmt

スコア25

test CHANGED
File without changes
test CHANGED
@@ -836,7 +836,7 @@
836
836
 
837
837
  for n in 1..fb do
838
838
 
839
- si[n]="/usr/home/hakamata/ru/tweet_html/si#{n}.txt"
839
+ si[n]="/usr/home/ru/tweet_html/si#{n}.txt"
840
840
 
841
841
  end
842
842
 
@@ -848,7 +848,7 @@
848
848
 
849
849
  for n in 1..fa do
850
850
 
851
- chan[n]="/usr/home/hakamata/ru/tweet_html/r#{n}.txt"
851
+ chan[n]="/usr/home/ru/tweet_html/r#{n}.txt"
852
852
 
853
853
  end
854
854
 

2

追加

2016/01/24 08:28

投稿

kamatmt
kamatmt

スコア25

test CHANGED
File without changes
test CHANGED
@@ -49,3 +49,871 @@
49
49
 
50
50
 
51
51
  考えられることとしてどういったことがありますか?
52
+
53
+
54
+
55
+
56
+
57
+ 追加.........................................................
58
+
59
+ 元のプログラムです。
60
+
61
+ 長く汚いプログラムで申し訳ないです。
62
+
63
+ 116:in `*': nil can't be coerced into Float (TypeError)
64
+
65
+ このようなエラーがでています。
66
+
67
+ 最終的には類以度を出したいです。
68
+
69
+ 手詰まり状態で困っています。
70
+
71
+
72
+
73
+ ```ruby
74
+
75
+ #encoding: utf-8
76
+
77
+
78
+
79
+ require "MeCab"
80
+
81
+ require "kconv"
82
+
83
+ require "nkf"
84
+
85
+
86
+
87
+ ###########
88
+
89
+
90
+
91
+ def hinsi(sentence)
92
+
93
+
94
+
95
+ mecab = MeCab::Tagger.new
96
+
97
+ node = mecab.parseToNode(sentence)
98
+
99
+ word = []
100
+
101
+ begin
102
+
103
+ node = node.next
104
+
105
+ data="#{node.surface.toutf8},#{node.feature.toutf8}"
106
+
107
+ if data.split(",")[1].toutf8 == "名詞" then
108
+
109
+ if data.split(",")[2].toutf8 =~ /一般|固有名詞/ then
110
+
111
+ word.push("#{node.surface.toutf8}")
112
+
113
+
114
+
115
+ end
116
+
117
+ end
118
+
119
+ end until node.next.feature.include?("BOS/EOS")
120
+
121
+
122
+
123
+ return word
124
+
125
+ end
126
+
127
+
128
+
129
+ ###################
130
+
131
+ #出現回数
132
+
133
+ def cou(ta)
134
+
135
+ hash = Hash.new
136
+
137
+ q =0
138
+
139
+ ta.each{|line|
140
+
141
+ array = line.split("\t")
142
+
143
+ tarm = array[0]
144
+
145
+ q += 1
146
+
147
+ if hash.include?(tarm) then
148
+
149
+ hash[tarm] += 1
150
+
151
+ else
152
+
153
+ hash[tarm] = 1
154
+
155
+ end
156
+
157
+ # print array[0], "\n"
158
+
159
+ }
160
+
161
+
162
+
163
+ a = hash.values
164
+
165
+ b = hash.keys
166
+
167
+ return a,q.round(3),b
168
+
169
+
170
+
171
+ end
172
+
173
+
174
+
175
+ #####################
176
+
177
+
178
+
179
+ # tf
180
+
181
+ def tf_(situgen,meitan)
182
+
183
+ c = situgen.length #単語の種類の数
184
+
185
+ tf = []
186
+
187
+ for n in 0..c-1 do
188
+
189
+ tf[n] = situgen[n] / meitan
190
+
191
+ tf[n] = tf[n].round(3)
192
+
193
+ end
194
+
195
+
196
+
197
+ return tf
198
+
199
+ end
200
+
201
+
202
+
203
+ ##################
204
+
205
+
206
+
207
+ #df
208
+
209
+ def df_(w,at)
210
+
211
+ hash = Hash.new
212
+
213
+ q =0
214
+
215
+
216
+
217
+ for n in w do
218
+
219
+ for m in at do
220
+
221
+ pos=m.include?(n)
222
+
223
+ if pos == true then
224
+
225
+ array = n.split("\t")
226
+
227
+ tarm = array[0]
228
+
229
+ q += 1
230
+
231
+ if hash.include?(tarm) then
232
+
233
+ hash[tarm] += 1
234
+
235
+ else
236
+
237
+ hash[tarm] = 1
238
+
239
+ end
240
+
241
+ end
242
+
243
+ end
244
+
245
+ end
246
+
247
+ a = hash.keys
248
+
249
+ b = hash.values
250
+
251
+
252
+
253
+ return a,b
254
+
255
+ end
256
+
257
+
258
+
259
+ ####################
260
+
261
+ #idf
262
+
263
+ def idf_(df_t,alt)
264
+
265
+ c=df_t.length
266
+
267
+ d=[]
268
+
269
+ idf=[]
270
+
271
+ for i in 0..c-1 do
272
+
273
+ d[i]=df_t[i].round(2)
274
+
275
+ idf[i] = Math.log(alt.length/d[i])+1
276
+
277
+ idf[i]= idf[i].round(3)
278
+
279
+ end
280
+
281
+
282
+
283
+ return idf
284
+
285
+ end
286
+
287
+
288
+
289
+ ###################
290
+
291
+
292
+
293
+ #tf-idf
294
+
295
+ def tfidf_(tf_t,idf_t)
296
+
297
+ t = tf_t.length
298
+
299
+
300
+
301
+ tfidf=[]
302
+
303
+ for i in 0..t-1 do
304
+
305
+ tfidf[i]=tf_t[i] * idf_t[i]
306
+
307
+ tfidf[i]=tfidf[i].round(3)
308
+
309
+ end
310
+
311
+
312
+
313
+ return tfidf
314
+
315
+ end
316
+
317
+
318
+
319
+ ############################
320
+
321
+
322
+
323
+ #単語ごとのベクトル
324
+
325
+ def coscount(b1,b2,co1,co2)
326
+
327
+ al=[]
328
+
329
+ al += b1
330
+
331
+ al += b2
332
+
333
+
334
+
335
+ a = al.uniq #種類の数
336
+
337
+ x = b1.length
338
+
339
+ y = b2.length
340
+
341
+ z = a.length
342
+
343
+
344
+
345
+ array1 =[]
346
+
347
+ for i in 0..z-1 do
348
+
349
+ array1[i] = 0
350
+
351
+ end
352
+
353
+
354
+
355
+ array2 =[]
356
+
357
+ for i in 0..z-1 do #s:種類の数
358
+
359
+ array2[i] = 0
360
+
361
+ end
362
+
363
+
364
+
365
+ for n in 0..z-1 do
366
+
367
+ for m in 0..x-1 do
368
+
369
+ if a[n] == b1[m] then
370
+
371
+ array1[n]=co1[m]
372
+
373
+ end
374
+
375
+ end
376
+
377
+ for l in 0..y-1 do
378
+
379
+ if a[n] == b2[l] then
380
+
381
+ array2[n]=co2[l]
382
+
383
+ end
384
+
385
+ end
386
+
387
+
388
+
389
+ end
390
+
391
+
392
+
393
+ return a,array1,array2
394
+
395
+ end
396
+
397
+
398
+
399
+
400
+
401
+ ####################################
402
+
403
+
404
+
405
+ #類似度
406
+
407
+ def cosine_similarity(vector1,vector2)
408
+
409
+ dp = dot_product(vector1, vector2)
410
+
411
+ nm = normalize(vector1) * normalize(vector2)
412
+
413
+ dp / nm
414
+
415
+ end
416
+
417
+
418
+
419
+ def dot_product(vector1, vector2)
420
+
421
+ sum = 0.0
422
+
423
+ vector1.each_with_index{ |val, i| sum += val*vector2[i] }
424
+
425
+ sum
426
+
427
+ end
428
+
429
+
430
+
431
+ def normalize(vector)
432
+
433
+ Math.sqrt(vector.inject(0.0){ |m,o| m += o**2 })
434
+
435
+ end
436
+
437
+
438
+
439
+
440
+
441
+
442
+
443
+ #####################
444
+
445
+
446
+
447
+ def main(t1,t2,k1,k2)
448
+
449
+ #形態素解析a
450
+
451
+
452
+
453
+ w1=[]
454
+
455
+ for j in 1..k1-1 do
456
+
457
+ if t1[j] != nil then
458
+
459
+ w1[j]=hinsi(t1[j])
460
+
461
+ end
462
+
463
+ end
464
+
465
+
466
+
467
+ #形態素解析b
468
+
469
+ w2=[]
470
+
471
+ for j in 1..k2-1 do
472
+
473
+ if t2[j] != nil then
474
+
475
+ w2[j]=hinsi(t2[j])
476
+
477
+ end
478
+
479
+ end
480
+
481
+
482
+
483
+ ####################
484
+
485
+
486
+
487
+ #出現回数a
488
+
489
+ c1 =[]
490
+
491
+ ac1= []
492
+
493
+ bc1=[]
494
+
495
+ for j in 0..k1-1 do
496
+
497
+ if w1[j] != nil then
498
+
499
+ c1[j],ac1[j],bc1[j] = cou(w1[j])
500
+
501
+ end
502
+
503
+ end
504
+
505
+
506
+
507
+ #出現回数b
508
+
509
+ c2 =[]
510
+
511
+ ac2= []
512
+
513
+ bc2=[]
514
+
515
+ for j in 0..k2-1 do
516
+
517
+ if w2[j] != nil then
518
+
519
+ c2[j],ac2[j],bc2[j] = cou(w2[j])
520
+
521
+ end
522
+
523
+ end
524
+
525
+
526
+
527
+ ########################
528
+
529
+
530
+
531
+ #tf a
532
+
533
+ tf1=[]
534
+
535
+ for j in 0..k1-1 do
536
+
537
+ if c1[j] != nil then
538
+
539
+ tf1[j] = tf_(c1[j],ac1[j])
540
+
541
+ end
542
+
543
+ end
544
+
545
+
546
+
547
+
548
+
549
+ #tf b
550
+
551
+ tf2=[]
552
+
553
+ for j in 0..k2-1 do
554
+
555
+ if c2[j] != nil then
556
+
557
+ tf2[j] = tf_(c2[j],ac2[j])
558
+
559
+ end
560
+
561
+ end
562
+
563
+
564
+
565
+ ###############################
566
+
567
+
568
+
569
+ #df a
570
+
571
+ key1=[]
572
+
573
+ df1 =[]
574
+
575
+
576
+
577
+ for j in 0..k1-1 do
578
+
579
+ if w1[j] != nil then
580
+
581
+ key1[j],df1[j] = df_(w1[j],t1) #key1:単語 df1:ある単語がでる文書の数
582
+
583
+ end
584
+
585
+ end
586
+
587
+
588
+
589
+
590
+
591
+ #df b
592
+
593
+ key2=[]
594
+
595
+ df2 =[]
596
+
597
+ for j in 0..k2-1 do
598
+
599
+ if w2[j] != nil then
600
+
601
+ key2[j],df2[j] = df_(w2[j],t2)
602
+
603
+ end
604
+
605
+ end
606
+
607
+
608
+
609
+ #################################
610
+
611
+
612
+
613
+ #idf a
614
+
615
+ idf1=[]
616
+
617
+ for j in 0..k1-1 do
618
+
619
+ if df1[j] != nil then
620
+
621
+ idf1[j] = idf_(df1[j],t1)
622
+
623
+ end
624
+
625
+ end
626
+
627
+
628
+
629
+ #idf b
630
+
631
+ idf2=[]
632
+
633
+ for j in 0..k2-1 do
634
+
635
+ if df2[j] != nil then
636
+
637
+ idf2[j] = idf_(df2[j],t2)
638
+
639
+ end
640
+
641
+ end
642
+
643
+
644
+
645
+
646
+
647
+ ##########################
648
+
649
+ #tfidf a
650
+
651
+ tfidf1=[]
652
+
653
+ for j in 0..k1-1 do
654
+
655
+ if tf1[j] != nil && idf1[j] != nil then
656
+
657
+ tfidf1[j] = tfidf_(tf1[j],idf1[j])
658
+
659
+ end
660
+
661
+ end
662
+
663
+
664
+
665
+ tfidf2=[]
666
+
667
+ for j in 0..k2-1 do
668
+
669
+ if tf1[j] != nil && idf1[j] != nil then
670
+
671
+ tfidf2[j] = tfidf_(tf2[j],idf2[j])
672
+
673
+ end
674
+
675
+ end
676
+
677
+
678
+
679
+ #puts tfidf1
680
+
681
+ #puts tfidf2,key2
682
+
683
+ #######################
684
+
685
+ #カウント
686
+
687
+
688
+
689
+ l=0
690
+
691
+ cosrui=[]
692
+
693
+ a=[]
694
+
695
+ ara1=[]
696
+
697
+ ara2=[]
698
+
699
+
700
+
701
+ for j in 0..k1-1 do
702
+
703
+ for i in 0..k2-1 do
704
+
705
+ a[l],ara1[l],ara2[l] =coscount(bc1[j],bc2[i],tfidf1[j],tfidf2[i])
706
+
707
+ l+=1
708
+
709
+ end
710
+
711
+ end
712
+
713
+
714
+
715
+ #類似度
716
+
717
+ for j in 0..l-1 do
718
+
719
+ cosrui[j] = cosine_similarity(ara1[j],ara2[j])
720
+
721
+ #h.puts "類似度",cosrui[j]
722
+
723
+ end
724
+
725
+
726
+
727
+
728
+
729
+ #####################
730
+
731
+
732
+
733
+
734
+
735
+ z = 0
736
+
737
+ cos=[]
738
+
739
+ if cosrui[0]>=cosrui[1] then
740
+
741
+ cos=cosrui[0]
742
+
743
+ z = 0
744
+
745
+ else
746
+
747
+ cos=cosrui[1]
748
+
749
+ z = 0
750
+
751
+ end
752
+
753
+
754
+
755
+ for n in 2..j-1 do
756
+
757
+ if cos <= cosrui[n] then
758
+
759
+ cos=cosrui[n]
760
+
761
+ z = n
762
+
763
+ end
764
+
765
+ end
766
+
767
+
768
+
769
+
770
+
771
+ return cos
772
+
773
+
774
+
775
+ end
776
+
777
+
778
+
779
+ ##########################################
780
+
781
+
782
+
783
+ #ファイル入力
784
+
785
+ def read_data_file(filename)
786
+
787
+ sentence=[]
788
+
789
+ f=File.open("#{filename}","r:UTF-8")
790
+
791
+ f.each{|data|
792
+
793
+ sentence.push(data)
794
+
795
+ }
796
+
797
+ k= sentence.length
798
+
799
+ t=[]
800
+
801
+ for j in 0..k-1 do
802
+
803
+ t[j]=NKF.nkf("-Xw",sentence[j])
804
+
805
+ end
806
+
807
+ t.uniq!
808
+
809
+ return t,k
810
+
811
+ end
812
+
813
+
814
+
815
+
816
+
817
+ #ファイル数
818
+
819
+ puts "ファイルb"
820
+
821
+ fb=gets.to_i #
822
+
823
+
824
+
825
+ puts "ファイルa"
826
+
827
+ fa=gets.to_i #
828
+
829
+
830
+
831
+
832
+
833
+ si=[]
834
+
835
+
836
+
837
+ for n in 1..fb do
838
+
839
+ si[n]="/usr/home/hakamata/ru/tweet_html/si#{n}.txt"
840
+
841
+ end
842
+
843
+
844
+
845
+
846
+
847
+ chan=[]
848
+
849
+ for n in 1..fa do
850
+
851
+ chan[n]="/usr/home/hakamata/ru/tweet_html/r#{n}.txt"
852
+
853
+ end
854
+
855
+
856
+
857
+
858
+
859
+ ##############
860
+
861
+
862
+
863
+ a=[]
864
+
865
+ b=[]
866
+
867
+ i=1
868
+
869
+ data=[]
870
+
871
+ cosr=[]
872
+
873
+ for n in 1..fb do
874
+
875
+ for m in 1..fa do
876
+
877
+ if si[n] != nil && chan[m] != nil then
878
+
879
+ d1,ke1 = read_data_file(si[n])
880
+
881
+ d2,ke2 = read_data_file(chan[m])
882
+
883
+
884
+
885
+ cosr[i]=main(d1,d2,ke1,ke2)
886
+
887
+ a[i]=n
888
+
889
+ b[i]=m
890
+
891
+ i+=1
892
+
893
+ end
894
+
895
+ end
896
+
897
+ end
898
+
899
+
900
+
901
+ h=File.open("w.txt","w:UTF-8")
902
+
903
+
904
+
905
+
906
+
907
+ for n in 1..i-1 do
908
+
909
+ h.puts "類似度",cosr[n]
910
+
911
+ h.puts a[n],b[n],"\n"
912
+
913
+ end
914
+
915
+
916
+
917
+
918
+
919
+ ```

1

 

2016/01/24 08:27

投稿

kamatmt
kamatmt

スコア25

test CHANGED
File without changes
test CHANGED
@@ -6,23 +6,31 @@
6
6
 
7
7
  計算の途中経過として
8
8
 
9
+
10
+
9
11
 
10
12
 
11
- 0.045
12
13
 
13
- 0.045
14
14
 
15
+ 0.045,
16
+
17
+ 0.045,
18
+
15
- 0.111
19
+ 0.111,
16
20
 
17
21
  ...
18
22
 
23
+
24
+
19
25
 
20
26
 
21
- 5.575
22
27
 
23
- 3.01
24
28
 
29
+ 5.575,
30
+
31
+ 3.01,
32
+
25
- 3.625
33
+ 3.625,
26
34
 
27
35
  ...
28
36