2

我正在尝试计算文本文件中单词的频率。但我必须使用不同的方法。例如,如果文件包含 BRAIN-ISCHEMIA 和 ISCHEMIA-BRAIN,我需要计算 BRAIN-ISCHEMIA 两次(并留下 ISCHEMIA-BRAIN),反之亦然。这是我的一段代码-

// Mapping of String->Integer (word -> frequency) 
    HashMap<String, Integer> frequencyMap = new HashMap<String, Integer>(); 

    // Iterate through each line of the file 
    String[] temp;
    String currentLine; 
    String currentLine2;
    while ((currentLine = in.readLine()) != null) { 

    // Remove this line if you want words to be case sensitive 
    currentLine = currentLine.toLowerCase();
    temp=currentLine.split("-");
    currentLine2=temp[1]+"-"+temp[0];

    // Iterate through each word of the current line 
    // Delimit words based on whitespace, punctuation, and quotes 
    StringTokenizer parser = new StringTokenizer(currentLine);
    while (parser.hasMoreTokens()) { 
    String currentWord = parser.nextToken(); 

    Integer frequency = frequencyMap.get(currentWord); 

    // Add the word if it doesn't already exist, otherwise increment the 
    // frequency counter. 
    if (frequency == null) { 
    frequency = 0; 
    } 
    frequencyMap.put(currentWord, frequency + 1); 
    }
    StringTokenizer parser2 = new StringTokenizer(currentLine2);
    while (parser2.hasMoreTokens()) { 
        String currentWord2 = parser2.nextToken(); 

        Integer frequency = frequencyMap.get(currentWord2); 

        // Add the word if it doesn't already exist, otherwise increment the 
        // frequency counter. 
        if (frequency == null) { 
        frequency = 0; 
        } 
        frequencyMap.put(currentWord2, frequency + 1); 

        } 
    }

    // Display our nice little Map 
    System.out.println(frequencyMap);

但是对于以下文件-

缺血-谷氨酸 缺血-脑 谷氨酸-脑 脑-耐受 脑-耐受 耐受-脑 谷氨酸-缺血 缺血-谷氨酸

我得到以下输出-

{谷氨酸-脑=1, 缺血-谷氨酸=3, 缺血-脑=1, 谷氨酸-缺血=3, 脑-耐受=3, 脑-缺血=1, 耐受-脑=3, 脑-谷氨酸=1}

我认为问题出在第二个。任何关于这个问题的观点都将受到高度赞赏。

4

3 回答 3

2

从算法的角度来看,您可能需要考虑以下方法:

对于每个字符串,拆分、排序、然后重新组合(即取 DEF-ABC 并转换为 ABC-DEF。ABC-DEF 将转换为 ABC-DEF)。然后将其用作频率计数的关键。

如果您需要保留确切的原始项目,只需将其包含在您的密钥中 - 这样密钥将具有:序数(重新组合的字符串)和原始项目。

于 2011-01-06T01:56:26.390 回答
1

免责声明:我偷了Kevin Day为我的实施建议的甜蜜把戏。

我仍然想发布只是为了让您知道使用正确的数据结构(Multiset/Bad)和正确的库(google-guava)不仅可以简化代码,而且可以提高效率

代码

public class BasicFrequencyCalculator
{
    public static void main(final String[] args) throws IOException
    {
        @SuppressWarnings("unchecked")
        Multiset<Word> frequency = Files.readLines(new File("c:/2.txt"), Charsets.ISO_8859_1, new LineProcessor() {

            private final Multiset<Word> result = HashMultiset.create();

            @Override
            public Object getResult()
            {
                return result;
            }

            @Override
            public boolean processLine(final String line) throws IOException
            {
                result.add(new Word(line));
                return true;
            }
        });
        for (Word w : frequency.elementSet())
        {
            System.out.println(w.getOriginal() + " = " + frequency.count(w));
        }
    }
}


public class Word
{
    private final String key;

    private final String original;

    public Word(final String orig)
    {
        this.original = orig.trim();
        String[] temp = original.toLowerCase().split("-");
        Arrays.sort(temp);
        key = temp[0] + "-"+temp[1];
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getKey() == null) ? 0 : getKey().hashCode());
        return result;
    }

    @Override
    public boolean equals(final Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (!(obj instanceof Word))
        {
            return false;
        }
        Word other = (Word) obj;
        if (getKey() == null)
        {
            if (other.getKey() != null)
            {
                return false;
            }
        }
        else if (!getKey().equals(other.getKey()))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return getOriginal();
    }

    public String getKey()
    {
        return key;
    }

    public String getOriginal()
    {
        return original;
    }
}

输出

BRAIN-TOLERATE     = 3
ISCHEMIA-GLUTAMATE = 3
GLUTAMATE-BRAIN    = 1
ISCHEMIA-BRAIN     = 1
于 2011-01-06T03:38:38.837 回答
0

感谢大家的帮助。这是我解决它的方法-

// Mapping of String->Integer (word -> frequency) 
    TreeMap<String, Integer> frequencyMap = new TreeMap<String, Integer>(); 

    // Iterate through each line of the file 
    String[] temp;
    String currentLine; 
    String currentLine2;
    while ((currentLine = in.readLine()) != null) { 

    temp=currentLine.split("-");
    currentLine2=temp[1]+"-"+temp[0];

    // Iterate through each word of the current line  
    StringTokenizer parser = new StringTokenizer(currentLine);
    while (parser.hasMoreTokens()) { 
    String currentWord = parser.nextToken(); 

    Integer frequency = frequencyMap.get(currentWord);  
    Integer frequency2 = frequencyMap.get(currentLine2);

    // Add the word if it doesn't already exist, otherwise increment the 
    // frequency counter. 
    if (frequency == null) {
        if (frequency2 == null)
            frequency = 0;
        else {
            frequencyMap.put(currentLine2, frequency2 + 1);
            break;
        }//else
    } //if (frequency == null)

    frequencyMap.put(currentWord, frequency + 1);
    }//while (parser.hasMoreTokens())

    }//while ((currentLine = in.readLine()) != null)

    // Display our nice little Map 
    System.out.println(frequencyMap);
于 2011-01-06T05:35:58.663 回答