如何建语料库_语料库的建立

I have my documents as:

doc1 = very good, very bad, you are great

doc2 = very bad, good restaurent, nice place to visit

I want to make my corpus separated with , so that my final DocumentTermMatrix becomes:

terms

docs very good very bad you are great good restaurent nice place to visit

doc1 tf-idf tf-idf tf-idf 0 0

doc2 0 tf-idf 0 tf-idf tf-idf

I know, how to calculate DocumentTermMatrix of individual words but don't know how to make the corpus separated for each phrase in R. A solution in R is preferred but solution in Python is also welcomed.

What I have tried is:

> library(tm)

> library(RWeka)

> BigramTokenizer

> options(mc.cores=1)

> texts

> corpus

> a

> as.matrix(a)

I am getting:

Docs

Terms 1 2

bad good restaurent 0 1

bad you are 1 0

good restaurent nice 0 1

good very bad 1 0

nice place to 0 1

place to visit 0 1

restaurent nice place 0 1

very bad good 0 1

very bad you 1 0

very good very 1 0

you are great 1 0

What I want is not combination of words but only the phrases that I showed in my matrix.

解决方案

Here's one approach using qdap + tm packages:

library(qdap); library(tm); library(qdapTools)

dat

doc2 = "very bad, good restaurent, nice place to visit"), "text", "docs")

x

m

weightTfIdf(m)

inspect(weightTfIdf(m))

## A document-term matrix (2 documents, 5 terms)

##

## Non-/sparse entries: 4/6

## Sparsity : 60%

## Maximal term length: 19

## Weighting : term frequency - inverse document frequency (normalized) (tf-idf)

##

## Terms

## Docs good restaurent nice place to visit very bad very good you are great

## doc1 0.0000000 0.0000000 0 0.3333333 0.3333333

## doc2 0.3333333 0.3333333 0 0.0000000 0.0000000

You could also do one fell swoop and return a DocumentTermMatrix but this may be harder to understand:

x

apply_as_tm(t(wfm(x$unhold(gsub(" ", "~~", x$output)), dat$docs)),

weightTfIdf, to.qdap=FALSE)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Python中,可以使用自带的sqlite3模块来构和操作SQLite数据库。以下是使用Python构SQLite数据库的一些步骤: 1. 导入sqlite3模块 ``` import sqlite3 ``` 2. 连接到数据库 ``` conn = sqlite3.connect('database.db') ``` 其中,'database.db'是数据库文件的名称。如果该文件不存在,它将被创。 3. 创表格 ``` conn.execute('''CREATE TABLE students (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, GRADE TEXT NOT NULL);''') ``` 这将创一个名为'students'的表,其中包含四个列:ID、NAME、AGE和GRADE。 4. 插入数据 ``` conn.execute("INSERT INTO students (ID, NAME, AGE, GRADE) \ VALUES (1, 'Tom', 12, 'A')") ``` 这将在'students'表中插入一行数据,其ID为1,名字为'Tom',年龄为12,等级为'A'。 5. 查询数据 ``` cursor = conn.execute("SELECT ID, NAME, AGE, GRADE from students") for row in cursor: print("ID = ", row[0]) print("NAME = ", row[1]) print("AGE = ", row[2]) print("GRADE = ", row[3], "\n") ``` 这将从'students'表中选择所有行,并逐行打印ID、NAME、AGE和GRADE列的值。 6. 关闭数据库连接 ``` conn.close() ``` 这将关闭与数据库的连接。 希望这些步骤能帮助你开始使用Python构SQLite数据库。 ### 回答2: 要用Python自语料库,可以按照以下步骤进行: 1. 收集语料:找到适合你需求的文本数据集,如小说、新闻、网页等。可以使用网络爬虫或者下载公开的语料库。确保你拥有足够的数据,以便建立一个有代表性的语料库。 2. 数据预处理:对收集到的数据进行预处理,包括去除标点符号、去除停用词、词干化(stemming)等。这有助于减少噪音,并且使数据更加规范化和易于处理。 3. 分词:使用合适的中文分词工具将文本数据分割成词语。一些常用的中文分词工具有jieba、pkuseg等。 4. 构词表:根据分词结果,构一个词表,将每个不重复的词语映射到一个唯一的数字标识符。这将帮助我们在后续的处理中更方便地操作词语。 5. 生成语料库:使用分词结果和词表,将每个文本数据转换为一个数字序列表示。可以使用词袋模型(Bag of Words)或者TF-IDF(Term Frequency-Inverse Document Frequency)等方法来表示文本。这样,每个文本将被表示为一个向量。 6. 存储语料库:将生成的语料库保存到文件或者数据库中,以便后续使用。 7. 使用语料库:使用生成的语料库可以进行多种自然语言处理任务,比如文本分类、情感分析、机器翻译等。可以使用各种机器学习或深度学习算法来训练模型。 通过以上步骤,你可以使用Python自一个中文语料库,并利用它进行各种文本处理任务。记住,语料库的质量和规模对于任务的性能有着很大的影响,所以要尽量收集大量高质量的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值