【DataStructure】Another usage of Map: Concordance

2024-03-02 20:32

本文主要是介绍【DataStructure】Another usage of Map: Concordance,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Statements: This blog was written by me, but most of content  is quoted from book【Data Structure with Java Hubbard】 


【Description】

Aconcordanceis a list of words that appear in a textdocument along with the numbers of the lines on which the words appear. It is just like an index of a book except that it lists line numbers instead of page.numbers. Concordances are useful for analyzing documents to find word frequencies and associations that are not evident from reading the document directly. This program builds a concordance for a text file. The run here uses this particular text taken from Shakespeare’s play Julius Caesar. The first part of the resulting concordance is shown on the right.

【Implement】

package com.albertshao.ds.map;//  Data Structures with Java, Second Edition
//  by John R. Hubbard
//  Copyright 2007 by McGraw-Hillimport java.io.*;
import java.util.*;public class Concordance {private Map<String,String> map = new HashMap<String,String>();public Concordance(String file) {int lineNumber = 0;try {Scanner input = new Scanner(new File(file));while (input.hasNextLine()) {String line = input.nextLine();++lineNumber;StringTokenizer parser = new StringTokenizer(line,",.;:()-!?' ");while (parser.hasMoreTokens()) {String word = parser.nextToken().toUpperCase();String listing = map.get(word);if (listing == null) {listing = "" + lineNumber;} else {listing += ", " + lineNumber;}map.put(word,listing);}}input.close();} catch(IOException e) {System.out.println(e);}}public void write(String file) {try {PrintWriter output = new PrintWriter(file);for (Map.Entry<String,String> entry : map.entrySet()) {output.println(entry);}output.close();} catch(IOException e) {System.out.println(e);}}
}

package com.albertshao.ds.map;//  Data Structures with Java, Second Edition
//  by John R. Hubbard
//  Copyright 2007 by McGraw-Hillpublic class TestConcordance {public static final String PATH = "D:\\machao\\DataStructure\\src\\com\\albertshao\\ds\\map\\";public static final String IN_FILE = "Shakespeare.txt";public static final String OUT_FILE = "Shakespeare.out";public static void main(String[] args) {Concordance c = new Concordance(PATH+IN_FILE);c.write(PATH+OUT_FILE);}
}

【Result】

The content in the Shakespeare.txt:
<span style="font-family:Arial;">Friends, Romans, countrymen, lend me your ears!
I come to bury Caesar, not to praise him.
The evil that men do lives after them,
The good is oft interred with their bones;
So let it be with Caesar. The noble Brutus
Hath told you Caesar was ambitious;
If it were so, it was a grievous fault;
And grievously hath Caesar answer'd it.
Here, under leave of Brutus and the rest, --
For Brutus is an honourable man;
So are they all, all honourable men.
Come I to speak in Caesar's funeral.
He was my friend, faithful and just to me.
But Brutus says he was ambitious;
And Brutus is an honourable man.
He hath brought many captives home to Rome.
Whose ransoms did the general coffers fill:
Did this in Caesar seem ambitious?
When that the poor have cried, Caesar hath wept;
Ambition should be made of sterner stuff.
Yet Brutus says he was ambitious;
And Brutus is an honourable man.
You all did see that on the Lupercal
I thrice presented him with a kingly crown,
Which he did thrice refuse: was this ambition?
Yet Brutus says he was ambitious;
And, sure, is an honourable man.
I speak not to disprove what Brutus spoke,
But here I am to speak what I do know.
You all did love him once, not without cause.
What cause withholds you, then, to mourn for him?
O judgement! thou art fled to brutish beasts,
And men have lost their reason!
</span>

The result also means the content in the Shakespeare.out:
<span style="font-family:Arial;font-size:14px;">GRIEVOUS=7
WHAT=28, 29, 31
KINGLY=24
REST=9
JUDGEMENT=32
SURE=27
CAUSE=30, 31
REFUSE=25
ME=1, 13
DO=3, 29
THEIR=4, 33
FUNERAL=12
NOT=2, 28, 30
YET=21, 26
CAESAR=2, 5, 6, 8, 12, 18, 19
LEAVE=9
THAT=3, 19, 23
COFFERS=17
HIM=2, 24, 30, 31
ARE=11
MADE=20
MY=13
CROWN=24
MOURN=31
FRIEND=13
THIS=18, 25
CAPTIVES=16
OFT=4
PRAISE=2
ROMANS=1
YOU=6, 23, 30, 31
HERE=9, 29
BURY=2
GRIEVOUSLY=8
WITHHOLDS=31
D=8
BEASTS=32
A=7, 24
O=32
LEND=1
WITHOUT=30
I=2, 12, 24, 28, 29, 29
SAYS=14, 21, 26
ANSWER=8
ON=23
CRIED=19
BUT=14, 29
STUFF=20
WEPT=19
ART=32
YOUR=1
S=12
OF=9, 20
AMBITIOUS=6, 14, 18, 21, 26
MANY=16
FLED=32
GENERAL=17
HE=13, 14, 16, 21, 25, 26
INTERRED=4
MEN=3, 11, 33
EVIL=3
FRIENDS=1
POOR=19
NOBLE=5
KNOW=29
WHOSE=17
LUPERCAL=23
BRUTISH=32
FAULT=7
THE=3, 4, 5, 9, 17, 19, 23
WERE=7
FOR=10, 31
THEY=11
THRICE=24, 25
AND=8, 9, 13, 15, 22, 27, 33
IF=7
UNDER=9
THEM=3
THEN=31
SEE=23
IN=12, 18
FILL=17
IS=4, 10, 15, 22, 27
ROME=16
IT=5, 7, 7, 8
WAS=6, 7, 13, 14, 21, 25, 26
ALL=11, 11, 23, 30
HAVE=19, 33
TOLD=6
LOST=33
ONCE=30
FAITHFUL=13
BRUTUS=5, 9, 10, 14, 15, 21, 22, 26, 28
AM=29
WITH=4, 5, 24
AN=10, 15, 22, 27
WHICH=25
HONOURABLE=10, 11, 15, 22, 27
TO=2, 2, 12, 13, 16, 28, 29, 31, 32
SPOKE=28
SHOULD=20
LIVES=3
BONES=4
BE=5, 20
AFTER=3
COUNTRYMEN=1
SPEAK=12, 28, 29
DID=17, 18, 23, 25, 30
AMBITION=20, 25
COME=2, 12
SEEM=18
REASON=33
BROUGHT=16
LOVE=30
STERNER=20
MAN=10, 15, 22, 27
WHEN=19
DISPROVE=28
PRESENTED=24
SO=5, 7, 11
HATH=6, 8, 16, 19
JUST=13
HOME=16
THOU=32
EARS=1
GOOD=4
LET=5
RANSOMS=17
</span>



