Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
m0_62281440
teamwork
提交
d4b17d2f
T
teamwork
项目概览
m0_62281440
/
teamwork
通知
12
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
teamwork
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d4b17d2f
编写于
3月 24, 2023
作者:
Sugar_chestnut03
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
测试文件
上级
1ce8549b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
71 addition
and
0 deletion
+71
-0
test/main.py
test/main.py
+71
-0
未找到文件。
test/main.py
0 → 100644
浏览文件 @
d4b17d2f
import
jieba
#载入jieba,分词
import
gensim
#调入genism,运算余弦相似度
import
re
#正则匹配,去除多余特殊符号
import
os
#用于查询绝对路径文件是否存在
def
get_file_contents
(
path
):
#获取指定路径的文件内容
str
=
''
f
=
open
(
path
,
'r'
,
encoding
=
'UTF-8'
)
line
=
f
.
readline
()
while
line
:
str
=
str
+
line
line
=
f
.
readline
()
f
.
close
()
return
str
#将读取到的文件内容先进行jieba分词,然后再把标点符号、转义符号等特殊符号过滤掉
def
filter
(
str
):
str
=
jieba
.
lcut
(
str
)
#进行分词
result
=
[]
for
tags
in
str
:
if
(
re
.
match
(
u
"[a-zA-Z0-9
\u4e00
-
\u9fa5
]"
,
tags
)):
#正则匹配,中英文及数字,其余全部排除
result
.
append
(
tags
)
else
:
pass
return
result
#传入过滤之后的数据,通过调用gensim.similarities.Similarity以计算余弦相似度
def
calc_similarity
(
text1
,
text2
):
texts
=
[
text1
,
text2
]
dictionary
=
gensim
.
corpora
.
Dictionary
(
texts
)
corpus
=
[
dictionary
.
doc2bow
(
text
)
for
text
in
texts
]
similarity
=
gensim
.
similarities
.
Similarity
(
'-Similarity-index'
,
corpus
,
num_features
=
len
(
dictionary
))
test_corpus_1
=
dictionary
.
doc2bow
(
text1
)
cosine_sim
=
similarity
[
test_corpus_1
][
1
]
return
cosine_sim
def
main_test
():
orig
=
input
(
"请输入原文的绝对路径:"
)
#将原文输入,测试用C:\Users\YUKI\PycharmProjects\pythonProject3\test\orig.txt
if
not
os
.
path
.
exists
(
orig
):
#检测原文绝对路径是否存在
print
(
"原文文文件不存在!"
)
exit
()
print
(
"成功装载原文"
)
copy
=
input
(
"请输入疑似抄袭文的绝对路径:"
)
#导入抄袭文,测试用:C:\Users\YUKI\PycharmProjects\pythonProject3\test\orig_0.8_dis_15.txt
if
not
os
.
path
.
exists
(
copy
):
#检测疑似抄袭文绝对路径是否存在
print
(
"抄袭论文文件不存在!"
)
exit
()
print
(
"成功装载抄袭文件"
)
save_path
=
r
"C:\Users\YUKI\PycharmProjects\pythonProject3\test\result.txt"
#导出结果文件,为绝对路径,请务必更改!!!
str1
=
get_file_contents
(
orig
)
#将源文件读入字符串str1
str2
=
get_file_contents
(
copy
)
#将抄袭文件读入字符串str2
text1
=
filter
(
str1
)
#过滤掉多余字符串
text2
=
filter
(
str2
)
similarity
=
calc_similarity
(
text1
,
text2
)
#计算相似度
print
(
"文章相似度: %.2f"
%
similarity
)
#作业要求输出浮点型2位小数
#将相似度结果写入指定文件result
result
=
result
=
round
(
similarity
.
item
(),
2
)
f
=
open
(
save_path
,
'w'
,
encoding
=
"utf-8"
)
f
.
write
(
"python"
+
" "
+
"main.py
\n
"
+
" "
+
orig
+
"
\n
"
+
copy
+
"
\n
"
+
"文章相似度: %.2f"
%
similarity
)
f
.
close
()
return
result
if
__name__
==
'__main__'
:
main_test
()
#本来是为了方便单元测试以改成这样,但后来发现影响也不大,遂懒得改了.jpg
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录