斯坦福NLP CS224N Winter 2024 作业:Assignment 1

本文介绍了如何在IT技术背景下,使用Python实现文本分析中的两个关键步骤:从给定的多句子语料中去除重复单词并创建一个排序后的列表,以及计算单词之间的共现矩阵以衡量它们在文档中的共现频率。
摘要由CSDN通过智能技术生成

原始题目
https://web.stanford.edu/class/cs224n/assignments/a1_preview/exploring_word_vectors.html

Question 1.1: Implement distinct_words [code] (2 points)

输入:
· corpus: 包括多个句子的语料

输出

  • corpus_words: 先去重,再排序后的单词列表
  • num_corpus_words:去重重复后的单词数量
def distinct_words(corpus):
    """ Determine a list of distinct words for the corpus.
        Params:
            corpus (list of list of strings): corpus of documents
        Return:
            corpus_words (list of strings): sorted list of distinct words across the corpus
            n_corpus_words (integer): number of distinct words across the corpus
    """
    corpus_words = []
    n_corpus_words = -1
    
    # ------------------
    # Write your implementation here.
    distinct_words_set  = set()
    for sentence in corpus:
        distinct_words_set.update(sentence )

    corpus_words = sorted(list(distinct_words_set))
    num_corpus_words = len(corpus_words)
   
    # ------------------

    return corpus_words, num_corpus_words
    ``
    # Question 1.2: Implement compute_co_occurrence_matrix [code] (3 points)
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值