zghwelcome 发表于 2023-9-18 20:40:51

【已解决】请教下各位大佬,这段Python计算相似度的代码如何转au3?

本帖最后由 zghwelcome 于 2023-9-19 22:55 编辑


主代码


import sys
from vector import Vector
from sketch import Sketch

# 测试文档列表
filenames = ['a.txt','b.txt','c.txt']
k = 5 #k-grams
d = 10000 #文档摘要维度
sketches =
print("sketches: " , sketches)
for i in range(len(filenames)):
    with open(filenames, 'r', encoding='UTF-8') as f:
      text = f.read()
      sketches = Sketch(text, k, d)
      # print("sketches: " , sketches,'\n\n\n')

# 输出结果标题
print(' ' * 20, end = ' ')
for filename in filenames:
    print('{:>25}'.format(filename), end = ' ')
print()

# 输出结果比较明细
for i in range(len(filenames)):
    print('{:10}'.format(filenames), end = ' ')
    for j in range(len(filenames)):
      print('{: <22}'.format(sketches.similarTo(sketches)), end = ' ')
    print()



用到的2个库文件:


求教下各位懂Python的大佬。


haijie1223 发表于 2023-9-19 08:19:35

赶脚也不是很难

zghwelcome 发表于 2023-9-19 11:36:06

haijie1223 发表于 2023-9-19 08:19
赶脚也不是很难

看不懂Python语法&#128557;

rdw167 发表于 2023-9-19 13:41:06

#include <Array.au3>
#include <Math.au3>

; Vector 类定义
Func VectorCreate($a)
    Local $vector = ObjCreate("Scripting.Dictionary")
    $vector.Item("coords") = $a
    $vector.Item("n") = UBound($a)
    $vector.__index = VectorMethod
    Return $vector
EndFunc

Func VectorMethod($vector, $name, $other = Null)
    Switch $name
      Case "getitem"
            Return $vector.Item("coords")
      Case "add"
            Return VectorAdd($vector, $other)
      Case "sub"
            Return VectorSub($vector, $other)
      Case "scale"
            Return VectorScale($vector, $other)
      Case "dot"
            Return VectorDot($vector, $other)
      Case "abs"
            Return VectorAbs($vector)
      ; TODO: 您可以继续添加其他向量操作
    EndSwitch
EndFunc

Func VectorAdd($vector, $other)
    Local $result
    For $i = 0 To UBound($vector.Item("coords")) - 1
      $result[$i] = $vector.Item("coords")[$i] + $other.Item("coords")[$i]
    Next
    Return VectorCreate($result)
EndFunc

Func VectorSub($vector, $other)
    Local $result
    For $i = 0 To UBound($vector.Item("coords")) - 1
      $result[$i] = $vector.Item("coords")[$i] - $other.Item("coords")[$i]
    Next
    Return VectorCreate($result)
EndFunc

Func VectorScale($vector, $factor)
    Local $result
    For $i = 0 To UBound($vector.Item("coords")) - 1
      $result[$i] = $vector.Item("coords")[$i] * $factor
    Next
    Return VectorCreate($result)
EndFunc

Func VectorDot($vector, $other)
    Local $dotProduct = 0
    For $i = 0 To UBound($vector.Item("coords")) - 1
      $dotProduct += $vector.Item("coords")[$i] * $other.Item("coords")[$i]
    Next
    Return $dotProduct
EndFunc

Func VectorAbs($vector)
    Local $sumSquare = 0
    For $i = 0 To UBound($vector.Item("coords")) - 1
      $sumSquare += $vector.Item("coords")[$i] * $vector.Item("coords")[$i]
    Next
    Return Sqrt($sumSquare)
EndFunc

; 测试代码
Func Main()
    Local $xCoords =
    Local $yCoords =
    Local $x = VectorCreate($xCoords)
    Local $y = VectorCreate($yCoords)

    ConsoleWrite("x = " & _ArrayToString(VectorMethod($x, "getitem")) & ", y = " & _ArrayToString(VectorMethod($y, "getitem")) & @CRLF)
    ConsoleWrite("x + y = " & _ArrayToString(VectorMethod(VectorMethod($x, "add", $y), "getitem")) & @CRLF)
    ConsoleWrite("10x = " & _ArrayToString(VectorMethod(VectorMethod($x, "scale", 10), "getitem")) & @CRLF)
    ConsoleWrite("|x| = " & VectorMethod($x, "abs") & @CRLF)
    ConsoleWrite("<x, y> = " & VectorMethod($x, "dot", $y) & @CRLF)
    ConsoleWrite("|x - y| = " & VectorMethod(VectorMethod($x, "sub", $y), "abs") & @CRLF)
EndFunc

Main()

zghwelcome 发表于 2023-9-19 22:55:13

rdw167 发表于 2023-9-19 13:41
#include
#include



感谢大佬的帮助!维度 d 设置 1000 时,au3的计算速度真的比Python逊色太多了
页: [1]
查看完整版本: 【已解决】请教下各位大佬,这段Python计算相似度的代码如何转au3?