简体   繁体   English

如何将python中的字符串转换为数字向量,以便我可以将它们与其他向量进行比较?

[英]How to convert strings in python to vectors of numbers so i can compare them to other vectors?

I am looking for a way to convert Strings into vectors of numbers in phyton.我正在寻找一种方法将字符串转换为 phyton 中的数字向量。 Like喜欢

"Hi how are you?" “你好你好吗?” -> "29 73 281 38" -> "29 73 281 38"
"How are you doing" -> "73 281 28 54" “你好吗”->“73 281 28 54”

I want to compare sentences from a user input to sentences out of a databese, which are stored as vectors.我想将来自用户输入的句子与存储为向量的数据库中的句子进行比较。

I am assuming you are trying to create a dense vector representation for your input sentences.我假设您正在尝试为您的输入句子创建一个密集的向量表示。

See if below code helps.看看下面的代码是否有帮助。

sentences = ["Hi how are you?", "How are you doing"]

# Step 1: Create vocabulary - a set of distinct tokens from your input sentences 
vocab = set()
for sentence in sentences:
    tokens = sentence.split()
    for token in tokens:
        vocab.add(token)

# Step 2: Create a map (token: ID)
vocab_map = {}
for i, token in enumerate(sorted(vocab)): # sorted lexicographically for reproducibility 
    vocab_map[token] = i

# encode the sentences using the map you created in the previous step
for sentence in sentences:
    encoded_sentence = []
    tokens = sentence.split()
    for token in tokens:
        encoded_sentence.append(str(vocab_map[token]))
    print(' '.join(encoded_sentence))

Running the above code should get output the following:运行上面的代码应该得到以下输出:

0 4 2 6
1 2 5 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python - 如何将列表列表转换为python中的集合,以便与其他集合进行比较? - How to convert list of lists to a set in python so I can compare to other sets? 在Python上连接向量字符串 - Join vectors strings on Python 假设我有2个向量。 我可以使用哪些算法进行比较? - Suppose I have 2 vectors. What algorithms can I use to compare them? 如何有效地将布尔表转换为一个热向量? - How can I convert a boolean table to one hot vectors efficiently? 如何在Python中按零和一的向量排列顺序,使最后一个索引上带有零的向量保持在底部? - How can I arrange vectors of Zeros and Ones in Python, in an order on which the vectors with ones on the last index stay on the bottom? 在Python中,如何将数字和字符串都转换为字节数组? - In Python, how can I convert both numbers and strings into byte arrays? 如何找出在 Python 中处理了多少个向量? - How I can find out how many vectors are processed in Python? 我如何在python中获得矢量的点积(列表形式) - how can i get the dot product of vectors (list form) in python 如何在python中绘制由向量给出的结构的表面? - How can I plot the surface of a structure which is given by vectors in python? 如何在此Python作业中将列表,标量和向量联系在一起? - How can I tie lists, scalars and vectors together in this Python assignment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM