Files
task-3-2-1-Text-Processing-…/260421_2509165028.py
2509165028 0e315ac98d 3-2-1
2026-04-23 16:06:54 +08:00

41 lines
1.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
# 第一部分题目1
s="Hello"
print("ASCII码分别为",[ord(o) for o in s])
print(f"ASCII码65对应的字符是{chr(65)}")
# 第二部分题目3
A=np.array([3,4])
B=np.array([1,2])
print(f"计算A+B的结果为{A+B}")
print(f"计算A*2的结果为{A*2}")
length=np.linalg.norm(A)
print(f"向量A的长度为{length}")
# 第二部分题目4
A=np.array([1,2,3])
B=np.array([4,5,6])
dot=np.dot(A,B)
print(f"A·B点积为{dot}")
def cosine_similarity(A, B):
dot = np.dot(A, B)
norm_a = np.linalg.norm(A)
norm_b = np.linalg.norm(B)
return dot / (norm_a * norm_b)
print(f"相似度 = {cosine_similarity(A, B):.3f}")
# 第三部分题目5
from sklearn.feature_extraction.text import CountVectorizer
docs = [
"Python 是 编程 语言",
"Java 是 编程 语言",
"Python Python Python"
]
vectorizer = CountVectorizer()
bow_matrix = vectorizer.fit_transform(docs)
print("词表:", vectorizer.get_feature_names_out())
print("BoW矩阵:")
print(bow_matrix.toarray())
# 第三部分题目6
print("BoW模型的缺点忽略词序所有词同等重要")