这篇关于【DataStructure】Another usage of Map: Concordance的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/767336

相关文章

Collection List Set Map的区别和联系

Collection List Set Map的区别和联系 这些都代表了Java中的集合,这里主要从其元素是否有序,是否可重复来进行区别记忆,以便恰当地使用,当然还存在同步方面的差异,见上一篇相关文章。 有序否 允许元素重复否 Collection 否 是 List 是 是 Set AbstractSet 否

Map

Map 是 Java 中用于存储键值对的集合接口。以下是对 Map 的详细介绍: 特点 键值对存储:每个元素包含一个键和一个值。 键唯一:键不能重复,但值可以重复。 无序/有序:根据具体实现,键值对的顺序可能无序(如 HashMap)或有序(如 TreeMap、LinkedHashMap)。 主要实现类 HashMap 基于哈希表,无序存储。 允许一个 null 键和多个 null 值。

Java中集合类Set、List和Map的区别

Java中的集合包括三大类,它们是Set、List和Map,它们都处于java.util包中,Set、List和Map都是接口,它们有各自的实现类。Set的实现类主要有HashSet和TreeSet,List的实现类主要有ArrayList,Map的实现类主要有HashMap和TreeMap。那么它们有什么区别呢? Set中的对象不按特定方式排序,并且没有重复对象。但它的有些实现类能对集合中的对

C++数据结构重要知识点(5)(哈希表、unordered_map和unordered_set封装)

1.哈希思想和哈希表 (1)哈希思想和哈希表的区别 哈希(散列、hash)是一种映射思想,本质上是值和值建立映射关系,key-value就使用了这种思想。哈希表(散列表,数据结构),主要功能是值和存储位置建立映射关系,它通过key-value模型中的key来定位数组的下标,将value存进该位置。 哈希思想和哈希表数据结构这两个概念要分清,哈希是哈希表的核心思想。 (2)unordered

【C++STL(十四)】一个哈希桶简单模拟实现unordered_map/set

目录 前言 一、改造哈希桶 改造结点类 改造主体  模板参数改造  迭代器(重点) 改造完整哈希桶 unordered_map模拟实现 unordered_set模拟实现 前言 前面的文章都有说unordered_map/set的底层结构就是哈希表,严格来说是哈希桶,那么接下来我们就尝试使用同一个哈希桶来模拟实现一下。整体的逻辑和一棵红黑树封装map/set类似,所以

Java中Map取值转String Null值处理

Map<String, Object> 直接取值转String String value = (String)map.get("key") 当map.get(“key”)为Null值时会报错。 使用String类的valueOf静态方法可以解决这个问题 String value = String.valueOf(map.get("key"))

Creating OpenAI Gym Environment from Map Data

题意:从地图数据创建 OpenAI Gym 环境 问题背景: I am just starting out with reinforcement learning and trying to create a custom environment with OpenAI gym. However, I am stumped with trying to create an enviro

【Java编程的逻辑】Map和Set

HashMap Map有键和值的概念。一个键映射到一个值,Map按照键存储和访问值,键不能重复。 HashMap实现了Map接口。 基本原理 HashMap的基本实现原理:内部有一个哈希表,即数组table,每个元素table[i]指向一个单向链表,根据键存取值,用键算出hash值,取模得到数组中的索引位置index,然后操作table[index]指向的单向链表。 存取的时候依据键的

RDD的map和flatMap

在 Apache Spark 中,map 和 flatMap 是 RDD(弹性分布式数据集)中最常用的转换操作之一。 map 假设你有一个包含整数的 RDD,你想要计算每个元素的平方。 from pyspark import SparkContextsc = SparkContext(appName="MapExample")# 创建一个包含整数的 RDDnumbers = sc.para

【python 多进程传参】pool.map() 函数传多参数

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。人工智能教程 一、背景介绍 相信很多人都用过,pool.map()函数,这个函数,有两个参数可以传,第一个参数传的是函数,第二个参数传的是数据列表。 那么怎么在第二个数据列表,多传几个参数呢,方法是通过对有多个参数的方法进行封装