找回密码
 加入
搜索
查看: 559|回复: 4

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

[复制链接]
发表于 2023-9-18 20:40:51 | 显示全部楼层 |阅读模式
本帖最后由 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 = [0 for i in filenames]
print("sketches: " , sketches)
for i in range(len(filenames)):
    with open(filenames[i], 'r', encoding='UTF-8') as f:
        text = f.read()
        sketches[i] = Sketch(text, k, d)
        # print("sketches[i]: " , sketches[i],'\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[i]), end = ' ')
    for j in range(len(filenames)):
        print('{: <22}'.format(sketches[i].similarTo(sketches[j])), end = ' ')
    print()
用到的2个库文件:


求教下各位懂Python的大佬。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2023-9-19 08:19:35 | 显示全部楼层
赶脚也不是很难
 楼主| 发表于 2023-9-19 11:36:06 | 显示全部楼层

看不懂Python语法&#128557;
发表于 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[UBound($vector.Item("coords"))]
    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[UBound($vector.Item("coords"))]
    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[UBound($vector.Item("coords"))]
    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[3] = [2.0, 2.0, 2.0]
    Local $yCoords[3] = [5.0, 5.0, 0.0]
    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()

评分

参与人数 2威望 +2 金钱 +159 贡献 +2 收起 理由
haijie1223 + 2 + 99 + 2 此处有掌声
zghwelcome + 60 很给力!

查看全部评分

 楼主| 发表于 2023-9-19 22:55:13 | 显示全部楼层

感谢大佬的帮助!  维度 d 设置 1000 时,au3的计算速度真的比Python逊色太多了
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-10 01:59 , Processed in 0.072246 second(s), 21 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表