简体   繁体   English

在文档中找到的单词索引-Java

[英]Index of words found in document - Java

I am trying to write a program that takes in a text file as input, retrieves the words, and outputs each word with each line number that they are located in. I'm having a lot of trouble with this project, although I've made some progress... 我正在尝试编写一个程序,将文本文件作为输入,检索单词,然后输出每个单词及其所在的每个行号。尽管我已经在这个项目中遇到了很多麻烦取得了一些进展...

So far I have an ArrayList which holds all of the words found in the document, without punctuation marks. 到目前为止,我有一个ArrayList ,它包含在文档中找到的所有单词,没有标点符号。 I am able to output this list and see all the words in the text file, but I do not know where to go from here... any ideas? 我可以输出此列表并查看文本文件中的所有单词,但是我不知道从这里去哪里...有什么想法吗?

example: 例:

myList = [A, ACTUALLY, ALMOST,....]

I need to somehow be able to associate each word with which line they came from, so I can populate a data structure that will hold each word with their associated line number(s). 我需要以某种方式能够将每个单词与它们来自的行关联起来,因此我可以填充一个数据结构,该数据结构将使用每个单词及其关联的行号来保存它们。

I am a programming novice so I am not very familiar with all the types of data structures and algorithms out there... my instructor suggested I use a dynamic multilinked list but I don't know how I would implement that verses ArrayLists and arrays. 我是一名编程新手,所以对那里的所有类型的数据结构和算法都不是很熟悉。我的老师建议我使用动态多链接列表,但不知道如何实现ArrayLists和数组。

Any ideas would be greatly appreciated. 任何想法将不胜感激。 Thanks! 谢谢!

You should use a hash table. 您应该使用哈希表。 A hash table is a key/value pair. 哈希表是键/值对。 The key can be every word in the text file, the value, an array list containing the line numbers. 键可以是文本文件中的每个单词,值,包含行号的数组列表。

Basically, loop through every word in the text file. 基本上,循环遍历文本文件中的每个单词。 If that word is not in your list of words, add it as the key and the line number as the value in a list into the hash table. 如果该单词不在您的单词列表中,请将其作为键,并将行号作为列表中的值添加到哈希表中。 If that word is already in the table, append the line number to the array list. 如果该单词已经在表中,则将行号附加到数组列表中。

Java has good docs on a hash table here Java的一个哈希表具有良好的文档在这里

for you to get the methods you need. 让您获得所需的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM