【问题标题】:to find count of each line in another file查找另一个文件中每一行的计数
【发布时间】:2014-02-26 11:35:57
【问题描述】:
How can i get now of times a particular line of one file present in another file 

我有两个文件 rule.txt 和 full.txt。我想检查 full.txt 中 rule.txt 中每一行的计数。请帮助我 在文件 rule.txt 中包含

    NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
    VGF--->V_VM_VF

    The another file full.txt contains 1000 of such type of rules. i want to calculate count of each rule in the rule.txt and I want to get output as line with count.that count is needed for the calculation of probability of each rule.rule.txt contain cfg rules of each sentence

    fc= codecs.open('full.txt', encoding='utf-8') 
    with open('rule.txt', 'r') as fh:
        for line in fh.readlines():
          if(line in fc.readlines()):
                print line
                count=count+1
    print count

    I have this code .but this is not working..plz help me.I need to calculate the probabilty of each  rule in the rule.txt by checking in full.txt.for probability calculation ,i need count of each rule individually.Can you please help me to count the no of times a rule present in full.txt

【问题讨论】:

  • 好的,所以我将NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU 视为一行?
  • 好吧好吧。该行在 full.txt 中出现了 10 次
  • 请检查下面的答案,
  • 如何在 python 中绘制树..

标签: count probability rules


【解决方案1】:

我假设你的文件不是超级大,而且你有足够的内存:

这是文件1:

NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
VGF--->V_VM_VF
KGF--->V_VM_VF P_NSF SSF
VGF--->V_VM_VF KLF NFG_JP

这是文件2:

NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
VGF--->V_VM_VF
VGF--->V_VM_VF
VGF--->V_VM_VF
KGF--->V_VM_VF P_NSF SSF
KGF--->V_VM_VF P_NSF SSF
VGF--->V_VM_VF
VGF--->V_VM_VF
KGF--->V_VM_VF P_NSF SSF
KGF--->V_VM_VF P_NSF SSF
VGF--->V_VM_VF KLF NFG_JP
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
VGF--->V_VM_VF
VGF--->V_VM_VF KLF NFG_JP
VGF--->V_VM_VF KLF NFG_JP
VGF--->V_VM_VF
VGF--->V_VM_VF KLF NFG_JP
VGF--->V_VM_VF KLF NFG_JP
VGF--->V_VM_VF KLF NFG_JP
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU

代码如下:

  #!/usr/bin/python

  import pprint

  lines1 = set()
  with open('txt1', 'r') as f1:
      lines1 = set([x.strip() for x in f1.readlines()])

  line_dict = dict()
  with open('txt2', 'r') as f2:
      for line in f2.readlines():
          line = line.strip()
          line_dict.setdefault(line, 0)
          line_dict[line] = line_dict.get(line, 0) + 1

  for line in lines1:
      print '%s : %d' % (line, line_dict.get(line, 0))

输出:

VGF--->V_VM_VF : 7
VGF--->V_VM_VF KLF NFG_JP : 6
KGF--->V_VM_VF P_NSF SSF : 4
NP--->N_NNP N_NN_S_NU N_NNP N_NNP N_NN_O_NU : 8

【讨论】:

  • 如何在python中绘制树来表示上述规则
  • 不太清楚,能否再发一个问题,让大家看看。
  • 好的,我现在标记 python。
  • 你的意思是文件的行数?
  • 要计算重复性,您需要维护一个字典或集合来做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
  • 2014-02-06
  • 2013-01-06
  • 2023-03-18
  • 1970-01-01
  • 2019-07-26
相关资源
最近更新 更多