学习参考书: http://nltk.googlecode.com/svn/trunk/doc/book/
1. 使用代理下载数据
nltk.set_proxy("**.com:80")
nltk.download()
2. 使用sents(fileid)函数时候出现:Resource 'tokenizers/punkt/english.pickle' not found. Please use the NLTK Downloader to obtain the resource:
import nltk
nltk.download()
安装窗口中选择'Models'项,然后'在 'Identifier' 列找 'punkt,点击下载安装该数据包
3. 语料Corpus元素获取函数
from nltk.corpus import webtext
webtext.fileids() #得到语料中所有文件的id集合
webtext.raw(fileid) #给定文件的所有字符集合
webtext.words(fileid) #所有单词集合
webtext.sents(fileid) #所有句子集合
4. 文本处理的一些常用函数
假若text是单词集合的列表
len(text) #单词个数
set(text) #去重
sorted(text) #排序
text.count('a') #数给定的单词的个数
text.index('a') #给定单词首次出现的位置
FreqDist(text) #单词及频率,keys()为单词,*[key]得到值
FreqDist(text).plot(50,cumulative=True) #画累积图
bigrams(text) #所有的相邻二元组
text.collocations() #找文本中频繁相邻二元组
text.concordance("word") #找给定单词出现的位置及上下文
text.similar("word") #找和给定单词语境相似的所有单词
text.common_context("a“,"b") #找两个单词相似的上下文语境
text.dispersion_plot(['a','b','c',...]) #单词在文本中的位置分布比较图
text.generate() #随机产生一段文本
to be continued
不知你是否注意到,当我们手机横屏,且使用Android自带的软键盘为EditText进行文本输入时,若不进行特殊的设置,该软键盘会占用整个界面,那么,如何让键盘只占用屏幕的一部分呢? 其实只需要改一个小小的属性即可!
<EditText android:id="@+id/text1" android:layout_width="150dip" android:layout_height="wrap_content" android:imeOptions="flagNoExtractUi"/>
另外使用android:imeOptinos可对Android自带的软键盘进行一些界面上的设置:
android:imeOptions="flagNoExtractUi" //使软键盘不全屏显示,只占用一部分屏幕
同时,这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone" //输入框右侧不带任何提示
android:imeOptions="actionGo" //右下角按键内容为'开始'
android:imeOptions="actionSearch" //右下角按键为放大镜图片,搜索
android:imeOptions="actionSend" //右下角按键内容为'发送'
android:imeOptions="actionNext" //右下角按键内容为'下一步'
android:imeOptions="actionDone" //右下角按键内容为'完成'
同时,可能EditText添加相应的监听器,捕捉用户点击了软键盘右下角按钮的监听事件,以便进行处理。
editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { Toast.makeText(MainActivity.this, "text2", Toast.LENGTH_SHORT).show(); return false; } });
创建部门表department
创建雇员表employee
新建持久化类
Department.java
package www.hbsi.net.many2one; public class Department { private Integer id; private String name; public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } }
Employee.java
package www.hbsi.net.many2one; public class Employee { private Integer id; private Department department;//反应了对象之间的关系 private String username; public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public Department getDepartment() { return this.department; } public void setDepartment(Department department) { this.department = department; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } }新建映射文件
Department.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="www.hbsi.net.many2one"> <class name="Department" table="department" catalog="hibernate"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="name" column ="name" type="string" /> </class> </hibernate-mapping>
Employee.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="www.hbsi.net.many2one"> <class name="Employee" table="employee"> <id name="id" type="java.lang.Integer"> <column name="id" /> <generator class="native" /> </id> <property name="username" type="string" column ="username" /> <many-to-one name="department" column ="dept_id" /> </class> </hibernate-mapping>更改hibernate映射问价的mapping属性
hibernate.cfg.xml;
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3306/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="www/hbsi/net/many2one/Department.hbm.xml" /> <mapping resource="www/hbsi/net/many2one/Employee.hbm.xml" /> </session-factory> </hibernate-configuration>创建测试类
Many2one.java
package www.hbsi.net.many2one; import javax.persistence.Temporal; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class Many2one { @Test public void add(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Department department = new Department(); department.setName("软件系"); Employee employee1 = new Employee(); employee1.setUsername("Nacy"); employee1.setDepartment(department); Employee employee2 = new Employee(); employee2.setUsername("Tom"); employee2.setDepartment(department); session .save(department); session .save(employee1); session .save(employee2); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }
控制台输出:
department表
employee表
Many2one.java
/** * 先保存部门在保存雇员 * 一般推荐这种方法 */ package www.hbsi.net.many2one; import org.hibernate.Session; import org.junit.Test; import www.hbsi.net.util.HibernateSessionFactory; public class Many2one { @Test public void add(){ Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Department department = new Department(); department.setName("软件系"); Employee employee1 = new Employee(); employee1.setUsername("Nacy"); employee1.setDepartment(department); Employee employee2 = new Employee(); employee2.setUsername("Tom"); employee2.setDepartment(department); session .save(department); session .save(employee1); session .save(employee2); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } /** * 通过雇员ID查找所属部门 */ @Test public void find() { Session session = HibernateSessionFactory.getSession(); session.beginTransaction(); Employee employee = (Employee) session.get(Employee.class, 5); System.out.println(employee.getUsername()+"--"+employee.getDepartment().getName()); session.getTransaction().commit(); HibernateSessionFactory.closeSession(); } }