【问题标题】:frequency of a word that is close to itself in a list列表中与其自身相近的单词出现的频率
【发布时间】:2021-07-07 12:06:15
【问题描述】:

我有 2 个文件,其中每个文件都出现了 3 次“test”一词。

distributed.txt

this
is
only
a
test
to
see
if
test
is
same
as
test

另一个文件有单词“test”。但它会立即重复或在 1 或 2 个单词之后重复。

closer.txt

this
is
only
a
test
to
test
test
see
if
is
same
as

“test”一词在两个文件中出现了 3 次。因此百分比是相同的。 但它在一个文件中彼此更接近,而在另一个文件中分布更广。机器学习中有什么方法可以知道一个单词在给定列表中的重复距离有多近或多远?

$ wc -l closer.txt
13 closer.txt

$ grep test closer.txt | wc -l
3

$ wc -l distributed.txt
13 distributed.txt

$ grep test distributed.txt | wc -l
3

【问题讨论】:

  • 你的意思是“均匀分布”?
  • 是的。这是正确的术语。
  • 我猜机器学习或 NLP 必须为此提供方法/模块。我可以添加那个标签@Wiktor Stribiżew 吗?
  • @shantanuo 这不是机器学习的问题。图书馆也不是,这些只是一般概念。此外,如果你只是要求一个图书馆,这是一个关于 SO 的题外话。

标签: python awk


【解决方案1】:

我会按照以下方式进行:1)获取目标词的行数(索引)2)计算标准偏差,为此,在您拥有 txt 文件的目录中创建脚本,例如将其命名为 @987654321 @:

import fileinput
import statistics
word = "test"
indices = []
for line in fileinput.input():
    if line.strip()==word:
        indices.append(fileinput.lineno())
print(statistics.stdev(indices))

然后在控制台中做

python wordspread.py distributed.txt
python wordspread.py closer.txt

这些命令会分别输出

4.0
1.5275252316519465

更高的价值意味着更大的点差。 fileinputstatistics 是内置模块,因此您无需安装除 python 之外的任何内容。请注意,此解决方案假定给定单词的出现次数在您的文件中大致相似,并且永远不会小于 2。

【讨论】:

  • 制作文件超过5000万字。 “测试”这个词出现了大约 4000 次。你的wordspread脚本返回14133024.901812198是不是表示分布均匀?
  • @shantanuo 我的解决方案是比较两个或更多文件
  • 它也适用于单个文件。如果这个数字那么大,那么价差就会更加分散。我刚检查过。感谢您的回答。
【解决方案2】:

您可以从每个文件中获取元素“test”的所有索引位置 -

For 1st file. = [4, 8, 12]
for 2nd file. = [4, 6, 7]

从索引中,我们可以得到索引之间的差异来检查元素之间的距离。这可以通过 np.diff(list)

来完成
For 1st file. = [4, 4]
For 2nd file. = [2, 1]

之后,我们可以取平均值得到平均距离。

for 1st file, it's = 4
for the 2nd file, it's = 1.5

这意味着元素“test”更靠近第二个文件。

import numpy as np
from statistics import mean

file_list = ['distributed.txt', 'closer.txt']
for file in file_list:
    with open(file) as f:
        lines = [ line.strip() for line in f ]
    indices = [i for i, x in enumerate(lines) if x == "test"] # this will return the list of index values
    diff = np.diff(indices) # to obtain the difference list
    print(mean(map(int,v)))

【讨论】:

    【解决方案3】:

    我会这样做。

    1. 首先计算每行与您的目标匹配的位置并记下行号;
    2. 然后,计算所看到的每个点之间的距离。如果该行没有目标,则添加文件行号的开始和结束;
    3. 最后,取目标之间距离的标准差。

    这是一个包含所有内容的 Python 函数:

    import statistics   
    import re 
    
    def dist_space(fn):
        ln=[]
        with open (fn) as f:
            for i, line in enumerate(f):
                if re.search(r'^test$', line):
                    ln.append(i)
        
        if ln[0]!=0: ln = [0]+ln 
        if ln[-1]!=i: ln.append(i)
        
        return statistics.stdev((j-i) for i,j in zip(ln,ln[1:]))
    

    针对您的示例进行测试:

    >>> dist_space('distributed.txt')
    0.0
    # ie, perfectly spaced between marks
    
    
    >>> dist_space('closer.txt') 
    1.8257418583505538
    # a standard deviation of 1.8 amongst the space distance -- not even
    

    【讨论】:

      【解决方案4】:

      这是一个可能的解决方案(lst 是您的单词列表):

      import numpy as np
      
      idxs = np.argwhere(np.array(lst) == 'test').flatten()
      uniform_idxs = np.linspace(-1, len(lst), idxs.size + 2)[1:-1]
      
      uniformity_index = np.abs(uniform_idxs - idxs).mean()
      

      这个解决方案很容易解释。假设您有 11 个单词,单词“test”被使用了 3 次。那么在这种情况下你就有了完美的一致性(uniformity_index = 0):

      lst = ['a', 'b', 'test', 'c', 'd', 'test', 'e', 'f', 'test', 'g', 'h']
      

      如果您的列表是这样的:

      lst = ['a', 'test', 'b', 'c', 'test', 'd', 'e', 'test', 'f', 'g', 'h']
      

      然后uniformity_index = 1。该索引表示平均索引数,您需要在列表中移动“测试”字词以获得完美的均匀分布。

      【讨论】:

        【解决方案5】:

        尝试获取单词之间的平均距离。您可以存储每个结果与下一个结果之间的步数,以便测量离散度。

        【讨论】:

        • 这应该是评论,而不是答案。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多